Tkrzw
Public Member Functions | List of all members
tkrzw::HashMutex Class 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...
 
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 exclusive ownership of all slots. More...
 

Detailed Description

Mutex for a hash table.

Constructor & Destructor Documentation

◆ HashMutex()

tkrzw::HashMutex::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()

tkrzw::HashMutex::~HashMutex ( )

Destructor.

Member Function Documentation

◆ GetNumSlots()

int32_t tkrzw::HashMutex::GetNumSlots ( ) const

Gets the number of the slots.

@ return the number of the slots.

◆ GetNumBuckets()

int64_t tkrzw::HashMutex::GetNumBuckets ( ) const

Gets the number of the buckets.

Returns
The number of the buckets.

◆ Rehash()

void tkrzw::HashMutex::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()

int64_t tkrzw::HashMutex::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]

int64_t tkrzw::HashMutex::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]

bool tkrzw::HashMutex::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()

void tkrzw::HashMutex::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]

int64_t tkrzw::HashMutex::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]

bool tkrzw::HashMutex::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()

void tkrzw::HashMutex::UnlockOneShared ( int64_t  bucket_index)

Releases shared ownership of a slot of a bucket.

Parameters
bucket_indexThe index of the bucket to unlock.

◆ LockAll()

void tkrzw::HashMutex::LockAll ( )

Gets exclusive ownership of all slots.

◆ UnlockAll()

void tkrzw::HashMutex::UnlockAll ( )

Releases exclusive ownership of all slots.

◆ LockAllShared()

void tkrzw::HashMutex::LockAllShared ( )

Gets shared ownership of all slots.

◆ UnlockAllShared()

void tkrzw::HashMutex::UnlockAllShared ( )

Releases exclusive ownership of all slots.