9 #ifndef LIBPMEMOBJ_CPP_MUTEX_HPP
10 #define LIBPMEMOBJ_CPP_MUTEX_HPP
13 #include <libpmemobj/thread.h>
14 #include <libpmemobj/tx_base.h>
46 if ((pop = pmemobj_pool_by_ptr(&plock)) ==
nullptr)
48 1, std::generic_category(),
49 "Persistent mutex not from persistent memory.");
51 pmemobj_mutex_zero(pop, &plock);
73 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
74 if (
int ret = pmemobj_mutex_lock(pop, &this->plock))
75 throw detail::exception_with_errormsg<lock_error>(
76 ret, std::system_category(),
77 "Failed to lock a mutex.");
97 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
98 int ret = pmemobj_mutex_trylock(pop, &this->plock);
102 else if (ret == EBUSY)
105 throw detail::exception_with_errormsg<lock_error>(
106 ret, std::system_category(),
107 "Failed to lock a mutex.");
120 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
121 int ret = pmemobj_mutex_unlock(pop, &this->plock);
123 throw detail::exception_with_errormsg<lock_error>(
124 ret, std::system_category(),
125 "Failed to unlock a mutex.");
147 return TX_PARAM_MUTEX;
Custom lock error class.
Definition: pexceptions.hpp:121
Persistent memory resident mutex implementation.
Definition: mutex.hpp:33
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:134
mutex()
Default constructor.
Definition: mutex.hpp:43
enum pobj_tx_param lock_type() const noexcept
The type of lock needed for the transaction API.
Definition: mutex.hpp:145
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: mutex.hpp:36
void unlock()
Unlocks a previously locked mutex.
Definition: mutex.hpp:118
mutex & operator=(const mutex &)=delete
Deleted assignment operator.
void lock()
Locks the mutex, blocks if already locked.
Definition: mutex.hpp:71
~mutex()=default
Defaulted destructor.
mutex(const mutex &)=delete
Deleted copy constructor.
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: mutex.hpp:95
Persistent memory namespace.
Definition: allocation_flag.hpp:15