Logo AND Algorithmique Numérique Distribuée

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