Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
now, kernel::actor::simcall_blocking can return a value
[simgrid.git] / src / s4u / s4u_ConditionVariable.cpp
1 /* Copyright (c) 2006-2019. 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 "simgrid/cond.h"
7 #include "simgrid/forward.h"
8 #include "simgrid/s4u/ConditionVariable.hpp"
9 #include "simgrid/simix.h"
10 #include "src/kernel/activity/ConditionVariableImpl.hpp"
11 #include "xbt/log.hpp"
12
13 #include <exception>
14 #include <mutex>
15
16 namespace simgrid {
17 namespace s4u {
18
19 ConditionVariablePtr ConditionVariable::create()
20 {
21   kernel::activity::ConditionVariableImpl* cond =
22       kernel::actor::simcall([] { return new kernel::activity::ConditionVariableImpl(); });
23   return ConditionVariablePtr(&cond->cond_, false);
24 }
25
26 /**
27  * Wait functions
28  */
29 void ConditionVariable::wait(MutexPtr lock)
30 {
31   simcall_cond_wait(cond_, lock->pimpl_);
32 }
33
34 void ConditionVariable::wait(std::unique_lock<Mutex>& lock)
35 {
36   simcall_cond_wait(cond_, lock.mutex()->pimpl_);
37 }
38
39 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout)
40 {
41   // The simcall uses -1 for "any timeout" but we don't want this:
42   if (timeout < 0)
43     timeout = 0.0;
44
45   if (simcall_cond_wait_timeout(cond_, lock.mutex()->pimpl_, timeout)) {
46     // If we reached the timeout, we have to take the lock again:
47     lock.mutex()->lock();
48     return std::cv_status::timeout;
49   } else {
50     return std::cv_status::no_timeout;
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 {
70   simgrid::kernel::actor::simcall([this]() { cond_->signal(); });
71 }
72
73 void ConditionVariable::notify_all()
74 {
75   simgrid::kernel::actor::simcall([this]() { cond_->broadcast(); });
76 }
77
78 void intrusive_ptr_add_ref(ConditionVariable* cond)
79 {
80   intrusive_ptr_add_ref(cond->cond_);
81 }
82
83 void intrusive_ptr_release(ConditionVariable* cond)
84 {
85   intrusive_ptr_release(cond->cond_);
86 }
87
88 } // namespace s4u
89 } // namespace simgrid
90
91 /* **************************** Public C interface *************************** */
92 sg_cond_t sg_cond_init()
93 {
94   simgrid::kernel::activity::ConditionVariableImpl* cond =
95       simgrid::kernel::actor::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
96
97   return new simgrid::s4u::ConditionVariable(cond);
98 }
99
100 void sg_cond_wait(sg_cond_t cond, sg_mutex_t mutex)
101 {
102   cond->wait(mutex);
103 }
104
105 int sg_cond_wait_for(sg_cond_t cond, sg_mutex_t mutex, double delay)
106 {
107   std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
108   return cond->wait_for(lock, delay) == std::cv_status::timeout ? 1 : 0;
109 }
110
111 void sg_cond_notify_one(sg_cond_t cond)
112 {
113   cond->notify_one();
114 }
115
116 void sg_cond_notify_all(sg_cond_t cond)
117 {
118   cond->notify_all();
119 }
120
121 void sg_cond_destroy(sg_cond_t cond)
122 {
123   delete cond;
124 }