Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the VM-related data out of MSG's private data for hosts
[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(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   // The simcall uses -1 for "any timeout" but we don't want this:
29   if (timeout < 0)
30     timeout = 0.0;
31
32   try {
33     simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
34     return std::cv_status::no_timeout;
35   }
36   catch (xbt_ex& e) {
37
38     // If the exception was a timeout, we have to take the lock again:
39     if (e.category == timeout_error) {
40       try {
41         lock.mutex()->lock();
42         return std::cv_status::timeout;
43       }
44       catch (...) {
45         std::terminate();
46       }
47     }
48
49     // Another exception: should we reaquire the lock?
50     std::terminate();
51   }
52   catch (...) {
53     std::terminate();
54   }
55 }
56
57 std::cv_status ConditionVariable::wait_until(std::unique_lock<Mutex>& lock, double timeout_time)
58 {
59   double now = SIMIX_get_clock();
60   double timeout;
61   if (timeout_time < now)
62     timeout = 0.0;
63   else
64     timeout = timeout_time - now;
65   return this->wait_for(lock, timeout);
66 }
67   
68 /**
69  * Notify functions
70  */
71 void ConditionVariable::notify_one() {
72    simcall_cond_signal(cond_);
73 }
74  
75 void ConditionVariable::notify_all() {
76   simcall_cond_broadcast(cond_);
77 }
78
79 void intrusive_ptr_add_ref(ConditionVariable* cond)
80 {
81   intrusive_ptr_add_ref(cond->cond_);
82 }
83
84 void intrusive_ptr_release(ConditionVariable* cond)
85 {
86   intrusive_ptr_release(cond->cond_);
87 }
88
89 }
90 }