Logo AND Algorithmique Numérique Distribuée

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