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 void SIMIX_mutex_destroy(smx_mutex_t mutex)
86 xbt_swag_free(mutex->sleeping);
92 /******************************** Conditional *********************************/
93 smx_cond_t SIMIX_cond_init()
95 smx_cond_t cond = xbt_new0(s_smx_cond_t,1);
98 cond->sleeping = xbt_swag_new(xbt_swag_offset(p,synchro_hookup));
99 cond->actions = xbt_fifo_new();
104 void SIMIX_cond_signal(smx_cond_t cond)
106 xbt_assert0((cond != NULL), "Invalid parameters");
107 smx_process_t proc = NULL;
109 if (xbt_swag_size(cond->sleeping) >= 1) {
110 proc = xbt_swag_extract(cond->sleeping);
111 __SIMIX_process_unblock(proc);
117 void SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex)
119 /* call the function with timeout, with max_duration > 0
120 * the process is blocked forever */
121 SIMIX_cond_wait_timeout(cond, mutex, -1);
125 void SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration)
127 smx_process_t self = SIMIX_process_self();
129 xbt_assert0((mutex != NULL), "Invalid parameters");
132 self->simdata->cond = cond;
136 SIMIX_mutex_unlock(mutex);
137 xbt_swag_insert(self, cond->sleeping);
138 /* creates a new action to be the timeout */
139 if (max_duration >=0) {
140 __SIMIX_process_block(max_duration);
141 self->simdata->cond = NULL;
144 __SIMIX_process_block(-1);
145 self->simdata->cond = NULL;
147 /* get the mutex again */
148 SIMIX_mutex_lock(cond->mutex);
154 void SIMIX_cond_broadcast(smx_cond_t cond)
156 xbt_assert0((cond != NULL), "Invalid parameters");
157 smx_process_t proc = NULL;
158 smx_process_t proc_next = NULL;
160 xbt_swag_foreach_safe(proc,proc_next,cond->sleeping) {
161 __SIMIX_process_unblock(proc);
162 xbt_swag_remove(proc,cond->sleeping);
168 void SIMIX_cond_destroy(smx_cond_t cond)
174 xbt_assert0( xbt_swag_size(cond->sleeping) == 0 , "Cannot destroy conditional");
176 xbt_swag_free(cond->sleeping);
177 xbt_fifo_free(cond->actions);
183 void SIMIX_register_condition_to_action(smx_action_t action, smx_cond_t cond)
185 xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
187 xbt_fifo_push(action->simdata->cond_list,cond);