1 /* Copyright (c) 2007-2018. 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 "src/kernel/activity/MutexImpl.hpp"
8 #include "src/kernel/activity/SynchroRaw.hpp"
9 #include "src/simix/smx_synchro_private.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ConditionVariable, simix_synchro, "Condition variables");
14 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
15 smx_simcall_t simcall);
17 /********************************* Condition **********************************/
20 * \brief Initialize a condition.
22 * Allocates and creates the data for the condition.
23 * It have to be called before the use of the condition.
26 smx_cond_t SIMIX_cond_init()
29 smx_cond_t cond = new simgrid::kernel::activity::ConditionVariableImpl();
35 * \brief Handle a condition waiting simcall without timeouts
36 * \param simcall the simcall
38 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
40 XBT_IN("(%p)", simcall);
41 smx_actor_t issuer = simcall->issuer;
43 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
48 * \brief Handle a condition waiting simcall with timeouts
49 * \param simcall the simcall
51 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
53 XBT_IN("(%p)", simcall);
54 smx_actor_t issuer = simcall->issuer;
56 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
60 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
61 smx_simcall_t simcall)
63 XBT_IN("(%p, %p, %f, %p,%p)", cond, mutex, timeout, issuer, simcall);
64 smx_activity_t synchro = nullptr;
66 XBT_DEBUG("Wait condition %p", cond);
68 /* If there is a mutex unlock it */
69 /* FIXME: what happens if the issuer is not the owner of the mutex? */
70 if (mutex != nullptr) {
72 mutex->unlock(issuer);
75 synchro = SIMIX_synchro_wait(issuer->host, timeout);
76 synchro->simcalls.push_front(simcall);
77 issuer->waiting_synchro = synchro;
78 cond->sleeping.push_back(*simcall->issuer);
86 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
87 ConditionVariableImpl::~ConditionVariableImpl() = default;
90 * \brief Signalizes a condition.
92 * Signalizes a condition and wakes up a sleeping process.
93 * If there are no process sleeping, no action is done.
94 * \param cond A condition
96 void ConditionVariableImpl::signal()
98 XBT_DEBUG("Signal condition %p", this);
100 /* If there are processes waiting for the condition choose one and try
101 to make it acquire the mutex */
102 if (not sleeping.empty()) {
103 auto& proc = sleeping.front();
104 sleeping.pop_front();
106 /* Destroy waiter's synchronization */
107 proc.waiting_synchro = nullptr;
109 /* Now transform the cond wait simcall into a mutex lock one */
110 smx_simcall_t simcall = &proc.simcall;
112 if (simcall->call == SIMCALL_COND_WAIT)
113 mutex = simcall_cond_wait__get__mutex(simcall);
115 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
116 simcall->call = SIMCALL_MUTEX_LOCK;
118 simcall_HANDLER_mutex_lock(simcall, mutex);
124 * \brief Broadcasts a condition.
126 * Signal ALL processes waiting on a condition.
127 * If there are no process waiting, no action is done.
129 void ConditionVariableImpl::broadcast()
131 XBT_DEBUG("Broadcast condition %p", this);
133 /* Signal the condition until nobody is waiting on it */
134 while (not sleeping.empty())
138 // boost::intrusive_ptr<ConditionVariableImpl> support:
139 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
141 cond->refcount_.fetch_add(1, std::memory_order_relaxed);
144 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
146 if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
147 std::atomic_thread_fence(std::memory_order_acquire);
148 xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
152 } // namespace activity
153 } // namespace kernel