1 /* Copyright (c) 2007-2014. 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.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
12 "SIMIX Synchronization (mutex, semaphores and conditions)");
14 static smx_synchro_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout);
15 static void SIMIX_synchro_finish(smx_synchro_t synchro);
16 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
17 smx_process_t issuer, smx_simcall_t simcall);
18 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
19 smx_simcall_t simcall);
21 /***************************** Raw synchronization *********************************/
23 static smx_synchro_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
25 XBT_IN("(%p, %f)",smx_host,timeout);
28 sync = xbt_mallocator_get(simix_global->synchro_mallocator);
29 sync->type = SIMIX_SYNC_SYNCHRO;
30 sync->name = xbt_strdup("synchro");
31 sync->synchro.sleep = surf_host_sleep(smx_host, timeout);
33 surf_action_set_data(sync->synchro.sleep, sync);
38 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall)
40 XBT_IN("(%p, %p)",process,simcall);
41 switch (simcall->call) {
43 case SIMCALL_MUTEX_LOCK:
44 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
47 case SIMCALL_COND_WAIT:
48 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
51 case SIMCALL_COND_WAIT_TIMEOUT:
52 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
55 case SIMCALL_SEM_ACQUIRE:
56 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
59 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
60 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
69 void SIMIX_synchro_destroy(smx_synchro_t synchro)
71 XBT_IN("(%p)",synchro);
72 XBT_DEBUG("Destroying synchro %p", synchro);
73 xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
74 surf_action_unref(synchro->synchro.sleep);
75 xbt_free(synchro->name);
76 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
80 void SIMIX_post_synchro(smx_synchro_t synchro)
82 XBT_IN("(%p)",synchro);
83 xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
84 if (surf_action_get_state(synchro->synchro.sleep) == SURF_ACTION_FAILED)
85 synchro->state = SIMIX_FAILED;
86 else if(surf_action_get_state(synchro->synchro.sleep) == SURF_ACTION_DONE)
87 synchro->state = SIMIX_SRC_TIMEOUT;
89 SIMIX_synchro_finish(synchro);
93 static void SIMIX_synchro_finish(smx_synchro_t synchro)
95 XBT_IN("(%p)",synchro);
96 smx_simcall_t simcall = xbt_fifo_shift(synchro->simcalls);
98 switch (synchro->state) {
100 case SIMIX_SRC_TIMEOUT:
101 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
105 simcall->issuer->context->iwannadie = 1;
106 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
114 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
115 simcall->issuer->waiting_synchro = NULL;
116 SIMIX_synchro_destroy(synchro);
117 SIMIX_simcall_answer(simcall);
120 /*********************************** Mutex ************************************/
122 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall){
123 return SIMIX_mutex_init();
126 * \brief Initialize a mutex.
128 * Allocs and creates the data for the mutex.
131 smx_mutex_t SIMIX_mutex_init(void)
134 s_smx_process_t p; /* useful to initialize sleeping swag */
136 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
138 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
144 * \brief Handles a mutex lock simcall.
145 * \param simcall the simcall
147 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
149 XBT_IN("(%p)",simcall);
150 /* FIXME: check where to validate the arguments */
151 smx_synchro_t synchro = NULL;
152 smx_process_t process = simcall->issuer;
155 /* FIXME: check if the host is active ? */
156 /* Somebody using the mutex, use a synchronization to get host failures */
157 synchro = SIMIX_synchro_wait(process->smx_host, -1);
158 xbt_fifo_push(synchro->simcalls, simcall);
159 simcall->issuer->waiting_synchro = synchro;
160 xbt_swag_insert(simcall->issuer, mutex->sleeping);
164 mutex->owner = simcall->issuer;
165 SIMIX_simcall_answer(simcall);
170 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
171 return SIMIX_mutex_trylock(mutex, simcall->issuer);
174 * \brief Tries to lock a mutex.
176 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
177 * This function does not block and wait for the mutex to be unlocked.
178 * \param mutex The mutex
179 * \param issuer The process that tries to acquire the mutex
180 * \return 1 - mutex free, 0 - mutex used
182 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
184 XBT_IN("(%p, %p)",mutex,issuer);
191 mutex->owner = issuer;
196 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
197 SIMIX_mutex_unlock(mutex, simcall->issuer);
200 * \brief Unlocks a mutex.
202 * Unlocks the mutex and gives it to a process waiting for it.
203 * If the unlocker is not the owner of the mutex nothing happens.
204 * If there are no process waiting, it sets the mutex as free.
205 * \param mutex The mutex
206 * \param issuer The process trying to unlock the mutex
208 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
210 XBT_IN("(%p, %p)",mutex,issuer);
211 smx_process_t p; /*process to wake up */
213 /* If the mutex is not owned by the issuer do nothing */
214 if (issuer != mutex->owner){
219 if (xbt_swag_size(mutex->sleeping) > 0) {
220 p = xbt_swag_extract(mutex->sleeping);
221 SIMIX_synchro_destroy(p->waiting_synchro);
222 p->waiting_synchro = NULL;
224 SIMIX_simcall_answer(&p->simcall);
226 /* nobody to wake up */
234 * \brief Destroys a mutex.
236 * Destroys and frees the mutex's memory.
237 * \param mutex A mutex
239 void SIMIX_mutex_destroy(smx_mutex_t mutex)
241 XBT_IN("(%p)",mutex);
243 xbt_swag_free(mutex->sleeping);
249 /********************************* Condition **********************************/
252 * \brief Initialize a condition.
254 * Allocates and creates the data for the condition.
255 * It have to be called before the use of the condition.
256 * \return A condition
258 smx_cond_t SIMIX_cond_init(void)
262 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
263 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
270 * \brief Handle a condition waiting simcall without timeouts
271 * \param simcall the simcall
273 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
275 XBT_IN("(%p)",simcall);
276 smx_process_t issuer = simcall->issuer;
278 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
283 * \brief Handle a condition waiting simcall with timeouts
284 * \param simcall the simcall
286 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
287 smx_mutex_t mutex, double timeout)
289 XBT_IN("(%p)",simcall);
290 smx_process_t issuer = simcall->issuer;
292 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
297 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
298 smx_process_t issuer, smx_simcall_t simcall)
300 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
301 smx_synchro_t synchro = NULL;
303 XBT_DEBUG("Wait condition %p", cond);
305 /* If there is a mutex unlock it */
306 /* FIXME: what happens if the issuer is not the owner of the mutex? */
309 SIMIX_mutex_unlock(mutex, issuer);
312 synchro = SIMIX_synchro_wait(issuer->smx_host, timeout);
313 xbt_fifo_unshift(synchro->simcalls, simcall);
314 issuer->waiting_synchro = synchro;
315 xbt_swag_insert(simcall->issuer, cond->sleeping);
320 * \brief Signalizes a condition.
322 * Signalizes a condition and wakes up a sleeping process.
323 * If there are no process sleeping, no action is done.
324 * \param cond A condition
326 void SIMIX_cond_signal(smx_cond_t cond)
329 smx_process_t proc = NULL;
330 smx_mutex_t mutex = NULL;
331 smx_simcall_t simcall = NULL;
333 XBT_DEBUG("Signal condition %p", cond);
335 /* If there are processes waiting for the condition choose one and try
336 to make it acquire the mutex */
337 if ((proc = xbt_swag_extract(cond->sleeping))) {
339 /* Destroy waiter's synchronization */
340 SIMIX_synchro_destroy(proc->waiting_synchro);
341 proc->waiting_synchro = NULL;
343 /* Now transform the cond wait simcall into a mutex lock one */
344 simcall = &proc->simcall;
345 if(simcall->call == SIMCALL_COND_WAIT)
346 mutex = simcall_cond_wait__get__mutex(simcall);
348 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
349 simcall->call = SIMCALL_MUTEX_LOCK;
351 simcall_HANDLER_mutex_lock(simcall, mutex);
357 * \brief Broadcasts a condition.
359 * Signal ALL processes waiting on a condition.
360 * If there are no process waiting, no action is done.
361 * \param cond A condition
363 void SIMIX_cond_broadcast(smx_cond_t cond)
366 XBT_DEBUG("Broadcast condition %p", cond);
368 /* Signal the condition until nobody is waiting on it */
369 while (xbt_swag_size(cond->sleeping)) {
370 SIMIX_cond_signal(cond);
376 * \brief Destroys a condition.
378 * Destroys and frees the condition's memory.
379 * \param cond A condition
381 void SIMIX_cond_destroy(smx_cond_t cond)
384 XBT_DEBUG("Destroy condition %p", cond);
387 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
388 "Cannot destroy conditional since someone is still using it");
390 xbt_swag_free(cond->sleeping);
396 /******************************** Semaphores **********************************/
397 #define SMX_SEM_NOLIMIT 99999
398 /** @brief Initialize a semaphore */
399 smx_sem_t SIMIX_sem_init(unsigned int value)
401 XBT_IN("(%u)",value);
404 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
405 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
411 /** @brief Destroys a semaphore */
412 void SIMIX_sem_destroy(smx_sem_t sem)
415 XBT_DEBUG("Destroy semaphore %p", sem);
417 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
418 "Cannot destroy semaphore since someone is still using it");
419 xbt_swag_free(sem->sleeping);
425 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
426 SIMIX_sem_release(sem);
428 /** @brief release the semaphore
430 * Unlock a process waiting on the semaphore.
431 * If no one was blocked, the semaphore capacity is increased by 1.
433 void SIMIX_sem_release(smx_sem_t sem)
438 XBT_DEBUG("Sem release semaphore %p", sem);
439 if ((proc = xbt_swag_extract(sem->sleeping))) {
440 SIMIX_synchro_destroy(proc->waiting_synchro);
441 proc->waiting_synchro = NULL;
442 SIMIX_simcall_answer(&proc->simcall);
443 } else if (sem->value < SMX_SEM_NOLIMIT) {
449 /** @brief Returns true if acquiring this semaphore would block */
450 int SIMIX_sem_would_block(smx_sem_t sem)
454 return (sem->value <= 0);
457 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
458 return SIMIX_sem_get_capacity(sem);
460 /** @brief Returns the current capacity of the semaphore */
461 int SIMIX_sem_get_capacity(smx_sem_t sem)
468 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
469 smx_simcall_t simcall)
471 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
472 smx_synchro_t synchro = NULL;
474 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
475 if (sem->value <= 0) {
476 synchro = SIMIX_synchro_wait(issuer->smx_host, timeout);
477 xbt_fifo_unshift(synchro->simcalls, simcall);
478 issuer->waiting_synchro = synchro;
479 xbt_swag_insert(issuer, sem->sleeping);
482 SIMIX_simcall_answer(simcall);
488 * \brief Handles a sem acquire simcall without timeout.
489 * \param simcall the simcall
491 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
493 XBT_IN("(%p)",simcall);
494 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
499 * \brief Handles a sem acquire simcall with timeout.
500 * \param simcall the simcall
502 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
504 XBT_IN("(%p)",simcall);
505 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
508 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
509 return SIMIX_sem_would_block(sem);