Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
partially stringify s4u::Actor
[simgrid.git] / src / s4u / s4u_ConditionVariable.cpp
1 /* Copyright (c) 2006-2018. 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/kernel/activity/ConditionVariableImpl.hpp"
15
16 namespace simgrid {
17 namespace s4u {
18
19 ConditionVariablePtr ConditionVariable::create()
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->pimpl_);
31 }
32
33 void ConditionVariable::wait(std::unique_lock<Mutex>& lock)
34 {
35   simcall_cond_wait(cond_, lock.mutex()->pimpl_);
36 }
37
38 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout)
39 {
40   // The simcall uses -1 for "any timeout" but we don't want this:
41   if (timeout < 0)
42     timeout = 0.0;
43
44   if (simcall_cond_wait_timeout(cond_, lock.mutex()->pimpl_, timeout)) {
45     // If we reached the timeout, we have to take the lock again:
46     lock.mutex()->lock();
47     return std::cv_status::timeout;
48   } else {
49     return std::cv_status::no_timeout;
50   }
51 }
52
53 std::cv_status ConditionVariable::wait_until(std::unique_lock<Mutex>& lock, double timeout_time)
54 {
55   double now = SIMIX_get_clock();
56   double timeout;
57   if (timeout_time < now)
58     timeout = 0.0;
59   else
60     timeout = timeout_time - now;
61   return this->wait_for(lock, timeout);
62 }
63
64 /**
65  * Notify functions
66  */
67 void ConditionVariable::notify_one()
68 {
69   simgrid::simix::simcall([this]() { cond_->signal(); });
70 }
71
72 void ConditionVariable::notify_all()
73 {
74   simgrid::simix::simcall([this]() { cond_->broadcast(); });
75 }
76
77 void intrusive_ptr_add_ref(ConditionVariable* cond)
78 {
79   intrusive_ptr_add_ref(cond->cond_);
80 }
81
82 void intrusive_ptr_release(ConditionVariable* cond)
83 {
84   intrusive_ptr_release(cond->cond_);
85 }
86
87 } // namespace s4u
88 } // namespace simgrid