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");