Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Remove broken ConditionVariable::wait_for(lock, duration, pred)
[simgrid.git] / include / simgrid / s4u / conditionVariable.hpp
1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_S4U_COND_VARIABLE_HPP
7 #define SIMGRID_S4U_COND_VARIABLE_HPP
8
9 #include <condition_variable>
10 #include <mutex>
11 #include <utility> // std::swap
12
13 #include <simgrid/simix.h>
14 #include <simgrid/s4u/mutex.hpp>
15
16 namespace simgrid {
17 namespace s4u {
18
19 class Mutex;
20
21 XBT_PUBLIC_CLASS ConditionVariable {
22   
23 public:
24   ConditionVariable();
25
26   ConditionVariable(ConditionVariable* cond) : cond_(SIMIX_cond_ref(cond->cond_)) {}
27   ~ConditionVariable();
28
29   // Copy+move (with the copy-and-swap idiom):
30   ConditionVariable(ConditionVariable const& cond) : cond_(SIMIX_cond_ref(cond.cond_)) {}
31   friend void swap(ConditionVariable& first, ConditionVariable& second)
32   {
33     using std::swap;
34     swap(first.cond_, second.cond_);
35   }
36   ConditionVariable& operator=(ConditionVariable cond)
37   {
38     swap(*this, cond);
39     return *this;
40   }
41   ConditionVariable(ConditionVariable&& cond) : cond_(nullptr)
42   {
43     swap(*this, cond);
44   }
45
46   bool valid() const
47   {
48     return cond_ != nullptr;
49   }
50   
51   /**
52   * Wait functions
53   */
54   void wait(std::unique_lock<Mutex>& lock);
55   // TODO, return std::cv_status
56   std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
57   // TODO, wait_until
58
59   /** Variant which takes a predice */
60   template<class P>
61   void wait(std::unique_lock<Mutex>& lock, P pred)
62   {
63     while (!pred())
64       wait(lock);
65   }
66
67   // TODO, return std::cv_status
68   // TODO,wait_until
69
70   /**
71   * Notify functions
72   */
73   void notify();
74   void notify_all();
75
76 private:
77   smx_cond_t cond_;
78
79 };
80 }} // namespace simgrid::s4u
81
82 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */