1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 "Logging specific to SIMIX (synchronization)");
14 static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout);
15 static void SIMIX_synchro_finish(smx_action_t action);
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 /***************************** Synchro action *********************************/
23 static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
25 XBT_IN("(%p, %f)",smx_host,timeout);
27 action = xbt_mallocator_get(simix_global->action_mallocator);
28 action->type = SIMIX_ACTION_SYNCHRO;
29 action->name = xbt_strdup("synchro");
30 action->synchro.sleep =
31 surf_workstation_model->extension.workstation.sleep(smx_host->host, timeout);
33 surf_workstation_model->action_data_set(action->synchro.sleep, action);
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.mutex->sleeping);
47 case SIMCALL_COND_WAIT:
48 xbt_swag_remove(process, simcall->cond_wait.cond->sleeping);
51 case SIMCALL_COND_WAIT_TIMEOUT:
52 xbt_swag_remove(process, simcall->cond_wait_timeout.cond->sleeping);
55 case SIMCALL_SEM_ACQUIRE:
56 xbt_swag_remove(process, simcall->sem_acquire.sem->sleeping);
59 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
60 xbt_swag_remove(process, simcall->sem_acquire_timeout.sem->sleeping);
69 void SIMIX_synchro_destroy(smx_action_t action)
71 XBT_IN("(%p)",action);
72 XBT_DEBUG("Destroying synchro %p", action);
73 action->synchro.sleep->model_type->action_unref(action->synchro.sleep);
74 xbt_free(action->name);
75 xbt_mallocator_release(simix_global->action_mallocator, action);
79 void SIMIX_post_synchro(smx_action_t action)
81 XBT_IN("(%p)",action);
82 if (surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_FAILED)
83 action->state = SIMIX_FAILED;
84 else if(surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_DONE)
85 action->state = SIMIX_SRC_TIMEOUT;
87 SIMIX_synchro_finish(action);
91 static void SIMIX_synchro_finish(smx_action_t action)
93 XBT_IN("(%p)",action);
94 smx_simcall_t simcall = xbt_fifo_shift(action->simcalls);
96 switch (action->state) {
98 case SIMIX_SRC_TIMEOUT:
100 THROWF(timeout_error, 0, "Synchro's wait timeout");
102 CATCH(simcall->issuer->running_ctx->exception) {
103 simcall->issuer->doexception = 1;
109 THROWF(host_error, 0, "Host failed");
111 CATCH(simcall->issuer->running_ctx->exception) {
112 simcall->issuer->doexception = 1;
121 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
122 SIMIX_synchro_destroy(action);
123 SIMIX_simcall_answer(simcall);
126 /*********************************** Mutex ************************************/
129 * \brief Initialize a mutex.
131 * Allocs and creates the data for the mutex.
134 smx_mutex_t SIMIX_mutex_init(void)
137 s_smx_process_t p; /* useful to initialize sleeping swag */
139 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
141 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
147 * \brief Handles a mutex lock simcall.
148 * \param simcall the simcall
150 void SIMIX_pre_mutex_lock(smx_simcall_t simcall)
152 XBT_IN("(%p)",simcall);
153 /* FIXME: check where to validate the arguments */
154 smx_action_t sync_act = NULL;
155 smx_mutex_t mutex = simcall->mutex_lock.mutex;
156 smx_process_t process = simcall->issuer;
159 /* FIXME: check if the host is active ? */
160 /* Somebody using the mutex, use a synchro action to get host failures */
161 sync_act = SIMIX_synchro_wait(process->smx_host, -1);
162 xbt_fifo_push(sync_act->simcalls, simcall);
163 simcall->issuer->waiting_action = sync_act;
164 xbt_swag_insert(simcall->issuer, mutex->sleeping);
168 mutex->owner = simcall->issuer;
169 SIMIX_simcall_answer(simcall);
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;
198 * \brief Unlocks a mutex.
200 * Unlocks the mutex and gives it to a process waiting for it.
201 * If the unlocker is not the owner of the mutex nothing happens.
202 * If there are no process waiting, it sets the mutex as free.
203 * \param mutex The mutex
204 * \param issuer The process trying to unlock the mutex
206 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
208 XBT_IN("(%p, %p)",mutex,issuer);
209 smx_process_t p; /*process to wake up */
211 /* If the mutex is not owned by the issuer do nothing */
212 if (issuer != mutex->owner){
217 if (xbt_swag_size(mutex->sleeping) > 0) {
218 p = xbt_swag_extract(mutex->sleeping);
219 SIMIX_synchro_destroy(p->waiting_action);
220 p->waiting_action = NULL;
222 SIMIX_simcall_answer(&p->simcall);
224 /* nobody to wake up */
232 * \brief Destroys a mutex.
234 * Destroys and frees the mutex's memory.
235 * \param mutex A mutex
237 void SIMIX_mutex_destroy(smx_mutex_t mutex)
239 XBT_IN("(%p)",mutex);
241 xbt_swag_free(mutex->sleeping);
247 /********************************* Condition **********************************/
250 * \brief Initialize a condition.
252 * Allocates and creates the data for the condition.
253 * It have to be called before the use of the condition.
254 * \return A condition
256 smx_cond_t SIMIX_cond_init()
260 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
261 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
268 * \brief Handle a condition waiting simcall without timeouts
269 * \param simcall the simcall
271 void SIMIX_pre_cond_wait(smx_simcall_t simcall)
273 XBT_IN("(%p)",simcall);
274 smx_process_t issuer = simcall->issuer;
275 smx_cond_t cond = simcall->cond_wait.cond;
276 smx_mutex_t mutex = simcall->cond_wait.mutex;
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 SIMIX_pre_cond_wait_timeout(smx_simcall_t simcall)
288 XBT_IN("(%p)",simcall);
289 smx_process_t issuer = simcall->issuer;
290 smx_cond_t cond = simcall->cond_wait_timeout.cond;
291 smx_mutex_t mutex = simcall->cond_wait_timeout.mutex;
292 double timeout = simcall->cond_wait_timeout.timeout;
294 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
299 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
300 smx_process_t issuer, smx_simcall_t simcall)
302 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
303 smx_action_t sync_act = NULL;
305 XBT_DEBUG("Wait condition %p", cond);
307 /* If there is a mutex unlock it */
308 /* FIXME: what happens if the issuer is not the owner of the mutex? */
311 SIMIX_mutex_unlock(mutex, issuer);
314 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
315 xbt_fifo_unshift(sync_act->simcalls, simcall);
316 issuer->waiting_action = sync_act;
317 xbt_swag_insert(simcall->issuer, cond->sleeping);
322 * \brief Signalizes a condition.
324 * Signalizes a condition and wakes up a sleeping process.
325 * If there are no process sleeping, no action is done.
326 * \param cond A condition
328 void SIMIX_cond_signal(smx_cond_t cond)
331 smx_process_t proc = NULL;
332 smx_mutex_t mutex = NULL;
333 smx_simcall_t simcall = NULL;
335 XBT_DEBUG("Signal condition %p", cond);
337 /* If there are processes waiting for the condition choose one and try
338 to make it acquire the mutex */
339 if ((proc = xbt_swag_extract(cond->sleeping))) {
341 /* Destroy waiter's synchro action */
342 SIMIX_synchro_destroy(proc->waiting_action);
343 proc->waiting_action = NULL;
345 /* Now transform the cond wait simcall into a mutex lock one */
346 simcall = &proc->simcall;
347 if(simcall->call == SIMCALL_COND_WAIT)
348 mutex = simcall->cond_wait.mutex;
350 mutex = simcall->cond_wait_timeout.mutex;
352 simcall->call = SIMCALL_MUTEX_LOCK;
353 simcall->mutex_lock.mutex = mutex;
355 SIMIX_pre_mutex_lock(simcall);
361 * \brief Broadcasts a condition.
363 * Signal ALL processes waiting on a condition.
364 * If there are no process waiting, no action is done.
365 * \param cond A condition
367 void SIMIX_cond_broadcast(smx_cond_t cond)
370 XBT_DEBUG("Broadcast condition %p", cond);
372 /* Signal the condition until nobody is waiting on it */
373 while (xbt_swag_size(cond->sleeping)) {
374 SIMIX_cond_signal(cond);
380 * \brief Destroys a contidion.
382 * Destroys and frees the condition's memory.
383 * \param cond A condition
385 void SIMIX_cond_destroy(smx_cond_t cond)
388 XBT_DEBUG("Destroy condition %p", cond);
391 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
392 "Cannot destroy conditional since someone is still using it");
394 xbt_swag_free(cond->sleeping);
400 /******************************** Semaphores **********************************/
401 #define SMX_SEM_NOLIMIT 99999
402 /** @brief Initialize a semaphore */
403 smx_sem_t SIMIX_sem_init(unsigned int value)
405 XBT_IN("(%d)",value);
408 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
409 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
415 /** @brief Destroys a semaphore */
416 void SIMIX_sem_destroy(smx_sem_t sem)
419 XBT_DEBUG("Destroy semaphore %p", sem);
421 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
422 "Cannot destroy semaphore since someone is still using it");
423 xbt_swag_free(sem->sleeping);
429 /** @brief release the semaphore
431 * Unlock a process waiting on the semaphore.
432 * If no one was blocked, the semaphore capacity is increased by 1.
434 void SIMIX_sem_release(smx_sem_t sem)
439 XBT_DEBUG("Sem release semaphore %p", sem);
440 if ((proc = xbt_swag_extract(sem->sleeping))) {
441 proc = xbt_swag_extract(sem->sleeping);
442 SIMIX_synchro_destroy(proc->waiting_action);
443 proc->waiting_action = NULL;
444 SIMIX_simcall_answer(&proc->simcall);
445 } else if (sem->value < SMX_SEM_NOLIMIT) {
451 /** @brief Returns true if acquiring this semaphore would block */
452 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
456 return (sem->value <= 0);
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_action_t sync_act = NULL;
473 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
474 if (sem->value <= 0) {
475 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
476 xbt_fifo_unshift(sync_act->simcalls, simcall);
477 issuer->waiting_action = sync_act;
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 SIMIX_pre_sem_acquire(smx_simcall_t simcall)
492 XBT_IN("(%p)",simcall);
493 _SIMIX_sem_wait(simcall->sem_acquire.sem, -1, simcall->issuer, simcall);
498 * \brief Handles a sem acquire simcall with timeout.
499 * \param simcall the simcall
501 void SIMIX_pre_sem_acquire_timeout(smx_simcall_t simcall)
503 XBT_IN("(%p)",simcall);
504 _SIMIX_sem_wait(simcall->sem_acquire_timeout.sem,
505 simcall->sem_acquire_timeout.timeout, simcall->issuer, simcall);