Logo AND Algorithmique Numérique Distribuée

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