1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
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. */
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
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_condition, simix_synchro, "Condition variables");
15 /********************************* Condition **********************************/
22 * @brief Signalizes a condition.
24 * Signalizes a condition and wakes up a sleeping process.
25 * If there are no process sleeping, no action is done.
27 void ConditionVariableImpl::signal()
29 XBT_DEBUG("Signal condition %p", this);
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();
37 /* Destroy waiter's synchronization */
38 proc.waiting_synchro_ = nullptr;
40 /* Now transform the cond wait simcall into a mutex lock one */
41 smx_simcall_t simcall = &proc.simcall_;
42 const auto* observer = dynamic_cast<kernel::actor::ConditionWaitSimcall*>(simcall->observer_);
43 xbt_assert(observer != nullptr);
44 observer->get_mutex()->lock(simcall->issuer_);
50 * @brief Broadcasts a condition.
52 * Signal ALL processes waiting on a condition.
53 * If there are no process waiting, no action is done.
55 void ConditionVariableImpl::broadcast()
57 XBT_DEBUG("Broadcast condition %p", this);
59 /* Signal the condition until nobody is waiting on it */
60 while (not sleeping_.empty())
64 void ConditionVariableImpl::wait(smx_mutex_t mutex, double timeout, actor::ActorImpl* issuer)
66 XBT_DEBUG("Wait condition %p", this);
67 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
69 /* If there is a mutex unlock it */
70 if (mutex != nullptr) {
71 xbt_assert(mutex->get_owner() == issuer,
72 "Actor %s cannot wait on ConditionVariable %p since it does not own the provided mutex %p",
73 issuer->get_cname(), this, mutex);
75 mutex->unlock(issuer);
78 RawImplPtr synchro(new RawImpl([this, issuer]() {
79 this->remove_sleeping_actor(*issuer);
80 auto* observer = dynamic_cast<kernel::actor::ConditionWaitSimcall*>(issuer->simcall_.observer_);
81 xbt_assert(observer != nullptr);
82 observer->set_result(true);
84 synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
85 synchro->register_simcall(&issuer->simcall_);
86 sleeping_.push_back(*issuer);
89 // boost::intrusive_ptr<ConditionVariableImpl> support:
90 void intrusive_ptr_add_ref(ConditionVariableImpl* cond)
92 cond->refcount_.fetch_add(1, std::memory_order_relaxed);
95 void intrusive_ptr_release(ConditionVariableImpl* cond)
97 if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
98 std::atomic_thread_fence(std::memory_order_acquire);
99 xbt_assert(cond->sleeping_.empty(), "Cannot destroy conditional since someone is still using it");
103 } // namespace activity
104 } // namespace kernel
105 } // namespace simgrid