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
37 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
39 XBT_IN("(%p)", simcall);
40 smx_actor_t issuer = simcall->issuer;
42 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
47 * \brief Handle a condition waiting simcall with timeouts
49 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
51 XBT_IN("(%p)", simcall);
52 smx_actor_t issuer = simcall->issuer;
54 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
58 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
59 smx_simcall_t simcall)
61 XBT_IN("(%p, %p, %f, %p,%p)", cond, mutex, timeout, issuer, simcall);
62 smx_activity_t synchro = nullptr;
64 XBT_DEBUG("Wait condition %p", cond);
66 /* If there is a mutex unlock it */
67 /* FIXME: what happens if the issuer is not the owner of the mutex? */
68 if (mutex != nullptr) {
70 mutex->unlock(issuer);
73 synchro = SIMIX_synchro_wait(issuer->host, timeout);
74 synchro->simcalls.push_front(simcall);
75 issuer->waiting_synchro = synchro;
76 cond->sleeping.push_back(*simcall->issuer);
84 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
85 ConditionVariableImpl::~ConditionVariableImpl() = default;
88 * \brief Signalizes a condition.
90 * Signalizes a condition and wakes up a sleeping process.
91 * If there are no process sleeping, no action is done.
93 void ConditionVariableImpl::signal()
95 XBT_DEBUG("Signal condition %p", this);
97 /* If there are processes waiting for the condition choose one and try
98 to make it acquire the mutex */
99 if (not sleeping.empty()) {
100 auto& proc = sleeping.front();
101 sleeping.pop_front();
103 /* Destroy waiter's synchronization */
104 proc.waiting_synchro = nullptr;
106 /* Now transform the cond wait simcall into a mutex lock one */
107 smx_simcall_t simcall = &proc.simcall;
108 smx_mutex_t simcall_mutex;
109 if (simcall->call == SIMCALL_COND_WAIT)
110 simcall_mutex = simcall_cond_wait__get__mutex(simcall);
112 simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
113 simcall->call = SIMCALL_MUTEX_LOCK;
115 simcall_HANDLER_mutex_lock(simcall, simcall_mutex);
121 * \brief Broadcasts a condition.
123 * Signal ALL processes waiting on a condition.
124 * If there are no process waiting, no action is done.
126 void ConditionVariableImpl::broadcast()
128 XBT_DEBUG("Broadcast condition %p", this);
130 /* Signal the condition until nobody is waiting on it */
131 while (not sleeping.empty())
135 // boost::intrusive_ptr<ConditionVariableImpl> support:
136 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
138 cond->refcount_.fetch_add(1, std::memory_order_relaxed);
141 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
143 if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
144 std::atomic_thread_fence(std::memory_order_acquire);
145 xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
149 } // namespace activity
150 } // namespace kernel