9 #ifndef LIBPMEMOBJ_CPP_CONDVARIABLE_HPP
10 #define LIBPMEMOBJ_CPP_CONDVARIABLE_HPP
13 #include <condition_variable>
17 #include <libpmemobj/thread.h>
34 typedef std::chrono::system_clock clock_type;
49 if ((pop = pmemobj_pool_by_ptr(&
pcond)) ==
nullptr)
51 1, std::generic_category(),
52 "Persistent condition variable not from persistent memory.");
54 pmemobj_cond_zero(pop, &
pcond);
73 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
74 if (
int ret = pmemobj_cond_signal(pop, &this->
pcond))
76 ret, std::system_category(),
77 "Error notifying one on a condition variable.")
78 .with_pmemobj_errormsg();
89 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
90 if (
int ret = pmemobj_cond_broadcast(pop, &this->
pcond))
92 ret, std::system_category(),
93 "Error notifying all on a condition variable.")
94 .with_pmemobj_errormsg();
135 template <
typename Lock>
161 template <
typename Predicate>
189 template <
typename Lock,
typename Predicate>
191 wait(Lock &lock, Predicate pred)
193 this->
wait_impl(*lock.mutex(), std::move(pred));
217 template <
typename Clock,
typename Duration>
220 const std::chrono::time_point<Clock, Duration> &timeout)
248 template <
typename Lock,
typename Clock,
typename Duration>
251 const std::chrono::time_point<Clock, Duration> &timeout)
278 template <
typename Clock,
typename Duration,
typename Predicate>
281 const std::chrono::time_point<Clock, Duration> &timeout,
311 template <
typename Lock,
typename Clock,
typename Duration,
315 const std::chrono::time_point<Clock, Duration> &timeout,
345 template <
typename Lock,
typename Rep,
typename Period>
347 wait_for(Lock &lock,
const std::chrono::duration<Rep, Period> &rel_time)
350 clock_type::now() + rel_time);
377 template <
typename Lock,
typename Rep,
typename Period,
380 wait_for(Lock &lock,
const std::chrono::duration<Rep, Period> &rel_time,
384 clock_type::now() + rel_time,
409 template <
typename Rep,
typename Period>
412 const std::chrono::duration<Rep, Period> &rel_time)
415 clock_type::now() + rel_time);
440 template <
typename Rep,
typename Period,
typename Predicate>
443 const std::chrono::duration<Rep, Period> &rel_time,
478 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
479 if (
int ret = pmemobj_cond_wait(pop, &this->
pcond,
482 ret, std::system_category(),
483 "Error waiting on a condition variable.")
484 .with_pmemobj_errormsg();
490 template <
typename Predicate>
501 template <
typename Clock,
typename Duration>
505 const std::chrono::time_point<Clock, Duration> &abs_timeout)
507 PMEMobjpool *pop = pmemobj_pool_by_ptr(
this);
510 const typename Clock::time_point their_now = Clock::now();
511 const clock_type::time_point my_now = clock_type::now();
512 const auto delta = abs_timeout - their_now;
513 const auto my_rel = my_now + delta;
517 auto ret = pmemobj_cond_timedwait(pop, &this->
pcond,
521 return std::cv_status::no_timeout;
522 else if (ret == ETIMEDOUT)
523 return std::cv_status::timeout;
526 ret, std::system_category(),
527 "Error waiting on a condition variable.")
528 .with_pmemobj_errormsg();
534 template <
typename Clock,
typename Duration,
typename Predicate>
538 const std::chrono::time_point<Clock, Duration> &abs_timeout,
543 std::cv_status::timeout)
Custom lock error class.
Definition: pexceptions.hpp:100
Persistent memory resident condition variable.
Definition: condition_variable.hpp:33
~condition_variable()=default
Defaulted destructor.
void wait(Lock &lock, Predicate pred)
Makes the current thread block until the condition variable is notified.
Definition: condition_variable.hpp:191
condition_variable & operator=(const condition_variable &)=delete
Deleted assignment operator.
condition_variable(const condition_variable &)=delete
Deleted copy constructor.
PMEMcond pcond
A POSIX style PMEM-resident condition variable.
Definition: condition_variable.hpp:549
void wait_impl(mutex &lock, Predicate pred)
Internal implementation of the wait call.
Definition: condition_variable.hpp:492
condition_variable()
Default constructor.
Definition: condition_variable.hpp:46
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: condition_variable.hpp:456
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:536
PMEMcond * native_handle_type
The handle typedef to the underlying basic type.
Definition: condition_variable.hpp:38
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:380
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:347
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:442
void notify_all()
Notify and unblock all threads waiting on *this condition.
Definition: condition_variable.hpp:87
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:503
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:250
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:280
void wait(mutex &lock, Predicate pred)
Makes the current thread block until the condition variable is notified.
Definition: condition_variable.hpp:163
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:411
void notify_one()
Notify and unblock one thread waiting on *this condition.
Definition: condition_variable.hpp:71
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:137
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:113
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:314
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:219
void wait_impl(mutex &lock)
Internal implementation of the wait call.
Definition: condition_variable.hpp:476
Persistent memory resident mutex implementation.
Definition: mutex.hpp:31
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:132
Commonly used conversions.
timespec timepoint_to_timespec(const std::chrono::time_point< Clock, Duration > &timepoint)
Convert std::chrono::time_point to posix timespec.
Definition: conversions.hpp:30
Persistent memory namespace.
Definition: allocation_flag.hpp:15