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.");
126 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
127 int ret = pmemobj_mutex_trylock(pop, &this->
plock);
131 else if (ret == EBUSY)
135 "Failed to lock a mutex.");
155 template <
typename Clock,
typename Duration>
158 const std::chrono::time_point<Clock, Duration> &timeout_time)
180 template <
typename Rep,
typename Period>
182 try_lock_for(
const std::chrono::duration<Rep, Period> &timeout_duration)
197 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
198 int ret = pmemobj_mutex_unlock(pop, &this->
plock);
201 "Failed to unlock a mutex.");
229 template <
typename Clock,
typename Duration>
233 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
236 const typename Clock::time_point their_now = Clock::now();
237 const clock_type::time_point my_now = clock_type::now();
238 const auto delta = abs_time - their_now;
239 const auto my_abs = my_now + delta;
241 struct timespec ts = detail::timepoint_to_timespec(my_abs);
243 auto ret = pmemobj_mutex_timedlock(pop, &this->
plock, &ts);
247 else if (ret == ETIMEDOUT)
251 "Failed to lock a mutex");