Logo AND Algorithmique Numérique Distribuée

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