Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify
[simgrid.git] / src / kernel / activity / ConditionVariableImpl.cpp
1 /* Copyright (c) 2007-2021. 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 "src/kernel/activity/ConditionVariableImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "src/kernel/activity/MutexImpl.hpp"
9 #include "src/kernel/activity/SynchroRaw.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include <cmath> // std::isfinite
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_condition, simix_synchro, "Condition variables");
14
15 /********************************* Condition **********************************/
16
17 namespace simgrid {
18 namespace kernel {
19 namespace activity {
20
21 /**
22  * @brief Signalizes a condition.
23  *
24  * Signalizes a condition and wakes up a sleeping actor.
25  * If there are no actor sleeping, no action is done.
26  */
27 void ConditionVariableImpl::signal()
28 {
29   XBT_DEBUG("Signal condition %p", this);
30
31   /* If there are actors waiting for the condition choose one and try to make it acquire the mutex */
32   if (not sleeping_.empty()) {
33     auto& proc = sleeping_.front();
34     sleeping_.pop_front();
35
36     /* Destroy waiter's synchronization */
37     proc.waiting_synchro_ = nullptr;
38
39     /* Now transform the cond wait simcall into a mutex lock one */
40     smx_simcall_t simcall = &proc.simcall_;
41     const auto* observer  = dynamic_cast<kernel::actor::ConditionWaitSimcall*>(simcall->observer_);
42     xbt_assert(observer != nullptr);
43     observer->get_mutex()->lock(simcall->issuer_);
44   }
45   XBT_OUT();
46 }
47
48 /**
49  * @brief Broadcasts a condition.
50  *
51  * Signal ALL actors waiting on a condition.
52  * If there are no actor waiting, no action is done.
53  */
54 void ConditionVariableImpl::broadcast()
55 {
56   XBT_DEBUG("Broadcast condition %p", this);
57
58   /* Signal the condition until nobody is waiting on it */
59   while (not sleeping_.empty())
60     signal();
61 }
62
63 void ConditionVariableImpl::wait(smx_mutex_t mutex, double timeout, actor::ActorImpl* issuer)
64 {
65   XBT_DEBUG("Wait condition %p", this);
66   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
67
68   /* If there is a mutex unlock it */
69   if (mutex != nullptr) {
70     xbt_assert(mutex->get_owner() == issuer,
71                "Actor %s cannot wait on ConditionVariable %p since it does not own the provided mutex %p",
72                issuer->get_cname(), this, mutex);
73     mutex_ = mutex;
74     mutex->unlock(issuer);
75   }
76
77   RawImplPtr synchro(new RawImpl([this, issuer]() {
78     this->remove_sleeping_actor(*issuer);
79     auto* observer = dynamic_cast<kernel::actor::ConditionWaitSimcall*>(issuer->simcall_.observer_);
80     xbt_assert(observer != nullptr);
81     observer->set_result(true);
82   }));
83   synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
84   synchro->register_simcall(&issuer->simcall_);
85   sleeping_.push_back(*issuer);
86 }
87
88 // boost::intrusive_ptr<ConditionVariableImpl> support:
89 void intrusive_ptr_add_ref(ConditionVariableImpl* cond)
90 {
91   cond->refcount_.fetch_add(1, std::memory_order_relaxed);
92 }
93
94 void intrusive_ptr_release(ConditionVariableImpl* cond)
95 {
96   if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
97     std::atomic_thread_fence(std::memory_order_acquire);
98     xbt_assert(cond->sleeping_.empty(), "Cannot destroy conditional since someone is still using it");
99     delete cond;
100   }
101 }
102 } // namespace activity
103 } // namespace kernel
104 } // namespace simgrid