38 #ifndef LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP 39 #define LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP 44 #include <libpmemobj/thread.h> 62 typedef std::chrono::system_clock clock_type;
76 if ((pop = pmemobj_pool_by_ptr(&
plock)) ==
nullptr)
78 1, std::generic_category(),
79 "Persistent mutex not from persistent memory.");
81 pmemobj_mutex_zero(pop, &
plock);
103 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
104 if (
int ret = pmemobj_mutex_lock(pop, &this->
plock))
106 "Failed to lock a mutex.")
107 .with_pmemobj_errormsg();
127 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
128 int ret = pmemobj_mutex_trylock(pop, &this->
plock);
132 else if (ret == EBUSY)
136 "Failed to lock a mutex.")
137 .with_pmemobj_errormsg();
157 template <
typename Clock,
typename Duration>
160 const std::chrono::time_point<Clock, Duration> &timeout_time)
182 template <
typename Rep,
typename Period>
184 try_lock_for(
const std::chrono::duration<Rep, Period> &timeout_duration)
199 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
200 int ret = pmemobj_mutex_unlock(pop, &this->
plock);
203 "Failed to unlock a mutex.")
204 .with_pmemobj_errormsg();
232 template <
typename Clock,
typename Duration>
236 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
239 const typename Clock::time_point their_now = Clock::now();
240 const clock_type::time_point my_now = clock_type::now();
241 const auto delta = abs_time - their_now;
242 const auto my_abs = my_now + delta;
244 struct timespec ts = detail::timepoint_to_timespec(my_abs);
246 auto ret = pmemobj_mutex_timedlock(pop, &this->
plock, &ts);
250 else if (ret == ETIMEDOUT)
254 "Failed to lock a mutex");
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:258
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:66
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:101
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: timed_mutex.hpp:125
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:213
bool try_lock_for(const std::chrono::duration< Rep, Period > &timeout_duration)
Makes the current thread block until the lock is acquired or a specified amount of time passes.
Definition: timed_mutex.hpp:184
timed_mutex & operator=(const timed_mutex &)=delete
Deleted assignment operator.
Commonly used conversions.
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:197
bool try_lock_until(const std::chrono::time_point< Clock, Duration > &timeout_time)
Makes the current thread block until the lock is acquired or a specific time is reached.
Definition: timed_mutex.hpp:159
Custom lock error class.
Definition: pexceptions.hpp:109
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:61
bool timedlock_impl(const std::chrono::time_point< Clock, Duration > &abs_time)
Internal implementation of the timed lock call.
Definition: timed_mutex.hpp:234
~timed_mutex()=default
Defaulted destructor.
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: allocation_flag.hpp:43
timed_mutex()
Default constructor.
Definition: timed_mutex.hpp:73