Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_condition, simix_synchro, "Condition variables");
13
14 /********************************* Condition **********************************/
15
16 /** @brief Handle a condition waiting simcall with timeouts */
17 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
18 {
19   simcall_cond_wait_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
20   cond->wait(mutex, timeout, simcall->issuer_);
21 }
22
23 namespace simgrid {
24 namespace kernel {
25 namespace activity {
26
27 /**
28  * @brief Signalizes a condition.
29  *
30  * Signalizes a condition and wakes up a sleeping process.
31  * If there are no process sleeping, no action is done.
32  */
33 void ConditionVariableImpl::signal()
34 {
35   XBT_DEBUG("Signal condition %p", this);
36
37   /* If there are processes waiting for the condition choose one and try
38      to make it acquire the mutex */
39   if (not sleeping_.empty()) {
40     auto& proc = sleeping_.front();
41     sleeping_.pop_front();
42
43     /* Destroy waiter's synchronization */
44     proc.waiting_synchro_ = nullptr;
45
46     /* Now transform the cond wait simcall into a mutex lock one */
47     smx_simcall_t simcall = &proc.simcall_;
48     MutexImpl* simcall_mutex;
49     if (simcall->call_ == simix::Simcall::COND_WAIT_TIMEOUT)
50       simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
51     else {
52       // FIXME? using here the MC observer to solve a problem not related to MC
53       auto* observer = dynamic_cast<mc::ConditionWaitSimcall*>(simcall->observer_);
54       xbt_assert(observer != nullptr);
55       simcall_mutex = observer->get_mutex();
56     }
57     simcall->call_ = simix::Simcall::RUN_BLOCKING;
58
59     simcall_mutex->lock(simcall->issuer_);
60   }
61   XBT_OUT();
62 }
63
64 /**
65  * @brief Broadcasts a condition.
66  *
67  * Signal ALL processes waiting on a condition.
68  * If there are no process waiting, no action is done.
69  */
70 void ConditionVariableImpl::broadcast()
71 {
72   XBT_DEBUG("Broadcast condition %p", this);
73
74   /* Signal the condition until nobody is waiting on it */
75   while (not sleeping_.empty())
76     signal();
77 }
78
79 void ConditionVariableImpl::wait(smx_mutex_t mutex, double timeout, actor::ActorImpl* issuer)
80 {
81   XBT_DEBUG("Wait condition %p", this);
82
83   /* If there is a mutex unlock it */
84   if (mutex != nullptr) {
85     xbt_assert(mutex->get_owner() == issuer,
86                "Actor %s cannot wait on ConditionVariable %p since it does not own the provided mutex %p",
87                issuer->get_cname(), this, mutex);
88     mutex_ = mutex;
89     mutex->unlock(issuer);
90   }
91
92   RawImplPtr synchro(new RawImpl([this, issuer]() {
93     this->remove_sleeping_actor(*issuer);
94     if (issuer->simcall_.call_ == simix::Simcall::COND_WAIT_TIMEOUT)
95       simcall_cond_wait_timeout__set__result(&issuer->simcall_, 1);
96   }));
97   synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
98   synchro->register_simcall(&issuer->simcall_);
99   sleeping_.push_back(*issuer);
100 }
101
102 // boost::intrusive_ptr<ConditionVariableImpl> support:
103 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
104 {
105   cond->refcount_.fetch_add(1, std::memory_order_relaxed);
106 }
107
108 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
109 {
110   if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
111     std::atomic_thread_fence(std::memory_order_acquire);
112     xbt_assert(cond->sleeping_.empty(), "Cannot destroy conditional since someone is still using it");
113     delete cond;
114   }
115 }
116 } // namespace activity
117 } // namespace kernel
118 } // namespace simgrid