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"
10 #include "src/simix/smx_synchro_private.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ConditionVariable, simix_synchro, "Condition variables");
14 /********************************* Condition **********************************/
16 /** @brief Handle a condition waiting simcall without timeouts */
17 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
19 XBT_IN("(%p)", simcall);
20 smx_actor_t issuer = simcall->issuer;
22 cond->wait(mutex, -1, issuer, simcall);
26 /** @brief Handle a condition waiting simcall with timeouts */
27 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
29 XBT_IN("(%p)", simcall);
30 smx_actor_t issuer = simcall->issuer;
31 simcall_cond_wait_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
32 cond->wait(mutex, timeout, issuer, simcall);
40 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
41 ConditionVariableImpl::~ConditionVariableImpl() = default;
44 * @brief Signalizes a condition.
46 * Signalizes a condition and wakes up a sleeping process.
47 * If there are no process sleeping, no action is done.
49 void ConditionVariableImpl::signal()
51 XBT_DEBUG("Signal condition %p", this);
53 /* If there are processes waiting for the condition choose one and try
54 to make it acquire the mutex */
55 if (not sleeping_.empty()) {
56 auto& proc = sleeping_.front();
57 sleeping_.pop_front();
59 /* Destroy waiter's synchronization */
60 proc.waiting_synchro = nullptr;
62 /* Now transform the cond wait simcall into a mutex lock one */
63 smx_simcall_t simcall = &proc.simcall;
64 smx_mutex_t simcall_mutex;
65 if (simcall->call == SIMCALL_COND_WAIT)
66 simcall_mutex = simcall_cond_wait__get__mutex(simcall);
68 simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
69 simcall->call = SIMCALL_MUTEX_LOCK;
71 simcall_HANDLER_mutex_lock(simcall, simcall_mutex);
77 * @brief Broadcasts a condition.
79 * Signal ALL processes waiting on a condition.
80 * If there are no process waiting, no action is done.
82 void ConditionVariableImpl::broadcast()
84 XBT_DEBUG("Broadcast condition %p", this);
86 /* Signal the condition until nobody is waiting on it */
87 while (not sleeping_.empty())
91 void ConditionVariableImpl::wait(smx_mutex_t mutex, double timeout, smx_actor_t issuer, smx_simcall_t simcall)
93 XBT_IN("(%p, %p, %f, %p,%p)", this, mutex, timeout, issuer, simcall);
94 RawImplPtr synchro = nullptr;
96 XBT_DEBUG("Wait condition %p", this);
98 /* If there is a mutex unlock it */
99 /* FIXME: what happens if the issuer is not the owner of the mutex? */
100 if (mutex != nullptr) {
102 mutex->unlock(issuer);
105 synchro = RawImplPtr(new RawImpl())->start(issuer->get_host(), timeout);
106 synchro->simcalls_.push_front(simcall);
107 issuer->waiting_synchro = synchro;
108 sleeping_.push_back(*simcall->issuer);
112 // boost::intrusive_ptr<ConditionVariableImpl> support:
113 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
115 cond->refcount_.fetch_add(1, std::memory_order_relaxed);
118 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
120 if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
121 std::atomic_thread_fence(std::memory_order_acquire);
122 xbt_assert(cond->sleeping_.empty(), "Cannot destroy conditional since someone is still using it");
126 } // namespace activity
127 } // namespace kernel
128 } // namespace simgrid