condition_variable.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_CONDVARIABLE_HPP
#define PMEMOBJ_CONDVARIABLE_HPP
#include <chrono>
#include <condition_variable>
#include "libpmemobj++/detail/conversions.hpp"
#include "libpmemobj++/mutex.hpp"
#include "libpmemobj/thread.h"
namespace pmem
{
namespace obj
{
class condition_variable {
typedef std::chrono::system_clock clock_type;
public:
typedef PMEMcond *native_handle_type;
condition_variable()
{
PMEMobjpool *pop;
if ((pop = pmemobj_pool_by_ptr(&pcond)) == nullptr)
throw lock_error(
1, std::generic_category(),
"Persistent condition variable not from"
" persistent memory.");
pmemobj_cond_zero(pop, &pcond);
}
~condition_variable() = default;
void
notify_one()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
if (int ret = pmemobj_cond_signal(pop, &this->pcond))
throw lock_error(ret, std::system_category(),
"Error notifying one on "
"a condition variable.");
}
void
notify_all()
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
if (int ret = pmemobj_cond_broadcast(pop, &this->pcond))
throw lock_error(ret, std::system_category(),
"Error notifying all on "
"a condition variable.");
}
void
wait(mutex &lock)
{
this->wait_impl(lock);
}
template <typename Lock>
void
wait(Lock &lock)
{
this->wait_impl(*lock.mutex());
}
template <typename Predicate>
void
wait(mutex &lock, Predicate pred)
{
this->wait_impl(lock, std::move(pred));
}
template <typename Lock, typename Predicate>
void
wait(Lock &lock, Predicate pred)
{
this->wait_impl(*lock.mutex(), std::move(pred));
}
template <typename Clock, typename Duration>
std::cv_status
wait_until(mutex &lock,
const std::chrono::time_point<Clock, Duration> &timeout)
{
return this->wait_until_impl(lock, timeout);
}
template <typename Lock, typename Clock, typename Duration>
std::cv_status
wait_until(Lock &lock,
const std::chrono::time_point<Clock, Duration> &timeout)
{
return this->wait_until_impl(*lock.mutex(), timeout);
}
template <typename Clock, typename Duration, typename Predicate>
bool
wait_until(mutex &lock,
const std::chrono::time_point<Clock, Duration> &timeout,
Predicate pred)
{
return this->wait_until_impl(lock, timeout, std::move(pred));
}
template <typename Lock, typename Clock, typename Duration,
typename Predicate>
bool
wait_until(Lock &lock,
const std::chrono::time_point<Clock, Duration> &timeout,
Predicate pred)
{
return this->wait_until_impl(*lock.mutex(), timeout,
std::move(pred));
}
template <typename Lock, typename Rep, typename Period>
std::cv_status
wait_for(Lock &lock, const std::chrono::duration<Rep, Period> &rel_time)
{
return this->wait_until_impl(*lock.mutex(),
clock_type::now() + rel_time);
}
template <typename Lock, typename Rep, typename Period,
typename Predicate>
bool
wait_for(Lock &lock, const std::chrono::duration<Rep, Period> &rel_time,
Predicate pred)
{
return this->wait_until_impl(*lock.mutex(),
clock_type::now() + rel_time,
std::move(pred));
}
template <typename Rep, typename Period>
std::cv_status
wait_for(mutex &lock,
const std::chrono::duration<Rep, Period> &rel_time)
{
return this->wait_until_impl(lock,
clock_type::now() + rel_time);
}
template <typename Rep, typename Period, typename Predicate>
bool
wait_for(mutex &lock,
const std::chrono::duration<Rep, Period> &rel_time,
Predicate pred)
{
return this->wait_until_impl(lock, clock_type::now() + rel_time,
std::move(pred));
}
native_handle_type
native_handle() noexcept
{
return &this->pcond;
}
condition_variable &operator=(const condition_variable &) = delete;
condition_variable(const condition_variable &) = delete;
private:
void
wait_impl(mutex &lock)
{
PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
if (int ret = pmemobj_cond_wait(pop, &this->pcond,
lock.native_handle()))
throw lock_error(ret, std::system_category(),
"Error waiting on a condition "
"variable.");
}
template <typename Predicate>
void
wait_impl(mutex &lock, Predicate pred)
{
while (!pred())
this->wait(lock);
}
template <typename Clock, typename Duration>
std::cv_status
wait_until_impl(
mutex &lock,
const std::chrono::time_point<Clock, Duration> &abs_timeout)
{
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_timeout - their_now;
const auto my_rel = my_now + delta;
struct timespec ts = detail::timepoint_to_timespec(my_rel);
auto ret = pmemobj_cond_timedwait(pop, &this->pcond,
lock.native_handle(), &ts);
if (ret == 0)
return std::cv_status::no_timeout;
else if (ret == ETIMEDOUT)
return std::cv_status::timeout;
else
throw lock_error(ret, std::system_category(),
"Error waiting on a condition "
"variable.");
}
template <typename Clock, typename Duration, typename Predicate>
bool
wait_until_impl(
mutex &lock,
const std::chrono::time_point<Clock, Duration> &abs_timeout,
Predicate pred)
{
while (!pred())
if (this->wait_until_impl(lock, abs_timeout) ==
std::cv_status::timeout)
return pred();
return true;
}
PMEMcond pcond;
};
} /* namespace obj */
} /* namespace pmem */
#endif /* PMEMOBJ_CONDVARIABLE_HPP */
pmem::obj::condition_variable::wait_for
bool wait_for(mutex &lock, const std::chrono::duration< Rep, Period > &rel_time, Predicate pred)
Makes the current thread block until the condition variable is notified or the specified amount of ti...
Definition: condition_variable.hpp:470
pmem::obj::condition_variable::pcond
PMEMcond pcond
A POSIX style PMEM-resident condition variable.
Definition: condition_variable.hpp:575
pmem::obj::condition_variable::wait_for
bool wait_for(Lock &lock, const std::chrono::duration< Rep, Period > &rel_time, Predicate pred)
Makes the current thread block until the condition variable is notified or the specified amount of ti...
Definition: condition_variable.hpp:408
pmem::obj::condition_variable::~condition_variable
~condition_variable()=default
Defaulted destructor.
pmem::obj::condition_variable::wait_until
std::cv_status wait_until(mutex &lock, const std::chrono::time_point< Clock, Duration > &timeout)
Makes the current thread block until the condition variable is notified, a specific time is reached o...
Definition: condition_variable.hpp:247
mutex.hpp
Pmem-resident mutex.
pmem::obj::condition_variable::wait_until
bool wait_until(Lock &lock, const std::chrono::time_point< Clock, Duration > &timeout, Predicate pred)
Makes the current thread block until the condition variable is notified or a specific time is reached...
Definition: condition_variable.hpp:342
pmem::obj::condition_variable::wait
void wait(Lock &lock, Predicate pred)
Makes the current thread block until the condition variable is notified.
Definition: condition_variable.hpp:219
pmem::obj::condition_variable::operator=
condition_variable & operator=(const condition_variable &)=delete
Deleted assignment operator.
pmem::obj::condition_variable::wait
void wait(Lock &lock)
Makes the current thread block until the condition variable is notified or it is woken up by some oth...
Definition: condition_variable.hpp:165
pmem::obj::condition_variable::wait
void wait(mutex &lock)
Makes the current thread block until the condition variable is notified or it is woken up by some oth...
Definition: condition_variable.hpp:141
pmem::obj::condition_variable::notify_one
void notify_one()
Notify and unblock one thread waiting on *this condition.
Definition: condition_variable.hpp:101
pmem::obj::condition_variable
Persistent memory resident condition variable.
Definition: condition_variable.hpp:62
pmem::obj::condition_variable::wait_impl
void wait_impl(mutex &lock)
Internal implementation of the wait call.
Definition: condition_variable.hpp:504
pmem::obj::condition_variable::wait_until_impl
std::cv_status wait_until_impl(mutex &lock, const std::chrono::time_point< Clock, Duration > &abs_timeout)
Internal implementation of the wait_until call.
Definition: condition_variable.hpp:530
pmem::obj::mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:155
pmem::obj::condition_variable::wait_impl
void wait_impl(mutex &lock, Predicate pred)
Internal implementation of the wait call.
Definition: condition_variable.hpp:519
conversions.hpp
Commonly used conversions.
pmem::obj::mutex
Persistent memory resident mutex implementation.
Definition: mutex.hpp:60
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:74
pmem::obj::condition_variable::wait_for
std::cv_status wait_for(Lock &lock, const std::chrono::duration< Rep, Period > &rel_time)
Makes the current thread block until the condition variable is notified, the specified amount of time...
Definition: condition_variable.hpp:375
pmem::obj::condition_variable::wait_until
std::cv_status wait_until(Lock &lock, const std::chrono::time_point< Clock, Duration > &timeout)
Makes the current thread block until the condition variable is notified, a specific time is reached o...
Definition: condition_variable.hpp:278
pmem::obj::condition_variable::notify_all
void notify_all()
Notify and unblock all threads waiting on *this condition.
Definition: condition_variable.hpp:116
pmem::obj::condition_variable::wait_until_impl
bool wait_until_impl(mutex &lock, const std::chrono::time_point< Clock, Duration > &abs_timeout, Predicate pred)
Internal implementation of the wait_until call.
Definition: condition_variable.hpp:562
pmem::obj::condition_variable::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: condition_variable.hpp:484
pmem
Definition: allocator.hpp:48
pmem::obj::condition_variable::native_handle_type
PMEMcond * native_handle_type
The handle typedef to the underlying basic type.
Definition: condition_variable.hpp:67
pmem::obj::condition_variable::wait
void wait(mutex &lock, Predicate pred)
Makes the current thread block until the condition variable is notified.
Definition: condition_variable.hpp:191
pmem::obj::condition_variable::wait_until
bool wait_until(mutex &lock, const std::chrono::time_point< Clock, Duration > &timeout, Predicate pred)
Makes the current thread block until the condition variable is notified or a specific time is reached...
Definition: condition_variable.hpp:308
pmem::obj::condition_variable::condition_variable
condition_variable()
Default constructor.
Definition: condition_variable.hpp:75
pmem::obj::condition_variable::wait_for
std::cv_status wait_for(mutex &lock, const std::chrono::duration< Rep, Period > &rel_time)
Makes the current thread block until the condition variable is notified, the specified amount of time...
Definition: condition_variable.hpp:439