1 /* Copyright (c) 2007-2015. 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(sg_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(sg_host_t smx_host, double timeout)
25 XBT_IN("(%p, %f)",smx_host,timeout);
28 sync = (smx_synchro_t) 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 = (smx_simcall_t) 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->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);
212 /* If the mutex is not owned by the issuer, that's not good */
213 if (issuer != mutex->owner)
214 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
215 SIMIX_process_get_name(mutex->owner),SIMIX_process_get_PID(mutex->owner));
217 if (xbt_swag_size(mutex->sleeping) > 0) {
218 /*process to wake up */
219 smx_process_t p = (smx_process_t) xbt_swag_extract(mutex->sleeping);
220 SIMIX_synchro_destroy(p->waiting_synchro);
221 p->waiting_synchro = NULL;
223 SIMIX_simcall_answer(&p->simcall);
225 /* nobody to wake up */
233 * \brief Destroys a mutex.
235 * Destroys and frees the mutex's memory.
236 * \param mutex A mutex
238 void SIMIX_mutex_destroy(smx_mutex_t mutex)
240 XBT_IN("(%p)",mutex);
242 xbt_swag_free(mutex->sleeping);
248 /********************************* Condition **********************************/
251 * \brief Initialize a condition.
253 * Allocates and creates the data for the condition.
254 * It have to be called before the use of the condition.
255 * \return A condition
257 smx_cond_t SIMIX_cond_init(void)
261 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
262 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
269 * \brief Handle a condition waiting simcall without timeouts
270 * \param simcall the simcall
272 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
274 XBT_IN("(%p)",simcall);
275 smx_process_t issuer = simcall->issuer;
277 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
282 * \brief Handle a condition waiting simcall with timeouts
283 * \param simcall the simcall
285 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
286 smx_mutex_t mutex, double timeout)
288 XBT_IN("(%p)",simcall);
289 smx_process_t issuer = simcall->issuer;
291 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
296 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
297 smx_process_t issuer, smx_simcall_t simcall)
299 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
300 smx_synchro_t synchro = NULL;
302 XBT_DEBUG("Wait condition %p", cond);
304 /* If there is a mutex unlock it */
305 /* FIXME: what happens if the issuer is not the owner of the mutex? */
308 SIMIX_mutex_unlock(mutex, issuer);
311 synchro = SIMIX_synchro_wait(issuer->host, timeout);
312 xbt_fifo_unshift(synchro->simcalls, simcall);
313 issuer->waiting_synchro = synchro;
314 xbt_swag_insert(simcall->issuer, cond->sleeping);
319 * \brief Signalizes a condition.
321 * Signalizes a condition and wakes up a sleeping process.
322 * If there are no process sleeping, no action is done.
323 * \param cond A condition
325 void SIMIX_cond_signal(smx_cond_t cond)
328 smx_process_t proc = NULL;
329 smx_mutex_t mutex = NULL;
330 smx_simcall_t simcall = NULL;
332 XBT_DEBUG("Signal condition %p", cond);
334 /* If there are processes waiting for the condition choose one and try
335 to make it acquire the mutex */
336 if ((proc = (smx_process_t) xbt_swag_extract(cond->sleeping))) {
338 /* Destroy waiter's synchronization */
339 SIMIX_synchro_destroy(proc->waiting_synchro);
340 proc->waiting_synchro = NULL;
342 /* Now transform the cond wait simcall into a mutex lock one */
343 simcall = &proc->simcall;
344 if(simcall->call == SIMCALL_COND_WAIT)
345 mutex = simcall_cond_wait__get__mutex(simcall);
347 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
348 simcall->call = SIMCALL_MUTEX_LOCK;
350 simcall_HANDLER_mutex_lock(simcall, mutex);
356 * \brief Broadcasts a condition.
358 * Signal ALL processes waiting on a condition.
359 * If there are no process waiting, no action is done.
360 * \param cond A condition
362 void SIMIX_cond_broadcast(smx_cond_t cond)
365 XBT_DEBUG("Broadcast condition %p", cond);
367 /* Signal the condition until nobody is waiting on it */
368 while (xbt_swag_size(cond->sleeping)) {
369 SIMIX_cond_signal(cond);
375 * \brief Destroys a condition.
377 * Destroys and frees the condition's memory.
378 * \param cond A condition
380 void SIMIX_cond_destroy(smx_cond_t cond)
383 XBT_DEBUG("Destroy condition %p", cond);
386 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
387 "Cannot destroy conditional since someone is still using it");
389 xbt_swag_free(cond->sleeping);
395 /******************************** Semaphores **********************************/
396 #define SMX_SEM_NOLIMIT 99999
397 /** @brief Initialize a semaphore */
398 smx_sem_t SIMIX_sem_init(unsigned int value)
400 XBT_IN("(%u)",value);
403 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
404 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
410 /** @brief Destroys a semaphore */
411 void SIMIX_sem_destroy(smx_sem_t sem)
414 XBT_DEBUG("Destroy semaphore %p", sem);
416 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
417 "Cannot destroy semaphore since someone is still using it");
418 xbt_swag_free(sem->sleeping);
424 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
425 SIMIX_sem_release(sem);
427 /** @brief release the semaphore
429 * Unlock a process waiting on the semaphore.
430 * If no one was blocked, the semaphore capacity is increased by 1.
432 void SIMIX_sem_release(smx_sem_t sem)
437 XBT_DEBUG("Sem release semaphore %p", sem);
438 if ((proc = (smx_process_t) xbt_swag_extract(sem->sleeping))) {
439 SIMIX_synchro_destroy(proc->waiting_synchro);
440 proc->waiting_synchro = NULL;
441 SIMIX_simcall_answer(&proc->simcall);
442 } else if (sem->value < SMX_SEM_NOLIMIT) {
448 /** @brief Returns true if acquiring this semaphore would block */
449 int SIMIX_sem_would_block(smx_sem_t sem)
453 return (sem->value <= 0);
456 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
457 return SIMIX_sem_get_capacity(sem);
459 /** @brief Returns the current capacity of the semaphore */
460 int SIMIX_sem_get_capacity(smx_sem_t sem)
467 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
468 smx_simcall_t simcall)
470 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
471 smx_synchro_t synchro = NULL;
473 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
474 if (sem->value <= 0) {
475 synchro = SIMIX_synchro_wait(issuer->host, timeout);
476 xbt_fifo_unshift(synchro->simcalls, simcall);
477 issuer->waiting_synchro = synchro;
478 xbt_swag_insert(issuer, sem->sleeping);
481 SIMIX_simcall_answer(simcall);
487 * \brief Handles a sem acquire simcall without timeout.
488 * \param simcall the simcall
490 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
492 XBT_IN("(%p)",simcall);
493 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
498 * \brief Handles a sem acquire simcall with timeout.
499 * \param simcall the simcall
501 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
503 XBT_IN("(%p)",simcall);
504 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
507 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
508 return SIMIX_sem_would_block(sem);