Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d5b9244fb613b0e9b67f8c9058558ebbaa85d6eb
[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 <mutex>
10 #include <utility> // std::swap
11
12 #include <simgrid/simix.h>
13 #include <simgrid/s4u/mutex.hpp>
14
15 namespace simgrid {
16 namespace s4u {
17
18 class Mutex;
19
20 XBT_PUBLIC_CLASS ConditionVariable {
21   
22 public:
23   ConditionVariable();
24
25   ConditionVariable(ConditionVariable* cond) : cond_(SIMIX_cond_ref(cond->cond_)) {}
26   ~ConditionVariable();
27
28   // Copy+move (with the copy-and-swap idiom):
29   ConditionVariable(ConditionVariable const& cond) : cond_(SIMIX_cond_ref(cond.cond_)) {}
30   friend void swap(ConditionVariable& first, ConditionVariable& second)
31   {
32     using std::swap;
33     swap(first.cond_, second.cond_);
34   }
35   ConditionVariable& operator=(ConditionVariable cond)
36   {
37     swap(*this, cond);
38     return *this;
39   }
40   ConditionVariable(ConditionVariable&& cond) : cond_(nullptr)
41   {
42     swap(*this, cond);
43   }
44
45   bool valid() const
46   {
47     return cond_ != nullptr;
48   }
49   
50   /**
51   * Wait functions
52   */
53   void wait(std::unique_lock<Mutex>& lock);
54   // TODO, return std::cv_status
55   void wait_for(std::unique_lock<Mutex>& lock, double duration);
56   // TODO, wait_until
57
58   /** Variant which takes a predice */
59   template<class P>
60   void wait(std::unique_lock<Mutex>& lock, P pred)
61   {
62     while (!pred())
63       wait(lock);
64   }
65   // TODO, return std::cv_status
66   template<class P>
67   void wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
68   {
69     while (!pred())
70       wait(lock, duration);
71   }
72   // TODO,wait_until
73
74   /**
75   * Notify functions
76   */
77   void notify();
78   void notify_all();
79
80 private:
81   smx_cond_t cond_;
82
83 };
84 }} // namespace simgrid::s4u
85
86 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */