Logo AND Algorithmique Numérique Distribuée

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