38 #ifndef PMEMOBJ_TIMED_MUTEX_HPP 39 #define PMEMOBJ_TIMED_MUTEX_HPP 44 #include "libpmemobj/thread.h" 62 typedef std::chrono::system_clock clock_type;
92 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
93 if (
int ret = pmemobj_mutex_lock(pop, &this->
plock))
95 "Failed to lock a mutex.");
115 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
116 int ret = pmemobj_mutex_trylock(pop, &this->
plock);
120 else if (ret == EBUSY)
124 "Failed to lock a mutex.");
144 template <
typename Clock,
typename Duration>
147 const std::chrono::time_point<Clock, Duration> &timeout_time)
169 template <
typename Rep,
typename Period>
171 try_lock_for(
const std::chrono::duration<Rep, Period> &timeout_duration)
190 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
191 if (
int ret = pmemobj_mutex_unlock(pop, &this->
plock))
193 "Failed to unlock a mutex.");
221 template <
typename Clock,
typename Duration>
225 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
228 const typename Clock::time_point their_now = Clock::now();
229 const clock_type::time_point my_now = clock_type::now();
230 const auto delta = abs_time - their_now;
231 const auto my_abs = my_now + delta;
233 struct timespec ts = detail::timepoint_to_timespec(my_abs);
235 auto ret = pmemobj_mutex_timedlock(pop, &this->
plock, &ts);
239 else if (ret == ETIMEDOUT)
243 "Failed to lock a mutex");
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:90
timed_mutex & operator=(const timed_mutex &)=delete
Deleted assignment operator.
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:146
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:171
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:188
Commonly used conversions.
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:66
timed_mutex() noexcept=default
Defaulted constructor.
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:247
Custom lock error class.
Definition: pexceptions.hpp:74
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: timed_mutex.hpp:113
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:202
Definition: condition_variable.hpp:48
bool timedlock_impl(const std::chrono::time_point< Clock, Duration > &abs_time)
Internal implementation of the timed lock call.
Definition: timed_mutex.hpp:223
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:61