Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9c9c58ac657f25f2a9d57a1a46116b8a3e9a94ed
[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   void wait_for(std::unique_lock<Mutex>& lock, double time);
55
56   /**
57   * Notify functions
58   */
59   void notify();
60   void notify_all();
61
62 private:
63   smx_cond_t cond_;
64
65 };
66 }} // namespace simgrid::s4u
67
68 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */