Logo AND Algorithmique Numérique Distribuée

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