cds
2.3.2
|
Array of locks. More...
#include <cds/sync/lock_array.h>
Public Types | |
typedef Lock | lock_type |
lock type | |
typedef SelectPolicy | select_cell_policy |
Cell selection policy functor. | |
Public Member Functions | |
lock_array (size_t nCapacity) | |
Constructs array of locks. More... | |
lock_array (size_t nCapacity, select_cell_policy const &policy) | |
Constructs array of lock and copy cell selection policy. More... | |
lock_array (size_t nCapacity, select_cell_policy &&policy) | |
Constructs array of lock and move cell selection policy. More... | |
~lock_array () | |
Destructs array of locks and frees used memory. | |
template<typename Q > | |
size_t | lock (Q const &hint) |
Locks a lock at cell hint . More... | |
template<typename Q > | |
size_t | try_lock (Q const &hint) |
Try lock a lock at cell hint . More... | |
void | unlock (size_t nCell) |
Unlock the lock specified by index nCell . | |
void | lock_all () |
Lock all. | |
void | unlock_all () |
Unlock all. | |
lock_type & | at (size_t nCell) const |
Get lock at cell nCell . More... | |
size_t | size () const |
Size of lock array. | |
Static Public Attributes | |
static size_t const | c_nUnspecifiedCell = (size_t) -1 |
failed try_lock call result | |
Protected Attributes | |
lock_type * | m_arrLocks |
lock array | |
size_t const | m_nCapacity |
array capacity | |
select_cell_policy | m_SelectCellPolicy |
Cell selection policy. | |
Array of locks.
The lock array is useful for building fine-grained lock-based data structure based on striping technique. Instead of locking access to data struct (a hash map, for example) at whole, the striping locks only part of the map (a bucket). So, access to different buckets can be simultaneous.
Template arguments:
Lock
- lock type, for example, std::mutex
, cds::sync::spin_lock
SelectPolicy
- array cell selection policy, the default is mod_select_policy Available policies: trivial_select_policy, pow2_select_policy, mod_select_policy.Alloc
- memory allocator for arrayTo determine array's cell the selection policy SelectPolicy
functor is used. Two arguments are passed to the policy:
nHint
- a hint to calculate cell index in the lock array. Usually, it is a hash value.nCapacity
- the size of the lock array The functor should return the index in the lock array.Note that the type of nHint
parameter can be any.
|
inline |
Constructs array of locks.
Allocates the array and initializes all locks as unlocked.
[in] | nCapacity | Array size |
|
inline |
Constructs array of lock and copy cell selection policy.
Allocates the array and initializes all locks as unlocked.
[in] | nCapacity | Array size |
policy | Cell selection policy (copy-constructible) |
|
inline |
Constructs array of lock and move cell selection policy.
Allocates the array and initializes all locks as unlocked.
[in] | nCapacity | Array size |
policy | Cell selection policy (move-constructible) |
|
inline |
Get lock at cell nCell
.
Precondition: nCell < size()
|
inline |
Locks a lock at cell hint
.
To define real array's cell which should be locked, select_cell_policy is used. The target cell is a result of select_cell_policy( hint, size())
.
Returns the index of locked lock.
|
inline |
Try lock a lock at cell hint
.
To define real array's cell which should be locked, select_cell_policy is used. The target cell is a result of select_cell_policy( hint, size())
.
Returns the index of locked lock if success, c_nUnspecifiedCell constant otherwise.