Logo AND Algorithmique Numérique Distribuée

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