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 "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
13 "SIMIX Synchronization (mutex, semaphores and conditions)");
15 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout);
16 static void SIMIX_synchro_finish(smx_synchro_t synchro);
17 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
18 smx_process_t issuer, smx_simcall_t simcall);
19 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
20 smx_simcall_t simcall);
22 /***************************** Raw synchronization *********************************/
24 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
26 XBT_IN("(%p, %f)",smx_host,timeout);
29 sync = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
30 sync->type = SIMIX_SYNC_SYNCHRO;
31 sync->name = xbt_strdup("synchro");
32 sync->synchro.sleep = surf_host_sleep(smx_host, timeout);
34 sync->synchro.sleep->setData(sync);
39 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall)
41 XBT_IN("(%p, %p)",process,simcall);
42 switch (simcall->call) {
44 case SIMCALL_MUTEX_LOCK:
45 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
48 case SIMCALL_COND_WAIT:
49 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
52 case SIMCALL_COND_WAIT_TIMEOUT:
53 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
56 case SIMCALL_SEM_ACQUIRE:
57 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
60 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
61 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
70 void SIMIX_synchro_destroy(smx_synchro_t synchro)
72 XBT_IN("(%p)",synchro);
73 XBT_DEBUG("Destroying synchro %p", synchro);
74 xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
75 synchro->synchro.sleep->unref();
76 xbt_free(synchro->name);
77 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
81 void SIMIX_post_synchro(smx_synchro_t synchro)
83 XBT_IN("(%p)",synchro);
84 xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
85 if (synchro->synchro.sleep->getState() == SURF_ACTION_FAILED)
86 synchro->state = SIMIX_FAILED;
87 else if(synchro->synchro.sleep->getState() == SURF_ACTION_DONE)
88 synchro->state = SIMIX_SRC_TIMEOUT;
90 SIMIX_synchro_finish(synchro);
94 static void SIMIX_synchro_finish(smx_synchro_t synchro)
96 XBT_IN("(%p)",synchro);
97 smx_simcall_t simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls);
99 switch (synchro->state) {
101 case SIMIX_SRC_TIMEOUT:
102 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
106 simcall->issuer->context->iwannadie = 1;
107 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
115 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
116 simcall->issuer->waiting_synchro = NULL;
117 SIMIX_synchro_destroy(synchro);
118 SIMIX_simcall_answer(simcall);
121 /*********************************** Mutex ************************************/
123 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall){
124 return SIMIX_mutex_init();
127 * \brief Initialize a mutex.
129 * Allocs and creates the data for the mutex.
132 smx_mutex_t SIMIX_mutex_init(void)
135 s_smx_process_t p; /* useful to initialize sleeping swag */
137 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
139 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
145 * \brief Handles a mutex lock simcall.
146 * \param simcall the simcall
148 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
150 XBT_IN("(%p)",simcall);
151 /* FIXME: check where to validate the arguments */
152 smx_synchro_t synchro = NULL;
153 smx_process_t process = simcall->issuer;
156 /* FIXME: check if the host is active ? */
157 /* Somebody using the mutex, use a synchronization to get host failures */
158 synchro = SIMIX_synchro_wait(process->host, -1);
159 xbt_fifo_push(synchro->simcalls, simcall);
160 simcall->issuer->waiting_synchro = synchro;
161 xbt_swag_insert(simcall->issuer, mutex->sleeping);
165 mutex->owner = simcall->issuer;
166 SIMIX_simcall_answer(simcall);
171 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
172 return SIMIX_mutex_trylock(mutex, simcall->issuer);
175 * \brief Tries to lock a mutex.
177 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
178 * This function does not block and wait for the mutex to be unlocked.
179 * \param mutex The mutex
180 * \param issuer The process that tries to acquire the mutex
181 * \return 1 - mutex free, 0 - mutex used
183 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
185 XBT_IN("(%p, %p)",mutex,issuer);
192 mutex->owner = issuer;
197 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
198 SIMIX_mutex_unlock(mutex, simcall->issuer);
201 * \brief Unlocks a mutex.
203 * Unlocks the mutex and gives it to a process waiting for it.
204 * If the unlocker is not the owner of the mutex nothing happens.
205 * If there are no process waiting, it sets the mutex as free.
206 * \param mutex The mutex
207 * \param issuer The process trying to unlock the mutex
209 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
211 XBT_IN("(%p, %p)",mutex,issuer);
213 /* If the mutex is not owned by the issuer, that's not good */
214 if (issuer != mutex->owner)
215 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
216 SIMIX_process_get_name(mutex->owner),SIMIX_process_get_PID(mutex->owner));
218 if (xbt_swag_size(mutex->sleeping) > 0) {
219 /*process to wake up */
220 smx_process_t p = (smx_process_t) 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->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 = (smx_process_t) 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 = (smx_process_t) 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->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);