Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] add condition variables
[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 <simgrid/s4u/mutex.hpp>
10
11
12 namespace simgrid {
13 namespace s4u {
14
15 class Mutex;
16
17 XBT_PUBLIC_CLASS ConditionVariable {
18   
19 public:
20   ConditionVariable();
21
22   ConditionVariable(ConditionVariable* cond) : cond_(SIMIX_cond_ref(cond->cond_)) {}
23   ~ConditionVariable();
24
25   // Copy+move (with the copy-and-swap idiom):
26   ConditionVariable(ConditionVariable const& cond) : cond_(SIMIX_cond_ref(cond.cond_)) {}
27   friend void swap(ConditionVariable& first, ConditionVariable& second)
28   {
29     using std::swap;
30     swap(first.cond_, second.cond_);
31   }
32   ConditionVariable& operator=(ConditionVariable cond)
33   {
34     swap(*this, cond);
35     return *this;
36   }
37   ConditionVariable(ConditionVariable&& cond) : cond_(nullptr)
38   {
39     swap(*this, cond);
40   }
41
42   bool valid() const
43   {
44     return cond_ != nullptr;
45   }
46   
47   /**
48   * Wait functions
49   */
50   void wait(Mutex *mutex);
51   void wait_for(Mutex *mutex, double time);
52
53   /**
54   * Notify functions
55   */
56   void notify();
57   void notify_all();
58
59 private:
60   smx_cond_t cond_;
61
62 };
63 }} // namespace simgrid::s4u
64
65 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */