Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[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/s4u/ConditionVariable.hpp"
7 #include "simgrid/simix.h"
8 #include "src/kernel/activity/ConditionVariableImpl.hpp"
9 #include "xbt/log.hpp"
10
11 #include <exception>
12 #include <mutex>
13
14 namespace simgrid {
15 namespace s4u {
16
17 ConditionVariablePtr ConditionVariable::create()
18 {
19   smx_cond_t cond = simcall_cond_init();
20   return ConditionVariablePtr(&cond->cond_, false);
21 }
22
23 /**
24  * Wait functions
25  */
26 void ConditionVariable::wait(MutexPtr lock)
27 {
28   simcall_cond_wait(cond_, lock->pimpl_);
29 }
30
31 void ConditionVariable::wait(std::unique_lock<Mutex>& lock)
32 {
33   simcall_cond_wait(cond_, lock.mutex()->pimpl_);
34 }
35
36 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout)
37 {
38   // The simcall uses -1 for "any timeout" but we don't want this:
39   if (timeout < 0)
40     timeout = 0.0;
41
42   if (simcall_cond_wait_timeout(cond_, lock.mutex()->pimpl_, timeout)) {
43     // If we reached the timeout, we have to take the lock again:
44     lock.mutex()->lock();
45     return std::cv_status::timeout;
46   } else {
47     return std::cv_status::no_timeout;
48   }
49 }
50
51 std::cv_status ConditionVariable::wait_until(std::unique_lock<Mutex>& lock, double timeout_time)
52 {
53   double now = SIMIX_get_clock();
54   double timeout;
55   if (timeout_time < now)
56     timeout = 0.0;
57   else
58     timeout = timeout_time - now;
59   return this->wait_for(lock, timeout);
60 }
61
62 /**
63  * Notify functions
64  */
65 void ConditionVariable::notify_one()
66 {
67   simgrid::simix::simcall([this]() { cond_->signal(); });
68 }
69
70 void ConditionVariable::notify_all()
71 {
72   simgrid::simix::simcall([this]() { cond_->broadcast(); });
73 }
74
75 void intrusive_ptr_add_ref(ConditionVariable* cond)
76 {
77   intrusive_ptr_add_ref(cond->cond_);
78 }
79
80 void intrusive_ptr_release(ConditionVariable* cond)
81 {
82   intrusive_ptr_release(cond->cond_);
83 }
84
85 } // namespace s4u
86 } // namespace simgrid