1 /* Copyright (c) 2007-2019. 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"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ConditionVariable, simix_synchro, "Condition variables");
13 /********************************* Condition **********************************/
15 /** @brief Handle a condition waiting simcall without timeouts */
16 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
18 XBT_IN("(%p)", simcall);
19 smx_actor_t issuer = simcall->issuer;
21 cond->wait(mutex, -1, issuer, simcall);
25 /** @brief Handle a condition waiting simcall with timeouts */
26 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
28 XBT_IN("(%p)", simcall);
29 smx_actor_t issuer = simcall->issuer;
30 simcall_cond_wait_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
31 cond->wait(mutex, timeout, issuer, simcall);
39 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
40 ConditionVariableImpl::~ConditionVariableImpl() = default;
43 * @brief Signalizes a condition.
45 * Signalizes a condition and wakes up a sleeping process.
46 * If there are no process sleeping, no action is done.
48 void ConditionVariableImpl::signal()
50 XBT_DEBUG("Signal condition %p", this);
52 /* If there are processes waiting for the condition choose one and try
53 to make it acquire the mutex */
54 if (not sleeping_.empty()) {
55 auto& proc = sleeping_.front();
56 sleeping_.pop_front();
58 /* Destroy waiter's synchronization */
59 proc.waiting_synchro = nullptr;
61 /* Now transform the cond wait simcall into a mutex lock one */
62 smx_simcall_t simcall = &proc.simcall;
63 MutexImpl* simcall_mutex;
64 if (simcall->call == SIMCALL_COND_WAIT)
65 simcall_mutex = simcall_cond_wait__get__mutex(simcall);
67 simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
68 simcall->call = SIMCALL_MUTEX_LOCK;
70 simcall_mutex->lock(simcall->issuer);
76 * @brief Broadcasts a condition.
78 * Signal ALL processes waiting on a condition.
79 * If there are no process waiting, no action is done.
81 void ConditionVariableImpl::broadcast()
83 XBT_DEBUG("Broadcast condition %p", this);
85 /* Signal the condition until nobody is waiting on it */
86 while (not sleeping_.empty())
90 void ConditionVariableImpl::wait(smx_mutex_t mutex, double timeout, actor::ActorImpl* issuer, smx_simcall_t simcall)
92 XBT_IN("(%p, %p, %f, %p,%p)", this, mutex, timeout, issuer, simcall);
93 RawImplPtr synchro = nullptr;
95 XBT_DEBUG("Wait condition %p", this);
97 /* If there is a mutex unlock it */
98 /* FIXME: what happens if the issuer is not the owner of the mutex? */
99 if (mutex != nullptr) {
101 mutex->unlock(issuer);
104 synchro = RawImplPtr(new RawImpl())->start(issuer->get_host(), timeout);
105 synchro->simcalls_.push_front(simcall);
106 issuer->waiting_synchro = synchro;
107 sleeping_.push_back(*simcall->issuer);
111 // boost::intrusive_ptr<ConditionVariableImpl> support:
112 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
114 cond->refcount_.fetch_add(1, std::memory_order_relaxed);
117 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
119 if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
120 std::atomic_thread_fence(std::memory_order_acquire);
121 xbt_assert(cond->sleeping_.empty(), "Cannot destroy conditional since someone is still using it");
125 } // namespace activity
126 } // namespace kernel
127 } // namespace simgrid