2 /* Copyright (c) 2007-2015. The SimGrid Team.
3 * All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "src/surf/surf_interface.hpp"
9 #include "smx_private.h"
12 #include "src/simix/SynchroRaw.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
15 "SIMIX Synchronization (mutex, semaphores and conditions)");
17 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout);
18 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
19 smx_process_t issuer, smx_simcall_t simcall);
20 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
21 smx_simcall_t simcall);
23 /***************************** Raw synchronization *********************************/
25 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
27 XBT_IN("(%p, %f)",smx_host,timeout);
29 simgrid::simix::Raw *sync = new simgrid::simix::Raw();
30 sync->sleep = surf_host_sleep(smx_host, timeout);
31 sync->sleep->setData(sync);
36 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall)
38 XBT_IN("(%p, %p)",process,simcall);
39 switch (simcall->call) {
41 case SIMCALL_MUTEX_LOCK:
42 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
45 case SIMCALL_COND_WAIT:
46 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
49 case SIMCALL_COND_WAIT_TIMEOUT:
50 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
53 case SIMCALL_SEM_ACQUIRE:
54 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
57 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
58 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
67 void SIMIX_synchro_finish(smx_synchro_t synchro)
69 XBT_IN("(%p)",synchro);
70 smx_simcall_t simcall = synchro->simcalls.front();
71 synchro->simcalls.pop_front();
73 switch (synchro->state) {
75 case SIMIX_SRC_TIMEOUT:
76 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
80 simcall->issuer->context->iwannadie = 1;
81 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
89 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
90 simcall->issuer->waiting_synchro = nullptr;
92 SIMIX_simcall_answer(simcall);
95 /*********************************** Mutex ************************************/
102 XBT_IN("(%p)", this);
103 // Useful to initialize sleeping swag:
104 simgrid::simix::Process p;
105 this->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
111 XBT_IN("(%p)", this);
112 xbt_swag_free(this->sleeping);
116 void Mutex::lock(smx_process_t issuer)
118 XBT_IN("(%p; %p)", this, issuer);
119 /* FIXME: check where to validate the arguments */
120 smx_synchro_t synchro = nullptr;
123 /* FIXME: check if the host is active ? */
124 /* Somebody using the mutex, use a synchronization to get host failures */
125 synchro = SIMIX_synchro_wait(issuer->host, -1);
126 synchro->simcalls.push_back(&issuer->simcall);
127 issuer->waiting_synchro = synchro;
128 xbt_swag_insert(issuer, this->sleeping);
132 this->owner = issuer;
133 SIMIX_simcall_answer(&issuer->simcall);
138 /** Tries to lock the mutex for a process
140 * \param issuer the process that tries to acquire the mutex
141 * \return whether we managed to lock the mutex
143 bool Mutex::try_lock(smx_process_t issuer)
145 XBT_IN("(%p, %p)", this, issuer);
152 this->owner = issuer;
157 /** Unlock a mutex for a process
159 * Unlocks the mutex and gives it to a process waiting for it.
160 * If the unlocker is not the owner of the mutex nothing happens.
161 * If there are no process waiting, it sets the mutex as free.
163 void Mutex::unlock(smx_process_t issuer)
165 XBT_IN("(%p, %p)", this, issuer);
167 /* If the mutex is not owned by the issuer, that's not good */
168 if (issuer != this->owner)
169 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
170 SIMIX_process_get_name(this->owner),SIMIX_process_get_PID(this->owner));
172 if (xbt_swag_size(this->sleeping) > 0) {
173 /*process to wake up */
174 smx_process_t p = (smx_process_t) xbt_swag_extract(this->sleeping);
175 delete p->waiting_synchro;
176 p->waiting_synchro = nullptr;
178 SIMIX_simcall_answer(&p->simcall);
180 /* nobody to wake up */
181 this->locked = false;
182 this->owner = nullptr;
190 /** Increase the refcount for this mutex */
191 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
193 if (mutex != nullptr)
194 intrusive_ptr_add_ref(mutex);
198 /** Decrease the refcount for this mutex */
199 void SIMIX_mutex_unref(smx_mutex_t mutex)
201 if (mutex != nullptr)
202 intrusive_ptr_release(mutex);
205 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall)
207 return new simgrid::simix::Mutex();
212 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
214 mutex->lock(simcall->issuer);
217 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
219 return mutex->try_lock(simcall->issuer);
222 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
224 mutex->unlock(simcall->issuer);
227 /********************************* Condition **********************************/
230 * \brief Initialize a condition.
232 * Allocates and creates the data for the condition.
233 * It have to be called before the use of the condition.
234 * \return A condition
236 smx_cond_t SIMIX_cond_init(void)
239 simgrid::simix::Process p;
240 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
241 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
242 cond->mutex = nullptr;
248 * \brief Handle a condition waiting simcall without timeouts
249 * \param simcall the simcall
251 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
253 XBT_IN("(%p)",simcall);
254 smx_process_t issuer = simcall->issuer;
256 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
261 * \brief Handle a condition waiting simcall with timeouts
262 * \param simcall the simcall
264 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
265 smx_mutex_t mutex, double timeout)
267 XBT_IN("(%p)",simcall);
268 smx_process_t issuer = simcall->issuer;
270 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
275 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
276 smx_process_t issuer, smx_simcall_t simcall)
278 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
279 smx_synchro_t synchro = nullptr;
281 XBT_DEBUG("Wait condition %p", cond);
283 /* If there is a mutex unlock it */
284 /* FIXME: what happens if the issuer is not the owner of the mutex? */
285 if (mutex != nullptr) {
287 mutex->unlock(issuer);
290 synchro = SIMIX_synchro_wait(issuer->host, timeout);
291 synchro->simcalls.push_front(simcall);
292 issuer->waiting_synchro = synchro;
293 xbt_swag_insert(simcall->issuer, cond->sleeping);
298 * \brief Signalizes a condition.
300 * Signalizes a condition and wakes up a sleeping process.
301 * If there are no process sleeping, no action is done.
302 * \param cond A condition
304 void SIMIX_cond_signal(smx_cond_t cond)
307 smx_process_t proc = nullptr;
308 smx_mutex_t mutex = nullptr;
309 smx_simcall_t simcall = nullptr;
311 XBT_DEBUG("Signal condition %p", cond);
313 /* If there are processes waiting for the condition choose one and try
314 to make it acquire the mutex */
315 if ((proc = (smx_process_t) xbt_swag_extract(cond->sleeping))) {
317 /* Destroy waiter's synchronization */
318 delete proc->waiting_synchro;
319 proc->waiting_synchro = nullptr;
321 /* Now transform the cond wait simcall into a mutex lock one */
322 simcall = &proc->simcall;
323 if(simcall->call == SIMCALL_COND_WAIT)
324 mutex = simcall_cond_wait__get__mutex(simcall);
326 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
327 simcall->call = SIMCALL_MUTEX_LOCK;
329 simcall_HANDLER_mutex_lock(simcall, mutex);
335 * \brief Broadcasts a condition.
337 * Signal ALL processes waiting on a condition.
338 * If there are no process waiting, no action is done.
339 * \param cond A condition
341 void SIMIX_cond_broadcast(smx_cond_t cond)
344 XBT_DEBUG("Broadcast condition %p", cond);
346 /* Signal the condition until nobody is waiting on it */
347 while (xbt_swag_size(cond->sleeping)) {
348 SIMIX_cond_signal(cond);
354 * \brief Destroys a condition.
356 * Destroys and frees the condition's memory.
357 * \param cond A condition
359 void SIMIX_cond_destroy(smx_cond_t cond)
362 XBT_DEBUG("Destroy condition %p", cond);
364 if (cond != nullptr) {
365 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
366 "Cannot destroy conditional since someone is still using it");
368 xbt_swag_free(cond->sleeping);
374 /******************************** Semaphores **********************************/
375 #define SMX_SEM_NOLIMIT 99999
376 /** @brief Initialize a semaphore */
377 smx_sem_t SIMIX_sem_init(unsigned int value)
379 XBT_IN("(%u)",value);
380 simgrid::simix::Process p;
382 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
383 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
389 /** @brief Destroys a semaphore */
390 void SIMIX_sem_destroy(smx_sem_t sem)
393 XBT_DEBUG("Destroy semaphore %p", sem);
394 if (sem != nullptr) {
395 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
396 "Cannot destroy semaphore since someone is still using it");
397 xbt_swag_free(sem->sleeping);
403 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
404 SIMIX_sem_release(sem);
406 /** @brief release the semaphore
408 * Unlock a process waiting on the semaphore.
409 * If no one was blocked, the semaphore capacity is increased by 1.
411 void SIMIX_sem_release(smx_sem_t sem)
416 XBT_DEBUG("Sem release semaphore %p", sem);
417 if ((proc = (smx_process_t) xbt_swag_extract(sem->sleeping))) {
418 delete proc->waiting_synchro;
419 proc->waiting_synchro = nullptr;
420 SIMIX_simcall_answer(&proc->simcall);
421 } else if (sem->value < SMX_SEM_NOLIMIT) {
427 /** @brief Returns true if acquiring this semaphore would block */
428 int SIMIX_sem_would_block(smx_sem_t sem)
432 return (sem->value <= 0);
435 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
436 return SIMIX_sem_get_capacity(sem);
438 /** @brief Returns the current capacity of the semaphore */
439 int SIMIX_sem_get_capacity(smx_sem_t sem)
446 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
447 smx_simcall_t simcall)
449 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
450 smx_synchro_t synchro = nullptr;
452 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
453 if (sem->value <= 0) {
454 synchro = SIMIX_synchro_wait(issuer->host, timeout);
455 synchro->simcalls.push_front(simcall);
456 issuer->waiting_synchro = synchro;
457 xbt_swag_insert(issuer, sem->sleeping);
460 SIMIX_simcall_answer(simcall);
466 * \brief Handles a sem acquire simcall without timeout.
467 * \param simcall the simcall
469 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
471 XBT_IN("(%p)",simcall);
472 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
477 * \brief Handles a sem acquire simcall with timeout.
478 * \param simcall the simcall
480 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
482 XBT_IN("(%p)",simcall);
483 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
486 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
487 return SIMIX_sem_would_block(sem);