timed_mutex.hpp
Go to the documentation of this file.
/*
* Copyright 2016-2017, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#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);
/* convert to my clock */
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;
};
} /* namespace obj */
} /* namespace pmem */
#endif /* PMEMOBJ_TIMED_MUTEX_HPP */
pmem::obj::timed_mutex::plock
PMEMmutex plock
A POSIX style PMEM-resident timed_mutex.
Definition: timed_mutex.hpp:252
pmem::obj::timed_mutex::native_handle_type
PMEMmutex * native_handle_type
Implementation defined handle to the native type.
Definition: timed_mutex.hpp:66
pmem::obj::timed_mutex::lock
void lock()
Locks the mutex, blocks if already locked.
Definition: timed_mutex.hpp:101
pmem::obj::timed_mutex::try_lock
bool try_lock()
Tries to lock the mutex, returns regardless if the lock succeeds.
Definition: timed_mutex.hpp:124
pmem::obj::timed_mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: timed_mutex.hpp:207
pmem::obj::timed_mutex::try_lock_for
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:182
pmem::obj::timed_mutex::operator=
timed_mutex & operator=(const timed_mutex &)=delete
Deleted assignment operator.
conversions.hpp
Commonly used conversions.
pmem::obj::timed_mutex::unlock
void unlock()
Unlocks a previously locked mutex.
Definition: timed_mutex.hpp:195
pmem::obj::timed_mutex::try_lock_until
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:157
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:74
pmem::obj::timed_mutex
Persistent memory resident timed_mutex implementation.
Definition: timed_mutex.hpp:61
pmem::obj::timed_mutex::timedlock_impl
bool timedlock_impl(const std::chrono::time_point< Clock, Duration > &abs_time)
Internal implementation of the timed lock call.
Definition: timed_mutex.hpp:228
pmem::obj::timed_mutex::~timed_mutex
~timed_mutex()=default
Defaulted destructor.
pmem
Definition: allocator.hpp:48
pmem::obj::timed_mutex::timed_mutex
timed_mutex()
Default constructor.
Definition: timed_mutex.hpp:73