1 /* Copyright (c) 2007-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "smx_private.hpp"
8 #include "src/surf/cpu_interface.hpp"
9 #include "src/surf/surf_interface.hpp"
12 #include <xbt/utility.hpp>
14 #include "src/kernel/activity/MutexImpl.hpp"
15 #include "src/kernel/activity/SynchroRaw.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
19 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
20 smx_actor_t issuer, smx_simcall_t simcall);
21 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
22 smx_simcall_t simcall);
24 /***************************** Raw synchronization *********************************/
26 smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
28 XBT_IN("(%p, %f)",smx_host,timeout);
30 simgrid::kernel::activity::RawImplPtr sync =
31 simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl());
32 sync->sleep = smx_host->pimpl_cpu->sleep(timeout);
33 sync->sleep->setData(sync.get());
38 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
40 XBT_IN("(%p, %p)",process,simcall);
41 switch (simcall->call) {
43 case SIMCALL_MUTEX_LOCK:
44 simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping, *process);
47 case SIMCALL_COND_WAIT:
48 simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping, *process);
51 case SIMCALL_COND_WAIT_TIMEOUT:
52 simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping, *process);
55 case SIMCALL_SEM_ACQUIRE:
56 simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping, *process);
59 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
60 simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping, *process);
69 void SIMIX_synchro_finish(smx_activity_t synchro)
71 XBT_IN("(%p)", synchro.get());
72 smx_simcall_t simcall = synchro->simcalls.front();
73 synchro->simcalls.pop_front();
75 switch (synchro->state) {
77 case SIMIX_SRC_TIMEOUT:
78 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
82 simcall->issuer->context->iwannadie = 1;
90 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
91 simcall->issuer->waiting_synchro = nullptr;
92 SIMIX_simcall_answer(simcall);
96 /********************************* Condition **********************************/
99 * \brief Initialize a condition.
101 * Allocates and creates the data for the condition.
102 * It have to be called before the use of the condition.
103 * \return A condition
105 smx_cond_t SIMIX_cond_init()
108 smx_cond_t cond = new s_smx_cond_t();
115 * \brief Handle a condition waiting simcall without timeouts
116 * \param simcall the simcall
118 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
120 XBT_IN("(%p)",simcall);
121 smx_actor_t issuer = simcall->issuer;
123 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
128 * \brief Handle a condition waiting simcall with timeouts
129 * \param simcall the simcall
131 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
132 smx_mutex_t mutex, double timeout)
134 XBT_IN("(%p)",simcall);
135 smx_actor_t issuer = simcall->issuer;
137 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
142 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
143 smx_actor_t issuer, smx_simcall_t simcall)
145 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
146 smx_activity_t synchro = nullptr;
148 XBT_DEBUG("Wait condition %p", cond);
150 /* If there is a mutex unlock it */
151 /* FIXME: what happens if the issuer is not the owner of the mutex? */
152 if (mutex != nullptr) {
154 mutex->unlock(issuer);
157 synchro = SIMIX_synchro_wait(issuer->host, timeout);
158 synchro->simcalls.push_front(simcall);
159 issuer->waiting_synchro = synchro;
160 cond->sleeping.push_back(*simcall->issuer);
165 * \brief Signalizes a condition.
167 * Signalizes a condition and wakes up a sleeping process.
168 * If there are no process sleeping, no action is done.
169 * \param cond A condition
171 void SIMIX_cond_signal(smx_cond_t cond)
174 XBT_DEBUG("Signal condition %p", cond);
176 /* If there are processes waiting for the condition choose one and try
177 to make it acquire the mutex */
178 if (not cond->sleeping.empty()) {
179 auto& proc = cond->sleeping.front();
180 cond->sleeping.pop_front();
182 /* Destroy waiter's synchronization */
183 proc.waiting_synchro = nullptr;
185 /* Now transform the cond wait simcall into a mutex lock one */
186 smx_simcall_t simcall = &proc.simcall;
188 if(simcall->call == SIMCALL_COND_WAIT)
189 mutex = simcall_cond_wait__get__mutex(simcall);
191 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
192 simcall->call = SIMCALL_MUTEX_LOCK;
194 simcall_HANDLER_mutex_lock(simcall, mutex);
200 * \brief Broadcasts a condition.
202 * Signal ALL processes waiting on a condition.
203 * If there are no process waiting, no action is done.
204 * \param cond A condition
206 void SIMIX_cond_broadcast(smx_cond_t cond)
209 XBT_DEBUG("Broadcast condition %p", cond);
211 /* Signal the condition until nobody is waiting on it */
212 while (not cond->sleeping.empty()) {
213 SIMIX_cond_signal(cond);
218 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
221 intrusive_ptr_add_ref(cond);
225 void SIMIX_cond_unref(smx_cond_t cond)
228 XBT_DEBUG("Destroy condition %p", cond);
229 if (cond != nullptr) {
230 intrusive_ptr_release(cond);
236 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
238 auto previous = cond->refcount_.fetch_add(1);
239 xbt_assert(previous != 0);
242 void intrusive_ptr_release(s_smx_cond_t *cond)
244 if (cond->refcount_.fetch_sub(1) == 1) {
245 xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
250 /******************************** Semaphores **********************************/
251 /** @brief Initialize a semaphore */
252 smx_sem_t SIMIX_sem_init(unsigned int value)
254 XBT_IN("(%u)",value);
255 smx_sem_t sem = new s_smx_sem_t;
261 /** @brief Destroys a semaphore */
262 void SIMIX_sem_destroy(smx_sem_t sem)
265 XBT_DEBUG("Destroy semaphore %p", sem);
266 if (sem != nullptr) {
267 xbt_assert(sem->sleeping.empty(), "Cannot destroy semaphore since someone is still using it");
273 /** @brief release the semaphore
275 * Unlock a process waiting on the semaphore.
276 * If no one was blocked, the semaphore capacity is increased by 1.
278 void SIMIX_sem_release(smx_sem_t sem)
281 XBT_DEBUG("Sem release semaphore %p", sem);
282 if (not sem->sleeping.empty()) {
283 auto& proc = sem->sleeping.front();
284 sem->sleeping.pop_front();
285 proc.waiting_synchro = nullptr;
286 SIMIX_simcall_answer(&proc.simcall);
293 /** @brief Returns true if acquiring this semaphore would block */
294 int SIMIX_sem_would_block(smx_sem_t sem)
298 return (sem->value <= 0);
301 /** @brief Returns the current capacity of the semaphore */
302 int SIMIX_sem_get_capacity(smx_sem_t sem)
309 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
310 smx_simcall_t simcall)
312 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
313 smx_activity_t synchro = nullptr;
315 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
316 if (sem->value <= 0) {
317 synchro = SIMIX_synchro_wait(issuer->host, timeout);
318 synchro->simcalls.push_front(simcall);
319 issuer->waiting_synchro = synchro;
320 sem->sleeping.push_back(*issuer);
323 SIMIX_simcall_answer(simcall);
329 * \brief Handles a sem acquire simcall without timeout.
330 * \param simcall the simcall
332 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
334 XBT_IN("(%p)",simcall);
335 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
340 * \brief Handles a sem acquire simcall with timeout.
341 * \param simcall the simcall
343 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
345 XBT_IN("(%p)",simcall);
346 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);