Logo AND Algorithmique Numérique Distribuée

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