Tkrzw
Public Member Functions | List of all members
tkrzw::HashMutex< SHAREDMUTEX > Class Template Referencefinal

Mutex for a hash table. More...

#include <tkrzw_thread_util.h>

Public Member Functions

 HashMutex (int32_t num_slots, int64_t num_buckets, uint64_t(*hash_func)(std::string_view, uint64_t))
 Constructor. More...
 
 ~HashMutex ()
 Destructor. More...
 
 HashMutex (const HashMutex &rhs)=delete
 Copy and assignment are disabled. More...
 
HashMutexoperator= (const HashMutex &rhs)=delete
 
int32_t GetNumSlots () const
 Gets the number of the slots. More...
 
int64_t GetNumBuckets () const
 Gets the number of the buckets. More...
 
void Rehash (int64_t num_buckets)
 Modifies the number of buckets. More...
 
int64_t GetBucketIndex (std::string_view data)
 Gets the index of the bucket of data. More...
 
int64_t LockOne (std::string_view data)
 Gets exclusive ownership of a slot of a bucket. More...
 
bool LockOne (int64_t bucket_index)
 Gets exclusive ownership of a slot by a bucket index. More...
 
void UnlockOne (int64_t bucket_index)
 Releases exclusive ownership of a slot of a bucket. More...
 
int64_t LockOneShared (std::string_view data)
 Gets shared ownership of a slot by a bucket index. More...
 
bool LockOneShared (int64_t bucket_index)
 Gets shared ownership of a slot of a bucket. More...
 
void UnlockOneShared (int64_t bucket_index)
 Releases shared ownership of a slot of a bucket. More...
 
void LockAll ()
 Gets exclusive ownership of all slots. More...
 
void UnlockAll ()
 Releases exclusive ownership of all slots. More...
 
void LockAllShared ()
 Gets shared ownership of all slots. More...
 
void UnlockAllShared ()
 Releases shared ownership of all slots. More...
 
std::vector< int64_t > LockMulti (const std::vector< std::string_view > &data_list)
 Gets exclusive ownership of slots of multiple buckets. More...
 
void UnlockMulti (const std::vector< int64_t > &bucket_indices)
 Releases exclusive ownership of slots of multiple buckets. More...
 
std::vector< int64_t > LockMultiShared (const std::vector< std::string_view > &data_list)
 Gets shared ownership of slots of multiple buckets. More...
 
void UnlockMultiShared (const std::vector< int64_t > &bucket_indices)
 Releases shared ownership of slots of multiple buckets. More...
 

Detailed Description

template<typename SHAREDMUTEX = std::shared_mutex>
class tkrzw::HashMutex< SHAREDMUTEX >

Mutex for a hash table.

Constructor & Destructor Documentation

◆ HashMutex() [1/2]

template<typename SHAREDMUTEX >
tkrzw::HashMutex< SHAREDMUTEX >::HashMutex ( int32_t  num_slots,
int64_t  num_buckets,
uint64_t(*)(std::string_view, uint64_t)  hash_func 
)

Constructor.

Parameters
num_slotsThe number of slots.
num_bucketsThe number of buckets.
hash_funcA hash function which takes a string view object and a bucket number and returns a bucket index.

◆ ~HashMutex()

template<typename SHAREDMUTEX >
tkrzw::HashMutex< SHAREDMUTEX >::~HashMutex

Destructor.

◆ HashMutex() [2/2]

template<typename SHAREDMUTEX = std::shared_mutex>
tkrzw::HashMutex< SHAREDMUTEX >::HashMutex ( const HashMutex< SHAREDMUTEX > &  rhs)
explicitdelete

Copy and assignment are disabled.

Member Function Documentation

◆ GetNumSlots()

template<typename SHAREDMUTEX >
int32_t tkrzw::HashMutex< SHAREDMUTEX >::GetNumSlots

Gets the number of the slots.

@ return the number of the slots.

◆ GetNumBuckets()

template<typename SHAREDMUTEX >
int64_t tkrzw::HashMutex< SHAREDMUTEX >::GetNumBuckets

