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 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
17 smx_simcall_t simcall)
19 XBT_IN("(%p, %p, %f, %p,%p)", cond, mutex, timeout, issuer, simcall);
20 simgrid::kernel::activity::RawImplPtr synchro = nullptr;
22 XBT_DEBUG("Wait condition %p", cond);
24 /* If there is a mutex unlock it */
25 /* FIXME: what happens if the issuer is not the owner of the mutex? */
26 if (mutex != nullptr) {
28 mutex->unlock(issuer);
31 synchro = simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl())
32 ->start(issuer->get_host(), timeout);
33 synchro->simcalls_.push_front(simcall);
34 issuer->waiting_synchro = synchro;
35 cond->sleeping.push_back(*simcall->issuer);
40 * @brief Handle a condition waiting simcall without timeouts
42 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
44 XBT_IN("(%p)", simcall);
45 smx_actor_t issuer = simcall->issuer;
47 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
52 * @brief Handle a condition waiting simcall with timeouts
54 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
56 XBT_IN("(%p)", simcall);
57 smx_actor_t issuer = simcall->issuer;
58 simcall_cond_wait_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
59 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
67 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
68 ConditionVariableImpl::~ConditionVariableImpl() = default;
71 * @brief Signalizes a condition.
73 * Signalizes a condition and wakes up a sleeping process.
74 * If there are no process sleeping, no action is done.
76 void ConditionVariableImpl::signal()
78 XBT_DEBUG("Signal condition %p", this);
80 /* If there are processes waiting for the condition choose one and try
81 to make it acquire the mutex */
82 if (not sleeping.empty()) {
83 auto& proc = sleeping.front();
86 /* Destroy waiter's synchronization */
87 proc.waiting_synchro = nullptr;
89 /* Now transform the cond wait simcall into a mutex lock one */
90 smx_simcall_t simcall = &proc.simcall;
91 smx_mutex_t simcall_mutex;
92 if (simcall->call == SIMCALL_COND_WAIT)
93 simcall_mutex = simcall_cond_wait__get__mutex(simcall);
95 simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
96 simcall->call = SIMCALL_MUTEX_LOCK;
98 simcall_HANDLER_mutex_lock(simcall, simcall_mutex);
104 * @brief Broadcasts a condition.
106 * Signal ALL processes waiting on a condition.
107 * If there are no process waiting, no action is done.
109 void ConditionVariableImpl::broadcast()
111 XBT_DEBUG("Broadcast condition %p", this);
113 /* Signal the condition until nobody is waiting on it */
114 while (not sleeping.empty())
118 // boost::intrusive_ptr<ConditionVariableImpl> support:
119 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
121 cond->refcount_.fetch_add(1, std::memory_order_relaxed);
124 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
126 if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
127 std::atomic_thread_fence(std::memory_order_acquire);
128 xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
132 } // namespace activity
133 } // namespace kernel