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. */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
12 "Logging specific to SIMIX (synchronization)");
15 /****************************** Synchronization *******************************/
17 /*********************************** Mutex ************************************/
20 * \brief Initialize a mutex.
22 * Allocs and creates the data for the mutex. It have to be called before the utilisation of the mutex.
25 smx_mutex_t SIMIX_mutex_init()
27 smx_mutex_t m = xbt_new0(s_smx_mutex_t, 1);
28 s_smx_process_t p; /* useful to initialize sleeping swag */
29 /* structures initialization */
31 m->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
36 * \brief Locks a mutex.
38 * Tries to lock a mutex, if the mutex isn't used yet, the process can continue its execution, else it'll be blocked here. You have to call #SIMIX_mutex_unlock to free the mutex.
39 * \param mutex The mutex
41 void SIMIX_mutex_lock(smx_mutex_t mutex)
43 smx_process_t self = SIMIX_process_self();
44 xbt_assert0((mutex != NULL), "Invalid parameters");
47 if (mutex->refcount) {
48 /* somebody using the mutex, block */
49 xbt_swag_insert(self, mutex->sleeping);
51 /* wait for some process make the unlock and wake up me from mutex->sleeping */
52 SIMIX_process_yield();
55 /* verify if the process was suspended */
56 while (self->suspended) {
57 SIMIX_process_yield();
69 * \brief Tries to lock a mutex.
71 * Tries to lock a mutex, return 1 if the mutex is free, 0 else. This function does not block the process if the mutex is used.
72 * \param mutex The mutex
73 * \return 1 - mutex free, 0 - mutex used
75 XBT_INLINE int SIMIX_mutex_trylock(smx_mutex_t mutex)
77 xbt_assert0((mutex != NULL), "Invalid parameters");
88 * \brief Unlocks a mutex.
90 * Unlocks the mutex and wakes up a process blocked on it. If there are no process sleeping, only sets the mutex as free.
91 * \param mutex The mutex
93 void SIMIX_mutex_unlock(smx_mutex_t mutex)
95 smx_process_t p; /*process to wake up */
97 xbt_assert0((mutex != NULL), "Invalid parameters");
99 if (xbt_swag_size(mutex->sleeping) > 0) {
100 p = xbt_swag_extract(mutex->sleeping);
102 xbt_swag_insert(p, simix_global->process_to_run);
104 /* nobody to wake up */
111 * \brief Destroys a mutex.
113 * Destroys and frees the mutex's memory.
114 * \param mutex A mutex
116 XBT_INLINE void SIMIX_mutex_destroy(smx_mutex_t mutex)
121 xbt_swag_free(mutex->sleeping);
127 /******************************** Conditional *********************************/
130 * \brief Initialize a condition.
132 * Allocates and creates the data for the condition.
133 * It have to be called before the use of the condition.
134 * \return A condition
136 smx_cond_t SIMIX_cond_init()
138 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
141 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
142 cond->actions = xbt_fifo_new();
148 * \brief Signalizes a condition.
150 * Signalizes a condition and wakes up a sleeping process. If there are no process sleeping, no action is done.
151 * \param cond A condition
153 void SIMIX_cond_signal(smx_cond_t cond)
155 smx_process_t proc = NULL;
156 DEBUG1("Signal condition %p", cond);
157 xbt_assert0((cond != NULL), "Invalid parameters");
160 if (xbt_swag_size(cond->sleeping) >= 1) {
161 proc = xbt_swag_extract(cond->sleeping);
162 xbt_swag_insert(proc, simix_global->process_to_run);
169 * \brief Waits on a condition.
171 * Blocks a process until the signal is called. This functions frees the mutex associated and locks it after its execution.
172 * \param cond A condition
173 * \param mutex A mutex
175 void SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
177 smx_action_t act_sleep;
179 DEBUG1("Wait condition %p", cond);
181 /* If there is a mutex unlock it */
184 SIMIX_mutex_unlock(mutex);
187 /* Always create an action null in case there is a host failure */
188 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
189 SIMIX_action_set_name(act_sleep,bprintf("Wait condition %p", cond));
190 SIMIX_process_self()->waiting_action = act_sleep;
191 SIMIX_register_action_to_condition(act_sleep, cond);
192 __SIMIX_cond_wait(cond);
193 SIMIX_process_self()->waiting_action = NULL;
194 SIMIX_unregister_action_to_condition(act_sleep, cond);
195 SIMIX_action_destroy(act_sleep);
197 /* get the mutex again if necessary */
199 SIMIX_mutex_lock(cond->mutex);
204 XBT_INLINE xbt_fifo_t SIMIX_cond_get_actions(smx_cond_t cond)
206 xbt_assert0((cond != NULL), "Invalid parameters");
207 return cond->actions;
210 void __SIMIX_cond_wait(smx_cond_t cond)
212 smx_process_t self = SIMIX_process_self();
213 xbt_assert0((cond != NULL), "Invalid parameters");
218 xbt_swag_insert(self, cond->sleeping);
219 SIMIX_process_yield();
221 while (self->suspended) {
222 SIMIX_process_yield();
228 * \brief Waits on a condition with timeout.
230 * Same behavior of #SIMIX_cond_wait, but waits a maximum time and throws an timeout_error if it happens.
231 * \param cond A condition
232 * \param mutex A mutex
233 * \param max_duration Timeout time
235 void SIMIX_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex,
238 smx_action_t act_sleep;
240 DEBUG1("Timed wait condition %p", cond);
242 /* If there is a mutex unlock it */
245 SIMIX_mutex_unlock(mutex);
248 if (max_duration >= 0) {
249 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
250 SIMIX_action_set_name(act_sleep,bprintf("Timed wait condition %p (max_duration:%f)", cond,max_duration));
251 SIMIX_register_action_to_condition(act_sleep, cond);
252 SIMIX_process_self()->waiting_action = act_sleep;
253 __SIMIX_cond_wait(cond);
254 SIMIX_process_self()->waiting_action = NULL;
255 SIMIX_unregister_action_to_condition(act_sleep, cond);
256 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
257 SIMIX_action_destroy(act_sleep);
258 THROW1(timeout_error, 0, "Condition timeout after %f",max_duration);
260 SIMIX_action_destroy(act_sleep);
264 SIMIX_cond_wait(cond,NULL);
266 /* get the mutex again if necessary */
268 SIMIX_mutex_lock(cond->mutex);
272 * \brief Broadcasts a condition.
274 * Signalizes a condition and wakes up ALL sleping process. If there are no process sleeping, no action is done.
275 * \param cond A condition
277 void SIMIX_cond_broadcast(smx_cond_t cond)
279 smx_process_t proc = NULL;
280 smx_process_t proc_next = NULL;
282 xbt_assert0((cond != NULL), "Invalid parameters");
284 DEBUG1("Broadcast condition %p", cond);
285 xbt_swag_foreach_safe(proc, proc_next, cond->sleeping) {
286 xbt_swag_remove(proc, cond->sleeping);
287 xbt_swag_insert(proc, simix_global->process_to_run);
292 * \brief Destroys a contidion.
294 * Destroys and frees the condition's memory.
295 * \param cond A condition
297 void SIMIX_cond_destroy(smx_cond_t cond)
299 DEBUG1("Destroy condition %p", cond);
303 xbt_fifo_item_t item = NULL;
304 smx_action_t action = NULL;
306 xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
307 "Cannot destroy conditional since someone is still using it");
308 xbt_swag_free(cond->sleeping);
310 DEBUG1("%d actions registered", xbt_fifo_size(cond->actions));
311 __SIMIX_cond_display_actions(cond);
312 xbt_fifo_foreach(cond->actions, item, action, smx_action_t) {
313 SIMIX_unregister_action_to_condition(action, cond);
315 __SIMIX_cond_display_actions(cond);
317 xbt_fifo_free(cond->actions);
323 void SIMIX_cond_display_info(smx_cond_t cond)
328 smx_process_t process = NULL;
330 INFO0("Blocked process on this condition:");
331 xbt_swag_foreach(process, cond->sleeping) {
332 INFO2("\t %s running on host %s", process->name,
333 process->smx_host->name);
338 /* ************************** Semaphores ************************************** */
339 #define SMX_SEM_NOLIMIT 99999
340 /** @brief Initialize a semaphore */
341 smx_sem_t SIMIX_sem_init(int capacity) {
342 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
344 sem->sleeping = xbt_fifo_new();
345 sem->actions = xbt_fifo_new();
346 sem->capacity = capacity;
349 /** @brief Destroys a semaphore */
350 void SIMIX_sem_destroy(smx_sem_t sem) {
351 smx_action_t action = NULL;
352 DEBUG1("Destroy semaphore %p", sem);
356 xbt_assert0(xbt_fifo_size(sem->sleeping) == 0,
357 "Cannot destroy semaphore since someone is still using it");
358 xbt_fifo_free(sem->sleeping);
360 DEBUG1("%d actions registered", xbt_fifo_size(sem->actions));
361 while((action=xbt_fifo_pop(sem->actions)))
362 SIMIX_unregister_action_to_semaphore(action, sem);
364 xbt_fifo_free(sem->actions);
368 /** @brief release the semaphore
370 * The first locked process on this semaphore is unlocked.
371 * If no one was blocked, the semaphore capacity is increased by 1.
373 void SIMIX_sem_release(smx_sem_t sem) {
376 DEBUG1("Sem release semaphore %p", sem);
377 if ((proc = xbt_fifo_shift(sem->sleeping)) != NULL) {
378 xbt_swag_insert(proc, simix_global->process_to_run);
379 } else if (sem->capacity != SMX_SEM_NOLIMIT) {
383 /** @brief make sure the semaphore will never be blocking again
385 * This function is not really in the semaphore spirit. It makes
386 * sure that the semaphore will never be blocking anymore.
388 * Releasing and acquiring the semaphore after calling this
389 * function is a noop. Such "broken" semaphores are useful to
390 * implement something between condition variables (with broadcast)
391 * and semaphore (with memory). It's like a semaphore signaled for ever.
393 * There is no way to reset the semaphore to a more regular state afterward.
395 void SIMIX_sem_release_forever(smx_sem_t sem) {
398 DEBUG1("Broadcast semaphore %p", sem);
399 while ((proc = xbt_fifo_shift(sem->sleeping)) != NULL) {
400 xbt_swag_insert(proc, simix_global->process_to_run);
402 sem->capacity = SMX_SEM_NOLIMIT;
406 * \brief Low level wait on a semaphore
408 * This function does not test the capacity of the semaphore and direcly locks
409 * the calling process on the semaphore (until someone call SIMIX_sem_release()
410 * on this semaphore). Do not call this function if you did not attach any action
411 * to this semaphore to be awaken. Note also that you may miss host failure if you
412 * do not attach a dummy action beforehand. SIMIX_sem_acquire does all these
413 * things for you so you it may be preferable to use.
415 void SIMIX_sem_block_onto(smx_sem_t sem) {
416 smx_process_t self = SIMIX_process_self();
420 xbt_fifo_push (sem->sleeping, self);
421 SIMIX_process_yield();
423 while (self->suspended)
424 SIMIX_process_yield();
427 /** @brief Returns true if acquiring this semaphore would block */
428 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem) {
429 return (sem->capacity<=0);
432 /** @brief Returns the current capacity of the semaphore
434 * If it's negative, that's the amount of processes locked on the semaphore
436 int SIMIX_sem_get_capacity(smx_sem_t sem){
437 return sem->capacity;
441 * \brief Waits on a semaphore
443 * If the capacity>0, decrease the capacity.
445 * If capacity==0, locks the current process
446 * until someone call SIMIX_sem_release() on this semaphore
448 void SIMIX_sem_acquire(smx_sem_t sem) {
449 smx_action_t act_sleep;
451 DEBUG1("Wait semaphore %p", sem);
453 if (sem->capacity == SMX_SEM_NOLIMIT) {
454 DEBUG1("semaphore %p wide open", sem);
455 return; /* don't even decrease it if wide open */
458 /* If capacity sufficient, decrease it */
459 if (sem->capacity>0) {
460 DEBUG1("semaphore %p has enough capacity", sem);
466 /* Always create an action null in case there is a host failure */
467 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
468 SIMIX_action_set_name(act_sleep,bprintf("Locked in semaphore %p", sem));
469 SIMIX_process_self()->waiting_action = act_sleep;
470 SIMIX_register_action_to_semaphore(act_sleep, sem);
471 SIMIX_sem_block_onto(sem);
472 SIMIX_process_self()->waiting_action = NULL;
473 SIMIX_unregister_action_to_semaphore(act_sleep, sem);
474 SIMIX_action_destroy(act_sleep);
475 DEBUG1("End of Wait on semaphore %p", sem);
479 * \brief Tries to acquire a semaphore before a timeout
481 * Same behavior of #SIMIX_sem_acquire, but waits a maximum time and throws an timeout_error if it happens.
483 void SIMIX_sem_acquire_timeout(smx_sem_t sem, double max_duration) {
484 smx_action_t act_sleep;
486 DEBUG2("Timed wait semaphore %p (timeout:%f)", sem,max_duration);
488 if (sem->capacity == SMX_SEM_NOLIMIT)
489 return; /* don't even decrease it if wide open */
491 /* If capacity sufficient, decrease it */
492 if (sem->capacity>0) {
497 if (max_duration >= 0) {
499 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
500 SIMIX_action_set_name(act_sleep,bprintf("Timed wait semaphore %p (max_duration:%f)", sem,max_duration));
501 SIMIX_register_action_to_semaphore(act_sleep, sem);
502 SIMIX_process_self()->waiting_action = act_sleep;
503 SIMIX_sem_block_onto(sem);
504 SIMIX_process_self()->waiting_action = NULL;
505 SIMIX_unregister_action_to_semaphore(act_sleep, sem);
506 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
507 SIMIX_action_destroy(act_sleep);
508 THROW1(timeout_error, 0, "Semaphore acquire timeouted after %f",max_duration);
510 SIMIX_action_destroy(act_sleep);
515 SIMIX_sem_acquire(sem);
518 * \brief Blocks on a set of semaphore
520 * If any of the semaphores has some more capacity, it gets decreased.
521 * If not, blocks until the capacity of one of the semaphores becomes more friendly.
523 * \return the rank in the dynar of the semaphore which just got locked from the set
525 unsigned int SIMIX_sem_acquire_any(xbt_dynar_t sems) {
527 unsigned int counter,result=-1;
528 smx_action_t act_sleep;
529 smx_process_t self = SIMIX_process_self();
531 xbt_assert0(xbt_dynar_length(sems),
532 "I refuse to commit sucide by locking on an **empty** set of semaphores!!");
533 DEBUG2("Wait on semaphore set %p (containing %ld semaphores)", sems,xbt_dynar_length(sems));
535 xbt_dynar_foreach(sems,counter,sem) {
536 if (!SIMIX_sem_would_block(sem)) {
537 DEBUG1("Semaphore %p wouldn't block; get it without waiting",sem);
538 SIMIX_sem_acquire(sem);
543 /* Always create an action null in case there is a host failure */
544 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
545 SIMIX_action_set_name(act_sleep,bprintf("Locked in semaphore %p", sem));
546 self->waiting_action = act_sleep;
547 SIMIX_register_action_to_semaphore(act_sleep, xbt_dynar_get_as(sems,0,smx_sem_t));
549 /* Get listed as member of all the provided semaphores */
550 self->sem = (smx_sem_t)sems; /* FIXME: we pass a pointer to dynar where a pointer to sem is expected...*/
551 xbt_dynar_foreach(sems,counter,sem) {
552 xbt_fifo_push(sem->sleeping, self);
554 SIMIX_process_yield();
556 while (self->suspended)
557 SIMIX_process_yield();
559 /* one of the semaphore unsuspended us -- great, let's search which one (and get out of the others) */
560 xbt_dynar_foreach(sems,counter,sem) {
561 if (!xbt_fifo_remove(sem->sleeping, self)) {
562 xbt_assert1(result == -1, "You're trying to wait more than once on semaphore %p, don't you ?", sem);
566 xbt_assert0(counter!=-1,"Cannot find which semaphore unlocked me!");
568 /* Destroy the waiting action */
569 self->waiting_action = NULL;
570 SIMIX_unregister_action_to_semaphore(act_sleep, xbt_dynar_get_as(sems,0,smx_sem_t));
571 SIMIX_action_destroy(act_sleep);