#ifndef PMEMOBJ_TIMED_MUTEX_HPP
#define PMEMOBJ_TIMED_MUTEX_HPP
#include <chrono>
#include "libpmemobj++/detail/conversions.hpp"
#include "libpmemobj/thread.h"
namespace pmem
{
namespace obj
{
class timed_mutex {
typedef std::chrono::system_clock clock_type;
public:
typedef PMEMmutex *native_handle_type;
timed_mutex()
{
PMEMobjpool *pop;
if ((pop = pmemobj_pool_by_ptr(&plock)) == nullptr)
throw lock_error(1, std::generic_category(),
"Persistent mutex not from persistent"
"memory.");
pmemobj_mutex_zero(pop, &plock);
}
~timed_mutex() = default;
void
lock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
if (int ret = pmemobj_mutex_lock(pop, &this->plock))
throw lock_error(ret, std::system_category(),
"Failed to lock a mutex.");
}
bool
try_lock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
int ret = pmemobj_mutex_trylock(pop, &this->plock);
if (ret == 0)
return true;
else if (ret == EBUSY)
return false;
else
throw lock_error(ret, std::system_category(),
"Failed to lock a mutex.");
}
template <typename Clock, typename Duration>
bool
try_lock_until(
const std::chrono::time_point<Clock, Duration> &timeout_time)
{
return timedlock_impl(timeout_time);
}
template <typename Rep, typename Period>
bool
try_lock_for(const std::chrono::duration<Rep, Period> &timeout_duration)
{
return timedlock_impl(clock_type::now() + timeout_duration);
}
void
unlock()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
(void)pmemobj_mutex_unlock(pop, &this->plock);
}
native_handle_type
native_handle() noexcept
{
return &this->plock;
}
timed_mutex &operator=(const timed_mutex &) = delete;
timed_mutex(const timed_mutex &) = delete;
private:
template <typename Clock, typename Duration>
bool
timedlock_impl(const std::chrono::time_point<Clock, Duration> &abs_time)
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
const typename Clock::time_point their_now = Clock::now();
const clock_type::time_point my_now = clock_type::now();
const auto delta = abs_time - their_now;
const auto my_abs = my_now + delta;
struct timespec ts = detail::timepoint_to_timespec(my_abs);
auto ret = pmemobj_mutex_timedlock(pop, &this->plock, &ts);
if (ret == 0)
return true;
else if (ret == ETIMEDOUT)
return false;
else
throw lock_error(ret, std::system_category(),
"Failed to lock a mutex");
}
PMEMmutex plock;
};
}
}
#endif