PMDK C++ bindings  1.7
This is the C++ bindings documentation for PMDK's libpmemobj.
condition_variable.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2018, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #ifndef LIBPMEMOBJ_CPP_CONDVARIABLE_HPP
39 #define LIBPMEMOBJ_CPP_CONDVARIABLE_HPP
40 
41 #include <chrono>
42 #include <condition_variable>
43 
45 #include <libpmemobj++/mutex.hpp>
46 #include <libpmemobj/thread.h>
47 
48 namespace pmem
49 {
50 
51 namespace obj
52 {
53 
63  typedef std::chrono::system_clock clock_type;
64 
65 public:
67  typedef PMEMcond *native_handle_type;
68 
76  {
77  PMEMobjpool *pop;
78  if ((pop = pmemobj_pool_by_ptr(&pcond)) == nullptr)
79  throw lock_error(
80  1, std::generic_category(),
81  "Persistent condition variable not from persistent memory.");
82 
83  pmemobj_cond_zero(pop, &pcond);
84  }
85 
89  ~condition_variable() = default;
90 
99  void
101  {
102  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
103  if (int ret = pmemobj_cond_signal(pop, &this->pcond))
104  throw lock_error(
105  ret, std::system_category(),
106  "Error notifying one on a condition variable.");
107  }
108 
114  void
116  {
117  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
118  if (int ret = pmemobj_cond_broadcast(pop, &this->pcond))
119  throw lock_error(
120  ret, std::system_category(),
121  "Error notifying all on a condition variable.");
122  }
123 
139  void
140  wait(mutex &lock)
141  {
142  this->wait_impl(lock);
143  }
144 
162  template <typename Lock>
163  void
164  wait(Lock &lock)
165  {
166  this->wait_impl(*lock.mutex());
167  }
168 
188  template <typename Predicate>
189  void
190  wait(mutex &lock, Predicate pred)
191  {
192  this->wait_impl(lock, std::move(pred));
193  }
194 
216  template <typename Lock, typename Predicate>
217  void
218  wait(Lock &lock, Predicate pred)
219  {
220  this->wait_impl(*lock.mutex(), std::move(pred));
221  }
222 
244  template <typename Clock, typename Duration>
245  std::cv_status
247  const std::chrono::time_point<Clock, Duration> &timeout)
248  {
249  return this->wait_until_impl(lock, timeout);
250  }
251 
275  template <typename Lock, typename Clock, typename Duration>
276  std::cv_status
277  wait_until(Lock &lock,
278  const std::chrono::time_point<Clock, Duration> &timeout)
279  {
280  return this->wait_until_impl(*lock.mutex(), timeout);
281  }
282 
305  template <typename Clock, typename Duration, typename Predicate>
306  bool
308  const std::chrono::time_point<Clock, Duration> &timeout,
309  Predicate pred)
310  {
311  return this->wait_until_impl(lock, timeout, std::move(pred));
312  }
313 
338  template <typename Lock, typename Clock, typename Duration,
339  typename Predicate>
340  bool
341  wait_until(Lock &lock,
342  const std::chrono::time_point<Clock, Duration> &timeout,
343  Predicate pred)
344  {
345  return this->wait_until_impl(*lock.mutex(), timeout,
346  std::move(pred));
347  }
348 
372  template <typename Lock, typename Rep, typename Period>
373  std::cv_status
374  wait_for(Lock &lock, const std::chrono::duration<Rep, Period> &rel_time)
375  {
376  return this->wait_until_impl(*lock.mutex(),
377  clock_type::now() + rel_time);
378  }
379 
404  template <typename Lock, typename Rep, typename Period,
405  typename Predicate>
406  bool
407  wait_for(Lock &lock, const std::chrono::duration<Rep, Period> &rel_time,
408  Predicate pred)
409  {
410  return this->wait_until_impl(*lock.mutex(),
411  clock_type::now() + rel_time,
412  std::move(pred));
413  }
414 
436  template <typename Rep, typename Period>
437  std::cv_status
439  const std::chrono::duration<Rep, Period> &rel_time)
440  {
441  return this->wait_until_impl(lock,
442  clock_type::now() + rel_time);
443  }
444 
467  template <typename Rep, typename Period, typename Predicate>
468  bool
470  const std::chrono::duration<Rep, Period> &rel_time,
471  Predicate pred)
472  {
473  return this->wait_until_impl(lock, clock_type::now() + rel_time,
474  std::move(pred));
475  }
476 
483  native_handle() noexcept
484  {
485  return &this->pcond;
486  }
487 
492 
497 
498 private:
502  void
504  {
505  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
506  if (int ret = pmemobj_cond_wait(pop, &this->pcond,
507  lock.native_handle()))
508  throw lock_error(
509  ret, std::system_category(),
510  "Error waiting on a condition variable.");
511  }
512 
516  template <typename Predicate>
517  void
518  wait_impl(mutex &lock, Predicate pred)
519  {
520  while (!pred())
521  this->wait(lock);
522  }
523 
527  template <typename Clock, typename Duration>
528  std::cv_status
530  mutex &lock,
531  const std::chrono::time_point<Clock, Duration> &abs_timeout)
532  {
533  PMEMobjpool *pop = pmemobj_pool_by_ptr(this);
534 
535  /* convert to my clock */
536  const typename Clock::time_point their_now = Clock::now();
537  const clock_type::time_point my_now = clock_type::now();
538  const auto delta = abs_timeout - their_now;
539  const auto my_rel = my_now + delta;
540 
541  struct timespec ts = detail::timepoint_to_timespec(my_rel);
542 
543  auto ret = pmemobj_cond_timedwait(pop, &this->pcond,
544  lock.native_handle(), &ts);
545 
546  if (ret == 0)
547  return std::cv_status::no_timeout;
548  else if (ret == ETIMEDOUT)
549  return std::cv_status::timeout;
550  else
551  throw lock_error(
552  ret, std::system_category(),
553  "Error waiting on a condition variable.");
554  }
555 
559  template <typename Clock, typename Duration, typename Predicate>
560  bool
562  mutex &lock,
563  const std::chrono::time_point<Clock, Duration> &abs_timeout,
564  Predicate pred)
565  {
566  while (!pred())
567  if (this->wait_until_impl(lock, abs_timeout) ==
568  std::cv_status::timeout)
569  return pred();
570  return true;
571  }
572 
574  PMEMcond pcond;
575 };
576 
577 } /* namespace obj */
578 
579 } /* namespace pmem */
580 
581 #endif /* LIBPMEMOBJ_CPP_CONDVARIABLE_HPP */
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:246
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:438
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:374
pmem::obj::mutex
Persistent memory resident mutex implementation.
Definition: mutex.hpp:60
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:469
pmem::obj::condition_variable::pcond
PMEMcond pcond
A POSIX style PMEM-resident condition variable.
Definition: condition_variable.hpp:574
pmem::obj::mutex::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: mutex.hpp:158
conversions.hpp
Commonly used conversions.
pmem::obj::condition_variable::condition_variable
condition_variable()
Default constructor.
Definition: condition_variable.hpp:75
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:529
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:140
pmem::obj::condition_variable::notify_one
void notify_one()
Notify and unblock one thread waiting on *this condition.
Definition: condition_variable.hpp:100
pmem::obj::condition_variable::operator=
condition_variable & operator=(const condition_variable &)=delete
Deleted assignment operator.
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:190
pmem::obj::condition_variable::wait_impl
void wait_impl(mutex &lock, Predicate pred)
Internal implementation of the wait call.
Definition: condition_variable.hpp:518
pmem::lock_error
Custom lock error class.
Definition: pexceptions.hpp:74
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:218
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:277
pmem::obj::condition_variable::native_handle
native_handle_type native_handle() noexcept
Access a native handle to this condition variable.
Definition: condition_variable.hpp:483
pmem::obj::condition_variable::condition_variable
condition_variable(const condition_variable &)=delete
Deleted copy constructor.
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_impl
void wait_impl(mutex &lock)
Internal implementation of the wait call.
Definition: condition_variable.hpp:503
pmem::obj::condition_variable
Persistent memory resident condition variable.
Definition: condition_variable.hpp:62
pmem::obj::condition_variable::~condition_variable
~condition_variable()=default
Defaulted destructor.
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:561
pmem::obj::condition_variable::notify_all
void notify_all()
Notify and unblock all threads waiting on *this condition.
Definition: condition_variable.hpp:115
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:307
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:407
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:341
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:164