3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4 All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
14 "Logging specific to SIMIX (synchronization)");
17 /****************************** Synchronization *******************************/
19 /*********************************** Mutex ************************************/
20 smx_mutex_t SIMIX_mutex_init()
22 smx_mutex_t m = xbt_new0(s_smx_mutex_t,1);
23 s_smx_process_t p; /* useful to initialize sleeping swag */
24 /* structures initialization */
26 m->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
30 void SIMIX_mutex_lock(smx_mutex_t mutex)
32 smx_process_t self = SIMIX_process_self();
34 xbt_assert0((mutex != NULL), "Invalid parameters");
37 /* somebody using the mutex, block */
38 xbt_swag_insert(self, mutex->sleeping);
39 self->simdata->mutex = mutex;
40 __SIMIX_process_block(-1,"");
41 self->simdata->mutex = NULL;
50 /* return 1 if the process got the mutex, else 0. */
51 int SIMIX_mutex_trylock(smx_mutex_t mutex)
53 xbt_assert0((mutex != NULL), "Invalid parameters");
63 void SIMIX_mutex_unlock(smx_mutex_t mutex)
65 smx_process_t p; /*process to wake up */
67 xbt_assert0((mutex != NULL), "Invalid parameters");
69 if (xbt_swag_size(mutex->sleeping) > 0) {
70 p = xbt_swag_extract(mutex->sleeping);
72 __SIMIX_process_unblock(p);
75 /* nobody to wape up */
81 SIMIX_error_t SIMIX_mutex_destroy(smx_mutex_t mutex)
86 xbt_swag_free(mutex->sleeping);
92 /******************************** Conditional *********************************/
93 smx_cond_t SIMIX_cond_init()
95 return xbt_new0(s_smx_cond_t,1);
98 void SIMIX_cond_signal(smx_cond_t cond)
103 void SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex)
108 void SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration)
113 void SIMIX_cond_broadcast(smx_cond_t cond)
118 void SIMIX_cond_destroy(smx_cond_t cond)