Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove "using namespace"
[simgrid.git] / src / s4u / s4u_conditionVariable.cpp
1 #include <exception>
2 #include <mutex>
3
4 #include <xbt/log.hpp>
5
6 #include "simgrid/s4u/conditionVariable.hpp"
7 #include "simgrid/simix.h"
8
9 namespace simgrid {
10 namespace s4u {
11
12 ConditionVariable::ConditionVariable()  : cond_(simcall_cond_init()){
13     
14 }
15
16 ConditionVariable::~ConditionVariable() {
17   SIMIX_cond_unref(cond_);
18 }
19
20 /**
21  * Wait functions
22  */
23 void ConditionVariable::wait(std::unique_lock<Mutex>& lock) {
24   simcall_cond_wait(cond_, lock.mutex()->mutex_);
25 }
26
27 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout) {
28   try {
29     simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
30     return std::cv_status::no_timeout;
31   }
32   catch (xbt_ex& e) {
33
34     // If the exception was a timeout, we have to take the lock again:
35     if (e.category == timeout_error) {
36       try {
37         lock.mutex()->lock();
38         return std::cv_status::timeout;
39       }
40       catch (...) {
41         std::terminate();
42       }
43     }
44
45     // Another exception: should we reaquire the lock?
46     std::terminate();
47   }
48   catch (...) {
49     std::terminate();
50   }
51 }
52
53 std::cv_status ConditionVariable::wait_until(std::unique_lock<Mutex>& lock, double timeout_time)
54 {
55   double now = SIMIX_get_clock();
56   double timeout;
57   if (timeout_time < now)
58     timeout = 0.0;
59   else
60     timeout = timeout_time - now;
61   return this->wait_for(lock, timeout);
62 }
63   
64 /**
65  * Notify functions
66  */
67 void ConditionVariable::notify_one() {
68    simcall_cond_signal(cond_);
69 }
70  
71 void ConditionVariable::notify_all() {
72   simcall_cond_broadcast(cond_);
73 }
74
75 }
76 }