Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug two memleaks
[simgrid.git] / src / s4u / s4u_conditionVariable.cpp
1 #include <exception>
2 #include <mutex>
3
4 #include <xbt/ex.hpp>
5 #include <xbt/log.hpp>
6
7 #include "src/simix/smx_synchro_private.h"
8 #include "simgrid/s4u/conditionVariable.hpp"
9 #include "simgrid/simix.h"
10
11 namespace simgrid {
12 namespace s4u {
13
14 ConditionVariablePtr ConditionVariable::createConditionVariable()
15 {
16   smx_cond_t cond = simcall_cond_init();
17   return ConditionVariablePtr(&cond->cond_, false);
18 }
19
20 /**
21  * Wait functions
22  */
23 void ConditionVariable::wait(MutexPtr lock)
24 {
25   simcall_cond_wait(cond_, lock->mutex_);
26 }
27
28 void ConditionVariable::wait(std::unique_lock<Mutex>& lock) {
29   simcall_cond_wait(cond_, lock.mutex()->mutex_);
30 }
31
32 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout) {
33   // The simcall uses -1 for "any timeout" but we don't want this:
34   if (timeout < 0)
35     timeout = 0.0;
36
37   try {
38     simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
39     return std::cv_status::no_timeout;
40   }
41   catch (xbt_ex& e) {
42
43     // If the exception was a timeout, we have to take the lock again:
44     if (e.category == timeout_error) {
45       try {
46         lock.mutex()->lock();
47         return std::cv_status::timeout;
48       }
49       catch (...) {
50         std::terminate();
51       }
52     }
53
54     // Another exception: should we reaquire the lock?
55     std::terminate();
56   }
57   catch (...) {
58     std::terminate();
59   }
60 }
61
62 std::cv_status ConditionVariable::wait_until(std::unique_lock<Mutex>& lock, double timeout_time)
63 {
64   double now = SIMIX_get_clock();
65   double timeout;
66   if (timeout_time < now)
67     timeout = 0.0;
68   else
69     timeout = timeout_time - now;
70   return this->wait_for(lock, timeout);
71 }
72   
73 /**
74  * Notify functions
75  */
76 void ConditionVariable::notify_one() {
77    simcall_cond_signal(cond_);
78 }
79  
80 void ConditionVariable::notify_all() {
81   simcall_cond_broadcast(cond_);
82 }
83
84 void intrusive_ptr_add_ref(ConditionVariable* cond)
85 {
86   intrusive_ptr_add_ref(cond->cond_);
87 }
88
89 void intrusive_ptr_release(ConditionVariable* cond)
90 {
91   intrusive_ptr_release(cond->cond_);
92 }
93
94 }
95 }