Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
if deadlock, close trace file so it may help to find the problem
[simgrid.git] / src / simix / smx_synchro.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "xbt/log.h"
9
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
12                                 "Logging specific to SIMIX (synchronization)");
13
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_req_t req);
18 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
19                             smx_req_t req);
20 static void SIMIX_sem_block_onto(smx_sem_t sem);
21
22 /***************************** Synchro action *********************************/
23
24 static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
25 {
26   smx_action_t action;
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->host, timeout);
32
33   surf_workstation_model->action_data_set(action->synchro.sleep, action);
34   return action;
35 }
36
37 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_req_t req)
38 {
39   switch (req->call) {
40
41     case REQ_MUTEX_LOCK:
42       xbt_swag_remove(process, req->mutex_lock.mutex->sleeping);
43       break;
44
45     case REQ_COND_WAIT:
46       xbt_swag_remove(process, req->cond_wait.cond->sleeping);
47       break;
48
49     case REQ_COND_WAIT_TIMEOUT:
50       xbt_swag_remove(process, req->cond_wait_timeout.cond->sleeping);
51       break;
52
53     case REQ_SEM_ACQUIRE:
54       xbt_swag_remove(process, req->sem_acquire.sem->sleeping);
55       break;
56
57     case REQ_SEM_ACQUIRE_TIMEOUT:
58       xbt_swag_remove(process, req->sem_acquire_timeout.sem->sleeping);
59       break;
60
61     default:
62       THROW_IMPOSSIBLE;
63   }
64 }
65
66 void SIMIX_synchro_destroy(smx_action_t action)
67 {
68   XBT_DEBUG("Destroying synchro %p", action);
69   action->synchro.sleep->model_type->action_unref(action->synchro.sleep);
70   xbt_free(action->name);
71   xbt_mallocator_release(simix_global->action_mallocator, action);
72 }
73
74 void SIMIX_post_synchro(smx_action_t action)
75 {
76   if (surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_FAILED)
77     action->state = SIMIX_FAILED;
78   else if(surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_DONE)
79     action->state = SIMIX_SRC_TIMEOUT;
80
81   SIMIX_synchro_finish(action);  
82 }
83
84 static void SIMIX_synchro_finish(smx_action_t action)
85 {
86   smx_req_t req = xbt_fifo_shift(action->request_list);
87
88   switch (action->state) {
89
90     case SIMIX_SRC_TIMEOUT:
91       TRY {
92         THROWF(timeout_error, 0, "Synchro's wait timeout");
93       } CATCH(req->issuer->running_ctx->exception) {
94         req->issuer->doexception = 1;
95       }
96       break;
97
98     case SIMIX_FAILED:
99       TRY {
100         THROWF(host_error, 0, "Host failed");
101       } CATCH(req->issuer->running_ctx->exception) {
102         req->issuer->doexception = 1;
103       }
104       break;
105
106     default:
107       THROW_IMPOSSIBLE;
108       break;
109   }
110
111   SIMIX_synchro_stop_waiting(req->issuer, req);
112   SIMIX_synchro_destroy(action);
113   SIMIX_request_answer(req);
114 }
115 /*********************************** Mutex ************************************/
116
117 /**
118  * \brief Initialize a mutex.
119  *
120  * Allocs and creates the data for the mutex.
121  * \return A mutex
122  */
123 smx_mutex_t SIMIX_mutex_init(void)
124 {
125   s_smx_process_t p;            /* useful to initialize sleeping swag */
126
127   smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
128   mutex->locked = 0;
129   mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
130   return mutex;
131 }
132
133 /**
134  * \brief Handle mutex lock request
135  * \param req The request
136  */
137 void SIMIX_pre_mutex_lock(smx_req_t req)
138 {
139   /* FIXME: check where to validate the arguments */
140   smx_action_t sync_act = NULL;
141   smx_mutex_t mutex = req->mutex_lock.mutex;
142   smx_process_t process = req->issuer;
143
144   if (mutex->locked) {
145     /* FIXME: check if the host is active ? */
146     /* Somebody using the mutex, use a synchro action to get host failures */
147     sync_act = SIMIX_synchro_wait(process->smx_host, -1);
148     xbt_fifo_push(sync_act->request_list, req);
149     req->issuer->waiting_action = sync_act;
150     xbt_swag_insert(req->issuer, mutex->sleeping);   
151   } else {
152     /* mutex free */
153     mutex->locked = 1;
154     mutex->owner = req->issuer;
155     SIMIX_request_answer(req);
156   }
157 }
158
159 /**
160  * \brief Tries to lock a mutex.
161  *
162  * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
163  * This function does not block and wait for the mutex to be unlocked.
164  * \param mutex The mutex
165  * \param issuer The process that tries to acquire the mutex
166  * \return 1 - mutex free, 0 - mutex used
167  */
168 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
169 {
170   if (mutex->locked)
171     return 0;
172
173   mutex->locked = 1;
174   mutex->owner = issuer;
175   return 1;
176 }
177
178 /**
179  * \brief Unlocks a mutex.
180  *
181  * Unlocks the mutex and gives it to a process waiting for it. 
182  * If the unlocker is not the owner of the mutex nothing happens.
183  * If there are no process waiting, it sets the mutex as free.
184  * \param mutex The mutex
185  * \param issuer The process trying to unlock the mutex
186  */
187 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
188 {
189   smx_process_t p;              /*process to wake up */
190
191   /* If the mutex is not owned by the issuer do nothing */
192   if (issuer != mutex->owner)
193     return;
194
195   if (xbt_swag_size(mutex->sleeping) > 0) {
196     p = xbt_swag_extract(mutex->sleeping);
197     SIMIX_synchro_destroy(p->waiting_action);
198     p->waiting_action = NULL;
199     mutex->owner = p;
200     SIMIX_request_answer(&p->request);
201   } else {
202     /* nobody to wake up */
203     mutex->locked = 0;
204     mutex->owner = NULL;
205   }
206 }
207
208 /**
209  * \brief Destroys a mutex.
210  *
211  * Destroys and frees the mutex's memory. 
212  * \param mutex A mutex
213  */
214 void SIMIX_mutex_destroy(smx_mutex_t mutex)
215 {
216   if (mutex){
217     xbt_swag_free(mutex->sleeping);
218     xbt_free(mutex);
219   }
220 }
221
222 /********************************* Condition **********************************/
223
224 /**
225  * \brief Initialize a condition.
226  *
227  * Allocates and creates the data for the condition.
228  * It have to be called before the use of the condition.
229  * \return A condition
230  */
231 smx_cond_t SIMIX_cond_init()
232 {
233   s_smx_process_t p;
234   smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
235   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
236   cond->mutex = NULL;
237   return cond;
238 }
239
240 /**
241  * \brief Handle condition waiting requests without timeouts
242  * \param The request
243  */
244 void SIMIX_pre_cond_wait(smx_req_t req)
245 {
246   smx_process_t issuer = req->issuer;
247   smx_cond_t cond = req->cond_wait.cond;
248   smx_mutex_t mutex = req->cond_wait.mutex;
249
250   _SIMIX_cond_wait(cond, mutex, -1, issuer, req);
251 }
252
253 /**
254  * \brief Handle condition waiting requests with timeouts
255  * \param The request
256  */
257 void SIMIX_pre_cond_wait_timeout(smx_req_t req)
258 {
259   smx_process_t issuer = req->issuer;
260   smx_cond_t cond = req->cond_wait_timeout.cond;
261   smx_mutex_t mutex = req->cond_wait_timeout.mutex;
262   double timeout = req->cond_wait_timeout.timeout;
263
264   _SIMIX_cond_wait(cond, mutex, timeout, issuer, req);
265 }
266
267
268 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
269                              smx_process_t issuer, smx_req_t req)
270 {
271   smx_action_t sync_act = NULL;
272
273   XBT_DEBUG("Wait condition %p", cond);
274
275   /* If there is a mutex unlock it */
276   /* FIXME: what happens if the issuer is not the owner of the mutex? */
277   if (mutex != NULL) {
278     cond->mutex = mutex;
279     SIMIX_mutex_unlock(mutex, issuer);
280   }
281
282   sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
283   xbt_fifo_unshift(sync_act->request_list, req);
284   issuer->waiting_action = sync_act;
285   xbt_swag_insert(req->issuer, cond->sleeping);   
286 }
287
288 /**
289  * \brief Signalizes a condition.
290  *
291  * Signalizes a condition and wakes up a sleeping process. 
292  * If there are no process sleeping, no action is done.
293  * \param cond A condition
294  */
295 void SIMIX_cond_signal(smx_cond_t cond)
296 {
297   smx_process_t proc = NULL;
298   smx_mutex_t mutex = NULL;
299   smx_req_t req = NULL;
300
301   XBT_DEBUG("Signal condition %p", cond);
302
303   /* If there are processes waiting for the condition choose one and try 
304      to make it acquire the mutex */
305   if ((proc = xbt_swag_extract(cond->sleeping))) {
306
307     /* Destroy waiter's synchro action */
308     SIMIX_synchro_destroy(proc->waiting_action);
309     proc->waiting_action = NULL;
310
311     /* Now transform the cond wait request into a mutex lock one */
312     req = &proc->request;
313     if(req->call == REQ_COND_WAIT)
314       mutex = req->cond_wait.mutex;
315     else
316       mutex = req->cond_wait_timeout.mutex;
317
318     req->call = REQ_MUTEX_LOCK;
319     req->mutex_lock.mutex = mutex;
320
321     SIMIX_pre_mutex_lock(req);
322   }
323 }
324
325 /**
326  * \brief Broadcasts a condition.
327  *
328  * Signal ALL processes waiting on a condition.
329  * If there are no process waiting, no action is done.
330  * \param cond A condition
331  */
332 void SIMIX_cond_broadcast(smx_cond_t cond)
333 {
334   XBT_DEBUG("Broadcast condition %p", cond);
335
336   /* Signal the condition until nobody is waiting on it */
337   while (xbt_swag_size(cond->sleeping)) {
338     SIMIX_cond_signal(cond);
339   }
340 }
341
342 /**
343  * \brief Destroys a contidion.
344  *
345  * Destroys and frees the condition's memory. 
346  * \param cond A condition
347  */
348 void SIMIX_cond_destroy(smx_cond_t cond)
349 {
350   XBT_DEBUG("Destroy condition %p", cond);
351
352   if (cond != NULL) {
353     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
354                 "Cannot destroy conditional since someone is still using it");
355
356     xbt_swag_free(cond->sleeping);
357     xbt_free(cond);
358   }
359 }
360
361 /******************************** Semaphores **********************************/
362 #define SMX_SEM_NOLIMIT 99999
363 /** @brief Initialize a semaphore */
364 smx_sem_t SIMIX_sem_init(unsigned int value)
365 {
366   s_smx_process_t p;
367
368   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
369   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
370   sem->value = value;
371   return sem;
372 }
373
374 /** @brief Destroys a semaphore */
375 void SIMIX_sem_destroy(smx_sem_t sem)
376 {
377   XBT_DEBUG("Destroy semaphore %p", sem);
378   if (sem != NULL) {
379     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
380                 "Cannot destroy semaphore since someone is still using it");
381     xbt_swag_free(sem->sleeping);
382     xbt_free(sem);
383   }
384 }
385
386 /** @brief release the semaphore
387  *
388  * Unlock a process waiting on the semaphore.
389  * If no one was blocked, the semaphore capacity is increased by 1.
390  */
391 void SIMIX_sem_release(smx_sem_t sem)
392 {
393   smx_process_t proc;
394
395   XBT_DEBUG("Sem release semaphore %p", sem);
396   if ((proc = xbt_swag_extract(sem->sleeping))) {
397     proc = xbt_swag_extract(sem->sleeping);
398     SIMIX_synchro_destroy(proc->waiting_action);
399     proc->waiting_action = NULL;
400     SIMIX_request_answer(&proc->request);
401   } else if (sem->value < SMX_SEM_NOLIMIT) {
402     sem->value++;
403   }
404 }
405
406 /** @brief Returns true if acquiring this semaphore would block */
407 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
408 {
409   return (sem->value <= 0);
410 }
411
412 /** @brief Returns the current capacity of the semaphore */
413 int SIMIX_sem_get_capacity(smx_sem_t sem)
414 {
415   return sem->value;
416 }
417
418 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
419                             smx_req_t req)
420 {
421   smx_action_t sync_act = NULL;
422
423   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
424   if (sem->value <= 0) {
425     sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
426     xbt_fifo_unshift(sync_act->request_list, req);
427     issuer->waiting_action = sync_act;
428     xbt_swag_insert(issuer, sem->sleeping);
429   } else {
430     sem->value--;
431     SIMIX_request_answer(req);
432   }
433 }
434
435 /**
436  * \brief Handle sem acquire requests without timeouts
437  */
438 void SIMIX_pre_sem_acquire(smx_req_t req)
439 {
440   _SIMIX_sem_wait(req->sem_acquire.sem, -1, req->issuer, req);
441 }
442
443 /**
444  * \brief Handle sem acquire requests with timeouts
445  */
446 void SIMIX_pre_sem_acquire_timeout(smx_req_t req)
447 {
448   _SIMIX_sem_wait(req->sem_acquire_timeout.sem,
449                   req->sem_acquire_timeout.timeout, req->issuer, req);  
450 }