Logo AND Algorithmique Numérique Distribuée

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