Logo AND Algorithmique Numérique Distribuée

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