9 #ifndef LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP
10 #define LIBPMEMOBJ_CPP_SHARED_MUTEX_HPP
12 #include <libpmemobj/thread.h>
13 #include <libpmemobj/tx_base.h>
46 if ((pop = pmemobj_pool_by_ptr(&plock)) ==
nullptr)
48 1, std::generic_category(),
49 "Persistent shared mutex not from persistent memory.");
51 pmemobj_rwlock_zero(pop, &plock);
74 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
75 if (
int ret = pmemobj_rwlock_wrlock(pop, &this->plock))
76 throw detail::exception_with_errormsg<lock_error>(
77 ret, std::system_category(),
78 "Failed to lock a shared mutex.");
99 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
100 if (
int ret = pmemobj_rwlock_rdlock(pop, &this->plock))
101 throw detail::exception_with_errormsg<lock_error>(
102 ret, std::system_category(),
103 "Failed to shared lock a shared mutex.");
123 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
124 int ret = pmemobj_rwlock_trywrlock(pop, &this->plock);
128 else if (ret == EBUSY)
131 throw detail::exception_with_errormsg<lock_error>(
132 ret, std::system_category(),
133 "Failed to lock a shared mutex.");
155 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
156 int ret = pmemobj_rwlock_tryrdlock(pop, &this->plock);
160 else if (ret == EBUSY)
163 throw detail::exception_with_errormsg<lock_error>(
164 ret, std::system_category(),
165 "Failed to lock a shared mutex.");
177 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
178 int ret = pmemobj_rwlock_unlock(pop, &this->plock);
180 throw detail::exception_with_errormsg<lock_error>(
181 ret, std::system_category(),
182 "Failed to unlock a shared mutex.");
216 return TX_PARAM_RWLOCK;
Custom lock error class.
Definition: pexceptions.hpp:121
Persistent memory resident shared_mutex implementation.
Definition: shared_mutex.hpp:32
void lock_shared()
Lock the mutex for shared access.
Definition: shared_mutex.hpp:97
shared_mutex()
Default constructor.
Definition: shared_mutex.hpp:43
void unlock_shared()
Unlocks the mutex.
Definition: shared_mutex.hpp:192
native_handle_type native_handle() noexcept
Access a native handle to this shared mutex.
Definition: shared_mutex.hpp:203
shared_mutex & operator=(const shared_mutex &)=delete
Deleted assignment operator.
shared_mutex(const shared_mutex &)=delete
Deleted copy constructor.
~shared_mutex()=default
Defaulted destructor.
void unlock()
Unlocks the mutex.
Definition: shared_mutex.hpp:175
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: shared_mutex.hpp:214
PMEMrwlock * native_handle_type
Implementation defined handle to the native type.
Definition: shared_mutex.hpp:35
void lock()
Lock the mutex for exclusive access.
Definition: shared_mutex.hpp:72
bool try_lock()
Try to lock the mutex for exclusive access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:121
bool try_lock_shared()
Try to lock the mutex for shared access, returns regardless if the lock succeeds.
Definition: shared_mutex.hpp:153
Persistent memory namespace.
Definition: allocation_flag.hpp:15