Gets the number of the buckets.

Returns
The number of the buckets.

◆ Rehash()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::Rehash ( int64_t  num_buckets)

Modifies the number of buckets.

Parameters
num_bucketsThe new number of the buckets.

Precondition: The thread must have called the LockOne method.

◆ GetBucketIndex()

template<typename SHAREDMUTEX >
int64_t tkrzw::HashMutex< SHAREDMUTEX >::GetBucketIndex ( std::string_view  data)

Gets the index of the bucket of data.

Parameters
dataThe data set in the hash table.
Returns
The index of the bucket which the data belongs to.

Precondition: The thread must have locked the bucket or all buckets.

◆ LockOne() [1/2]

template<typename SHAREDMUTEX >
int64_t tkrzw::HashMutex< SHAREDMUTEX >::LockOne ( std::string_view  data)

Gets exclusive ownership of a slot of a bucket.

Parameters
dataThe data to be set in the hash table.
Returns
The index of the bucket which the data should belong to.

◆ LockOne() [2/2]

template<typename SHAREDMUTEX >
bool tkrzw::HashMutex< SHAREDMUTEX >::LockOne ( int64_t  bucket_index)

Gets exclusive ownership of a slot by a bucket index.

Parameters
bucket_indexThe index of the bucket to lock.
Returns
True on success or False on failure.

◆ UnlockOne()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::UnlockOne ( int64_t  bucket_index)

Releases exclusive ownership of a slot of a bucket.

Parameters
bucket_indexThe index of the bucket to unlock.

◆ LockOneShared() [1/2]

template<typename SHAREDMUTEX >
int64_t tkrzw::HashMutex< SHAREDMUTEX >::LockOneShared ( std::string_view  data)

Gets shared ownership of a slot by a bucket index.

Parameters
dataThe data to be set in the hash table.
Returns
The index of the bucket which the data should belong to.

◆ LockOneShared() [2/2]

template<typename SHAREDMUTEX >
bool tkrzw::HashMutex< SHAREDMUTEX >::LockOneShared ( int64_t  bucket_index)

Gets shared ownership of a slot of a bucket.

Parameters
bucket_indexThe index of the bucket to lock.
Returns
True on success or False on failure.

◆ UnlockOneShared()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::UnlockOneShared ( int64_t  bucket_index)

Releases shared ownership of a slot of a bucket.

Parameters
bucket_indexThe index of the bucket to unlock.

◆ LockAll()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::LockAll

Gets exclusive ownership of all slots.

◆ UnlockAll()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::UnlockAll

Releases exclusive ownership of all slots.

◆ LockAllShared()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::LockAllShared

Gets shared ownership of all slots.

◆ UnlockAllShared()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::UnlockAllShared

Releases shared ownership of all slots.

◆ LockMulti()

template<typename SHAREDMUTEX >
std::vector< int64_t > tkrzw::HashMutex< SHAREDMUTEX >::LockMulti ( const std::vector< std::string_view > &  data_list)

Gets exclusive ownership of slots of multiple buckets.

Parameters
data_listThe data list to be set in the hash table.
Returns
The indices of the buckets which the data list should belong to.

◆ UnlockMulti()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::UnlockMulti ( const std::vector< int64_t > &  bucket_indices)

Releases exclusive ownership of slots of multiple buckets.

Parameters
bucket_indicesThe indices of the buckets to unlock.

◆ LockMultiShared()

template<typename SHAREDMUTEX >
std::vector< int64_t > tkrzw::HashMutex< SHAREDMUTEX >::LockMultiShared ( const std::vector< std::string_view > &  data_list)

Gets shared ownership of slots of multiple buckets.

Parameters
data_listThe data list to be set in the hash table.
Returns
The indices of the buckets which the data list should belong to.

◆ UnlockMultiShared()

template<typename SHAREDMUTEX >
void tkrzw::HashMutex< SHAREDMUTEX >::UnlockMultiShared ( const std::vector< int64_t > &  bucket_indices)

Releases shared ownership of slots of multiple buckets.

Parameters
bucket_indicesThe indices of the buckets to unlock.