Logo AND Algorithmique Numérique Distribuée

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