9 #ifndef LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP
10 #define LIBPMEMOBJ_CPP_TIMED_MUTEX_HPP
15 #include <libpmemobj/thread.h>
35 typedef std::chrono::system_clock clock_type;
49 if ((pop = pmemobj_pool_by_ptr(&plock)) ==
nullptr)
51 1, std::generic_category(),
52 "Persistent mutex not from persistent memory.");
54 pmemobj_mutex_zero(pop, &plock);
76 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
77 if (
int ret = pmemobj_mutex_lock(pop, &this->plock))
78 throw detail::exception_with_errormsg<lock_error>(
79 ret, std::system_category(),
80 "Failed to lock a mutex.");
100 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
101 int ret = pmemobj_mutex_trylock(pop, &this->plock);
105 else if (ret == EBUSY)
108 throw detail::exception_with_errormsg<lock_error>(
109 ret, std::system_category(),
110 "Failed to lock a mutex.");
130 template <
typename Clock,
typename Duration>
133 const std::chrono::time_point<Clock, Duration> &timeout_time)
135 return timedlock_impl(timeout_time);
155 template <
typename Rep,
typename Period>
157 try_lock_for(
const std::chrono::duration<Rep, Period> &timeout_duration)
159 return timedlock_impl(clock_type::now() + timeout_duration);
172 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
173 int ret = pmemobj_mutex_unlock(pop, &this->plock);
175 throw detail::exception_with_errormsg<lock_error>(
176 ret, std::system_category(),
177 "Failed to unlock a mutex.");
205 template <
typename Clock,
typename Duration>
207 timedlock_impl(
const std::chrono::time_point<Clock, Duration> &abs_time)
209 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
212 const typename Clock::time_point their_now = Clock::now();
213 const clock_type::time_point my_now = clock_type::now();
214 const auto delta = abs_time - their_now;
215 const auto my_abs = my_now + delta;
219 auto ret = pmemobj_mutex_timedlock(pop, &this->plock, &ts);
223 else if (ret == ETIMEDOUT)
226 throw detail::exception_with_errormsg<lock_error>(
227 ret, std::system_category(),
228 "Failed to lock a mutex");
Custom lock error class.
Definition: pexceptions.hpp:121
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:34
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:74
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:186
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:170
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:132
timed_mutex()
Default constructor.
Definition: timed_mutex.hpp:46
timed_mutex(const timed_mutex &)=delete
Deleted copy constructor.
~timed_mutex()=default
Defaulted destructor.
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: timed_mutex.hpp:98
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:39
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:157
timed_mutex & operator=(const timed_mutex &)=delete
Deleted assignment operator.
Commonly used conversions.
timespec timepoint_to_timespec(const std::chrono::time_point< Clock, Duration > &timepoint)
Convert std::chrono::time_point to posix timespec.
Definition: conversions.hpp:30
Persistent memory namespace.
Definition: allocation_flag.hpp:15