Logo AND Algorithmique Numérique Distribuée

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