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, 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__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_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:
99 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
103 simcall->issuer->context->iwannadie = 1;
104 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
112 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
113 simcall->issuer->waiting_action = NULL;
114 SIMIX_synchro_destroy(action);
115 SIMIX_simcall_answer(simcall);
118 /*********************************** Mutex ************************************/
120 smx_mutex_t SIMIX_pre_mutex_init(smx_simcall_t simcall){
121 return SIMIX_mutex_init();
124 * \brief Initialize a mutex.
126 * Allocs and creates the data for the mutex.
129 smx_mutex_t SIMIX_mutex_init(void)
132 s_smx_process_t p; /* useful to initialize sleeping swag */
134 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
136 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
142 * \brief Handles a mutex lock simcall.
143 * \param simcall the simcall
145 void SIMIX_pre_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
147 XBT_IN("(%p)",simcall);
148 /* FIXME: check where to validate the arguments */
149 smx_action_t sync_act = NULL;
150 smx_process_t process = simcall->issuer;
153 /* FIXME: check if the host is active ? */
154 /* Somebody using the mutex, use a synchro action to get host failures */
155 sync_act = SIMIX_synchro_wait(process->smx_host, -1);
156 xbt_fifo_push(sync_act->simcalls, simcall);
157 simcall->issuer->waiting_action = sync_act;
158 xbt_swag_insert(simcall->issuer, mutex->sleeping);
162 mutex->owner = simcall->issuer;
163 SIMIX_simcall_answer(simcall);
168 int SIMIX_pre_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
169 return SIMIX_mutex_trylock(mutex, simcall->issuer);
172 * \brief Tries to lock a mutex.
174 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
175 * This function does not block and wait for the mutex to be unlocked.
176 * \param mutex The mutex
177 * \param issuer The process that tries to acquire the mutex
178 * \return 1 - mutex free, 0 - mutex used
180 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
182 XBT_IN("(%p, %p)",mutex,issuer);
189 mutex->owner = issuer;
194 void SIMIX_pre_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
195 SIMIX_mutex_unlock(mutex, simcall->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 */
231 void SIMIX_pre_mutex_destroy(smx_simcall_t simcall, smx_mutex_t mutex){
232 SIMIX_mutex_destroy(mutex);
235 * \brief Destroys a mutex.
237 * Destroys and frees the mutex's memory.
238 * \param mutex A mutex
240 void SIMIX_mutex_destroy(smx_mutex_t mutex)
242 XBT_IN("(%p)",mutex);
244 xbt_swag_free(mutex->sleeping);
250 /********************************* Condition **********************************/
252 smx_cond_t SIMIX_pre_cond_init(smx_simcall_t simcall){
253 return SIMIX_cond_init();
256 * \brief Initialize a condition.
258 * Allocates and creates the data for the condition.
259 * It have to be called before the use of the condition.
260 * \return A condition
262 smx_cond_t SIMIX_cond_init(void)
266 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
267 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
274 * \brief Handle a condition waiting simcall without timeouts
275 * \param simcall the simcall
277 void SIMIX_pre_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
279 XBT_IN("(%p)",simcall);
280 smx_process_t issuer = simcall->issuer;
282 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
287 * \brief Handle a condition waiting simcall with timeouts
288 * \param simcall the simcall
290 void SIMIX_pre_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
291 smx_mutex_t mutex, double timeout)
293 XBT_IN("(%p)",simcall);
294 smx_process_t issuer = simcall->issuer;
296 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
301 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
302 smx_process_t issuer, smx_simcall_t simcall)
304 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
305 smx_action_t sync_act = NULL;
307 XBT_DEBUG("Wait condition %p", cond);
309 /* If there is a mutex unlock it */
310 /* FIXME: what happens if the issuer is not the owner of the mutex? */
313 SIMIX_mutex_unlock(mutex, issuer);
316 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
317 xbt_fifo_unshift(sync_act->simcalls, simcall);
318 issuer->waiting_action = sync_act;
319 xbt_swag_insert(simcall->issuer, cond->sleeping);
323 void SIMIX_pre_cond_signal(smx_simcall_t simcall, smx_cond_t cond){
324 SIMIX_cond_signal(cond);
327 * \brief Signalizes a condition.
329 * Signalizes a condition and wakes up a sleeping process.
330 * If there are no process sleeping, no action is done.
331 * \param cond A condition
333 void SIMIX_cond_signal(smx_cond_t cond)
336 smx_process_t proc = NULL;
337 smx_mutex_t mutex = NULL;
338 smx_simcall_t simcall = NULL;
340 XBT_DEBUG("Signal condition %p", cond);
342 /* If there are processes waiting for the condition choose one and try
343 to make it acquire the mutex */
344 if ((proc = xbt_swag_extract(cond->sleeping))) {
346 /* Destroy waiter's synchro action */
347 SIMIX_synchro_destroy(proc->waiting_action);
348 proc->waiting_action = NULL;
350 /* Now transform the cond wait simcall into a mutex lock one */
351 simcall = &proc->simcall;
352 if(simcall->call == SIMCALL_COND_WAIT)
353 mutex = simcall_cond_wait__get__mutex(simcall);
355 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
356 simcall->call = SIMCALL_MUTEX_LOCK;
358 SIMIX_pre_mutex_lock(simcall, mutex);
363 void SIMIX_pre_cond_broadcast(smx_simcall_t simcall, smx_cond_t cond){
364 SIMIX_cond_broadcast(cond);
367 * \brief Broadcasts a condition.
369 * Signal ALL processes waiting on a condition.
370 * If there are no process waiting, no action is done.
371 * \param cond A condition
373 void SIMIX_cond_broadcast(smx_cond_t cond)
376 XBT_DEBUG("Broadcast condition %p", cond);
378 /* Signal the condition until nobody is waiting on it */
379 while (xbt_swag_size(cond->sleeping)) {
380 SIMIX_cond_signal(cond);
385 void SIMIX_pre_cond_destroy(smx_simcall_t simcall, smx_cond_t cond){
386 SIMIX_cond_destroy(cond);
389 * \brief Destroys a contidion.
391 * Destroys and frees the condition's memory.
392 * \param cond A condition
394 void SIMIX_cond_destroy(smx_cond_t cond)
397 XBT_DEBUG("Destroy condition %p", cond);
400 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
401 "Cannot destroy conditional since someone is still using it");
403 xbt_swag_free(cond->sleeping);
409 /******************************** Semaphores **********************************/
410 #define SMX_SEM_NOLIMIT 99999
411 smx_sem_t SIMIX_pre_sem_init(smx_simcall_t simcall, unsigned int value){
412 return SIMIX_sem_init(value);
414 /** @brief Initialize a semaphore */
415 smx_sem_t SIMIX_sem_init(unsigned int value)
417 XBT_IN("(%u)",value);
420 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
421 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
427 void SIMIX_pre_sem_destroy(smx_simcall_t simcall, smx_sem_t sem){
428 SIMIX_sem_destroy(sem);
430 /** @brief Destroys a semaphore */
431 void SIMIX_sem_destroy(smx_sem_t sem)
434 XBT_DEBUG("Destroy semaphore %p", sem);
436 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
437 "Cannot destroy semaphore since someone is still using it");
438 xbt_swag_free(sem->sleeping);
444 void SIMIX_pre_sem_release(smx_simcall_t simcall, smx_sem_t sem){
445 SIMIX_sem_release(sem);
447 /** @brief release the semaphore
449 * Unlock a process waiting on the semaphore.
450 * If no one was blocked, the semaphore capacity is increased by 1.
452 void SIMIX_sem_release(smx_sem_t sem)
457 XBT_DEBUG("Sem release semaphore %p", sem);
458 if ((proc = xbt_swag_extract(sem->sleeping))) {
459 SIMIX_synchro_destroy(proc->waiting_action);
460 proc->waiting_action = NULL;
461 SIMIX_simcall_answer(&proc->simcall);
462 } else if (sem->value < SMX_SEM_NOLIMIT) {
468 /** @brief Returns true if acquiring this semaphore would block */
469 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
473 return (sem->value <= 0);
476 int SIMIX_pre_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
477 return SIMIX_sem_get_capacity(sem);
479 /** @brief Returns the current capacity of the semaphore */
480 int SIMIX_sem_get_capacity(smx_sem_t sem)
487 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
488 smx_simcall_t simcall)
490 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
491 smx_action_t sync_act = NULL;
493 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
494 if (sem->value <= 0) {
495 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
496 xbt_fifo_unshift(sync_act->simcalls, simcall);
497 issuer->waiting_action = sync_act;
498 xbt_swag_insert(issuer, sem->sleeping);
501 SIMIX_simcall_answer(simcall);
507 * \brief Handles a sem acquire simcall without timeout.
508 * \param simcall the simcall
510 void SIMIX_pre_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
512 XBT_IN("(%p)",simcall);
513 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
518 * \brief Handles a sem acquire simcall with timeout.
519 * \param simcall the simcall
521 void SIMIX_pre_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
523 XBT_IN("(%p)",simcall);
524 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);