Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8dd790cc53adc23d11002c072879a3ea61f3fc7e
[simgrid.git] / src / simix / smx_synchro.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4    All rights reserved.                                          */
5
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. */
8
9 #include "private.h"
10 #include "xbt/log.h"
11
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
14                                 "Logging specific to SIMIX (synchronization)");
15
16
17 /****************************** Synchronization *******************************/
18
19 /*********************************** Mutex ************************************/
20
21 /**
22  * \brief Initialize a mutex.
23  *
24  * Allocs and creates the data for the mutex. It have to be called before the utilisation of the mutex.
25  * \return A mutex
26  */
27 smx_mutex_t SIMIX_mutex_init()
28 {
29   smx_mutex_t m = xbt_new0(s_smx_mutex_t, 1);
30   s_smx_process_t p;            /* useful to initialize sleeping swag */
31   /* structures initialization */
32   m->refcount = 0;
33   m->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
34   return m;
35 }
36
37 /**
38  * \brief Locks a mutex.
39  *
40  * 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.
41  * \param mutex The mutex
42  */
43 void SIMIX_mutex_lock(smx_mutex_t mutex)
44 {
45   smx_process_t self = SIMIX_process_self();
46   xbt_assert0((mutex != NULL), "Invalid parameters");
47
48
49   if (mutex->refcount) {
50     /* somebody using the mutex, block */
51     xbt_swag_insert(self, mutex->sleeping);
52     self->mutex = mutex;
53     /* wait for some process make the unlock and wake up me from mutex->sleeping */
54     SIMIX_process_yield();
55     self->mutex = NULL;
56
57     /* verify if the process was suspended */
58     while (self->suspended) {
59       SIMIX_process_yield();
60     }
61
62     mutex->refcount = 1;
63   } else {
64     /* mutex free */
65     mutex->refcount = 1;
66   }
67   return;
68 }
69
70 /**
71  * \brief Tries to lock a mutex.
72  *
73  * 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.
74  * \param mutex The mutex
75  * \return 1 - mutex free, 0 - mutex used
76  */
77 int SIMIX_mutex_trylock(smx_mutex_t mutex)
78 {
79   xbt_assert0((mutex != NULL), "Invalid parameters");
80
81   if (mutex->refcount)
82     return 0;
83   else {
84     mutex->refcount = 1;
85     return 1;
86   }
87 }
88
89 /**
90  * \brief Unlocks a mutex.
91  *
92  * Unlocks the mutex and wakes up a process blocked on it. If there are no process sleeping, only sets the mutex as free.
93  * \param mutex The mutex
94  */
95 void SIMIX_mutex_unlock(smx_mutex_t mutex)
96 {
97   smx_process_t p;              /*process to wake up */
98
99   xbt_assert0((mutex != NULL), "Invalid parameters");
100
101   if (xbt_swag_size(mutex->sleeping) > 0) {
102     p = xbt_swag_extract(mutex->sleeping);
103     mutex->refcount = 0;
104     xbt_swag_insert(p, simix_global->process_to_run);
105   } else {
106     /* nobody to wake up */
107     mutex->refcount = 0;
108   }
109   return;
110 }
111
112 /**
113  * \brief Destroys a mutex.
114  *
115  * Destroys and frees the mutex's memory. 
116  * \param mutex A mutex
117  */
118 void SIMIX_mutex_destroy(smx_mutex_t mutex)
119 {
120   if (mutex == NULL)
121     return;
122   else {
123     xbt_swag_free(mutex->sleeping);
124     xbt_free(mutex);
125     return;
126   }
127 }
128
129 /******************************** Conditional *********************************/
130
131 /**
132  * \brief Initialize a condition.
133  *
134  * Allocs and creates the data for the condition. It have to be called before the utilisation of the condition.
135  * \return A condition
136  */
137 smx_cond_t SIMIX_cond_init()
138 {
139   smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
140   s_smx_process_t p;
141
142   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
143   cond->actions = xbt_fifo_new();
144   cond->mutex = NULL;
145   return cond;
146 }
147
148 /**
149  * \brief Signalizes a condition.
150  *
151  * Signalizes a condition and wakes up a sleeping process. If there are no process sleeping, no action is done.
152  * \param cond A condition
153  */
154 void SIMIX_cond_signal(smx_cond_t cond)
155 {
156   smx_process_t proc = NULL;
157   DEBUG1("Signal condition %p", cond);
158   xbt_assert0((cond != NULL), "Invalid parameters");
159
160
161   if (xbt_swag_size(cond->sleeping) >= 1) {
162     proc = xbt_swag_extract(cond->sleeping);
163     xbt_swag_insert(proc, simix_global->process_to_run);
164   }
165
166   return;
167 }
168
169 /**
170  * \brief Waits on a condition.
171  *
172  * Blocks a process until the signal is called. This functions frees the mutex associated and locks it after its execution.
173  * \param cond A condition
174  * \param mutex A mutex
175  */
176 void SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
177 {
178   smx_action_t act_sleep;
179
180   DEBUG1("Wait condition %p", cond);
181
182   /* If there is a mutex unlock it */
183   if(mutex != NULL){
184     cond->mutex = mutex;
185     SIMIX_mutex_unlock(mutex);
186   }
187   
188   /* Always create an action null in case there is a host failure */
189   act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
190   SIMIX_action_set_name(act_sleep,bprintf("Wait condition %p", cond));
191   SIMIX_process_self()->waiting_action = act_sleep;
192   SIMIX_register_action_to_condition(act_sleep, cond);
193   __SIMIX_cond_wait(cond);
194   SIMIX_process_self()->waiting_action = NULL;
195   SIMIX_unregister_action_to_condition(act_sleep, cond);
196   SIMIX_action_destroy(act_sleep);
197
198   /* get the mutex again if necessary */
199   if(mutex != NULL)
200     SIMIX_mutex_lock(cond->mutex);
201
202   return;
203 }
204
205 xbt_fifo_t SIMIX_cond_get_actions(smx_cond_t cond)
206 {
207   xbt_assert0((cond != NULL), "Invalid parameters");
208   return cond->actions;
209 }
210
211 void __SIMIX_cond_wait(smx_cond_t cond)
212 {
213   smx_process_t self = SIMIX_process_self();
214   xbt_assert0((cond != NULL), "Invalid parameters");
215
216   /* process status */
217
218   self->cond = cond;
219   xbt_swag_insert(self, cond->sleeping);
220   SIMIX_process_yield();
221   self->cond = NULL;
222   while (self->suspended) {
223     SIMIX_process_yield();
224   }
225   return;
226 }
227
228 /**
229  * \brief Waits on a condition with timeout.
230  *
231  * Same behavior of #SIMIX_cond_wait, but waits a maximum time and throws an timeout_error if it happens.
232  * \param cond A condition
233  * \param mutex A mutex
234  * \param max_duration Timeout time
235  */
236 void SIMIX_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex,
237                              double max_duration)
238 {
239   smx_action_t act_sleep;
240
241   DEBUG1("Timed wait condition %p", cond);
242
243   /* If there is a mutex unlock it */
244   if(mutex != NULL){
245     cond->mutex = mutex;
246     SIMIX_mutex_unlock(mutex);
247   }
248
249   if (max_duration >= 0) {
250     act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
251     SIMIX_action_set_name(act_sleep,bprintf("Timed wait condition %p (max_duration:%f)", cond,max_duration));
252     SIMIX_register_action_to_condition(act_sleep, cond);
253     SIMIX_process_self()->waiting_action = act_sleep;
254     __SIMIX_cond_wait(cond);
255     SIMIX_process_self()->waiting_action = NULL;
256     SIMIX_unregister_action_to_condition(act_sleep, cond);
257     if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
258       SIMIX_action_destroy(act_sleep);
259       THROW1(timeout_error, 0, "Condition timeout after %f",max_duration);
260     } else {
261       SIMIX_action_destroy(act_sleep);
262     }
263
264   } else
265     __SIMIX_cond_wait(cond);
266
267   /* get the mutex again if necessary */
268   if(mutex != NULL)
269     SIMIX_mutex_lock(cond->mutex);
270
271   return;
272 }
273
274 /**
275  * \brief Broadcasts a condition.
276  *
277  * Signalizes a condition and wakes up ALL sleping process. If there are no process sleeping, no action is done.
278  * \param cond A condition
279  */
280 void SIMIX_cond_broadcast(smx_cond_t cond)
281 {
282   smx_process_t proc = NULL;
283   smx_process_t proc_next = NULL;
284
285   xbt_assert0((cond != NULL), "Invalid parameters");
286
287   DEBUG1("Broadcast condition %p", cond);
288   xbt_swag_foreach_safe(proc, proc_next, cond->sleeping) {
289     xbt_swag_remove(proc, cond->sleeping);
290     xbt_swag_insert(proc, simix_global->process_to_run);
291   }
292
293   return;
294 }
295
296 /**
297  * \brief Destroys a contidion.
298  *
299  * Destroys and frees the condition's memory. 
300  * \param cond A condition
301  */
302 void SIMIX_cond_destroy(smx_cond_t cond)
303 {
304   DEBUG1("Destroy condition %p", cond);
305   if (cond == NULL)
306     return;
307   else {
308     xbt_fifo_item_t item = NULL;
309     smx_action_t action = NULL;
310
311     xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
312                 "Cannot destroy conditional since someone is still using it");
313     xbt_swag_free(cond->sleeping);
314
315     DEBUG1("%d actions registered", xbt_fifo_size(cond->actions));
316     __SIMIX_cond_display_actions(cond);
317     xbt_fifo_foreach(cond->actions, item, action, smx_action_t) {
318       SIMIX_unregister_action_to_condition(action, cond);
319     }
320     __SIMIX_cond_display_actions(cond);
321
322     xbt_fifo_free(cond->actions);
323     xbt_free(cond);
324     return;
325   }
326 }
327
328 void SIMIX_cond_display_info(smx_cond_t cond)
329 {
330   if (cond == NULL)
331     return;
332   else {
333     smx_process_t process = NULL;
334
335     INFO0("Blocked process on this condition:");
336     xbt_swag_foreach(process, cond->sleeping) {
337       INFO2("\t %s running on host %s", process->name,
338             process->smx_host->name);
339     }
340   }
341 }