Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mc-process
[simgrid.git] / src / simix / smx_synchro.c
1 /* Copyright (c) 2007-2014. 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                                 "SIMIX Synchronization (mutex, semaphores and conditions)");
13
14 static smx_synchro_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout);
15 static void SIMIX_synchro_finish(smx_synchro_t synchro);
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 /***************************** Raw synchronization *********************************/
22
23 static smx_synchro_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
24 {
25   XBT_IN("(%p, %f)",smx_host,timeout);
26
27   smx_synchro_t sync;
28   sync = xbt_mallocator_get(simix_global->synchro_mallocator);
29   sync->type = SIMIX_SYNC_SYNCHRO;
30   sync->name = xbt_strdup("synchro");
31   sync->synchro.sleep = 
32     surf_workstation_sleep(smx_host, timeout);
33
34   surf_action_set_data(sync->synchro.sleep, sync);
35   XBT_OUT();
36   return sync;
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_synchro_t synchro)
71 {
72   XBT_IN("(%p)",synchro);
73   XBT_DEBUG("Destroying synchro %p", synchro);
74   xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
75   surf_action_unref(synchro->synchro.sleep);
76   xbt_free(synchro->name);
77   xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
78   XBT_OUT();
79 }
80
81 void SIMIX_post_synchro(smx_synchro_t synchro)
82 {
83   XBT_IN("(%p)",synchro);
84   xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
85   if (surf_action_get_state(synchro->synchro.sleep) == SURF_ACTION_FAILED)
86     synchro->state = SIMIX_FAILED;
87   else if(surf_action_get_state(synchro->synchro.sleep) == SURF_ACTION_DONE)
88     synchro->state = SIMIX_SRC_TIMEOUT;
89
90   SIMIX_synchro_finish(synchro);  
91   XBT_OUT();
92 }
93
94 static void SIMIX_synchro_finish(smx_synchro_t synchro)
95 {
96   XBT_IN("(%p)",synchro);
97   smx_simcall_t simcall = xbt_fifo_shift(synchro->simcalls);
98
99   switch (synchro->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_synchro = NULL;
117   SIMIX_synchro_destroy(synchro);
118   SIMIX_simcall_answer(simcall);
119   XBT_OUT();
120 }
121 /*********************************** Mutex ************************************/
122
123 smx_mutex_t simcall_HANDLER_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 simcall_HANDLER_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_synchro_t synchro = 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 synchronization to get host failures */
158     synchro = SIMIX_synchro_wait(process->smx_host, -1);
159     xbt_fifo_push(synchro->simcalls, simcall);
160     simcall->issuer->waiting_synchro = synchro;
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 simcall_HANDLER_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 simcall_HANDLER_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_synchro);
223     p->waiting_synchro = 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 /**
235  * \brief Destroys a mutex.
236  *
237  * Destroys and frees the mutex's memory. 
238  * \param mutex A mutex
239  */
240 void SIMIX_mutex_destroy(smx_mutex_t mutex)
241 {
242   XBT_IN("(%p)",mutex);
243   if (mutex){
244     xbt_swag_free(mutex->sleeping);
245     xbt_free(mutex);
246   }
247   XBT_OUT();
248 }
249
250 /********************************* Condition **********************************/
251
252 /**
253  * \brief Initialize a condition.
254  *
255  * Allocates and creates the data for the condition.
256  * It have to be called before the use of the condition.
257  * \return A condition
258  */
259 smx_cond_t SIMIX_cond_init(void)
260 {
261   XBT_IN("()");
262   s_smx_process_t p;
263   smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
264   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
265   cond->mutex = NULL;
266   XBT_OUT();
267   return cond;
268 }
269
270 /**
271  * \brief Handle a condition waiting simcall without timeouts
272  * \param simcall the simcall
273  */
274 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
275 {
276   XBT_IN("(%p)",simcall);
277   smx_process_t issuer = simcall->issuer;
278
279   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
280   XBT_OUT();
281 }
282
283 /**
284  * \brief Handle a condition waiting simcall with timeouts
285  * \param simcall the simcall
286  */
287 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
288                                  smx_mutex_t mutex, double timeout)
289 {
290   XBT_IN("(%p)",simcall);
291   smx_process_t issuer = simcall->issuer;
292
293   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
294   XBT_OUT();
295 }
296
297
298 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
299                              smx_process_t issuer, smx_simcall_t simcall)
300 {
301   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
302   smx_synchro_t synchro = NULL;
303
304   XBT_DEBUG("Wait condition %p", cond);
305
306   /* If there is a mutex unlock it */
307   /* FIXME: what happens if the issuer is not the owner of the mutex? */
308   if (mutex != NULL) {
309     cond->mutex = mutex;
310     SIMIX_mutex_unlock(mutex, issuer);
311   }
312
313   synchro = SIMIX_synchro_wait(issuer->smx_host, timeout);
314   xbt_fifo_unshift(synchro->simcalls, simcall);
315   issuer->waiting_synchro = synchro;
316   xbt_swag_insert(simcall->issuer, cond->sleeping);   
317   XBT_OUT();
318 }
319
320 /**
321  * \brief Signalizes a condition.
322  *
323  * Signalizes a condition and wakes up a sleeping process. 
324  * If there are no process sleeping, no action is done.
325  * \param cond A condition
326  */
327 void SIMIX_cond_signal(smx_cond_t cond)
328 {
329   XBT_IN("(%p)",cond);
330   smx_process_t proc = NULL;
331   smx_mutex_t mutex = NULL;
332   smx_simcall_t simcall = NULL;
333
334   XBT_DEBUG("Signal condition %p", cond);
335
336   /* If there are processes waiting for the condition choose one and try 
337      to make it acquire the mutex */
338   if ((proc = xbt_swag_extract(cond->sleeping))) {
339
340     /* Destroy waiter's synchronization */
341     SIMIX_synchro_destroy(proc->waiting_synchro);
342     proc->waiting_synchro = NULL;
343
344     /* Now transform the cond wait simcall into a mutex lock one */
345     simcall = &proc->simcall;
346     if(simcall->call == SIMCALL_COND_WAIT)
347       mutex = simcall_cond_wait__get__mutex(simcall);
348     else
349       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
350     simcall->call = SIMCALL_MUTEX_LOCK;
351
352     simcall_HANDLER_mutex_lock(simcall, mutex);
353   }
354   XBT_OUT();
355 }
356
357 /**
358  * \brief Broadcasts a condition.
359  *
360  * Signal ALL processes waiting on a condition.
361  * If there are no process waiting, no action is done.
362  * \param cond A condition
363  */
364 void SIMIX_cond_broadcast(smx_cond_t cond)
365 {
366   XBT_IN("(%p)",cond);
367   XBT_DEBUG("Broadcast condition %p", cond);
368
369   /* Signal the condition until nobody is waiting on it */
370   while (xbt_swag_size(cond->sleeping)) {
371     SIMIX_cond_signal(cond);
372   }
373   XBT_OUT();
374 }
375
376 /**
377  * \brief Destroys a condition.
378  *
379  * Destroys and frees the condition's memory. 
380  * \param cond A condition
381  */
382 void SIMIX_cond_destroy(smx_cond_t cond)
383 {
384   XBT_IN("(%p)",cond);
385   XBT_DEBUG("Destroy condition %p", cond);
386
387   if (cond != NULL) {
388     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
389                 "Cannot destroy conditional since someone is still using it");
390
391     xbt_swag_free(cond->sleeping);
392     xbt_free(cond);
393   }
394   XBT_OUT();
395 }
396
397 /******************************** Semaphores **********************************/
398 #define SMX_SEM_NOLIMIT 99999
399 /** @brief Initialize a semaphore */
400 smx_sem_t SIMIX_sem_init(unsigned int value)
401 {
402   XBT_IN("(%u)",value);
403   s_smx_process_t p;
404
405   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
406   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
407   sem->value = value;
408   XBT_OUT();
409   return sem;
410 }
411
412 /** @brief Destroys a semaphore */
413 void SIMIX_sem_destroy(smx_sem_t sem)
414 {
415   XBT_IN("(%p)",sem);
416   XBT_DEBUG("Destroy semaphore %p", sem);
417   if (sem != NULL) {
418     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
419                 "Cannot destroy semaphore since someone is still using it");
420     xbt_swag_free(sem->sleeping);
421     xbt_free(sem);
422   }
423   XBT_OUT();
424 }
425
426 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
427   SIMIX_sem_release(sem);
428 }
429 /** @brief release the semaphore
430  *
431  * Unlock a process waiting on the semaphore.
432  * If no one was blocked, the semaphore capacity is increased by 1.
433  */
434 void SIMIX_sem_release(smx_sem_t sem)
435 {
436   XBT_IN("(%p)",sem);
437   smx_process_t proc;
438
439   XBT_DEBUG("Sem release semaphore %p", sem);
440   if ((proc = xbt_swag_extract(sem->sleeping))) {
441     SIMIX_synchro_destroy(proc->waiting_synchro);
442     proc->waiting_synchro = NULL;
443     SIMIX_simcall_answer(&proc->simcall);
444   } else if (sem->value < SMX_SEM_NOLIMIT) {
445     sem->value++;
446   }
447   XBT_OUT();
448 }
449
450 /** @brief Returns true if acquiring this semaphore would block */
451 int SIMIX_sem_would_block(smx_sem_t sem)
452 {
453   XBT_IN("(%p)",sem);
454   XBT_OUT();
455   return (sem->value <= 0);
456 }
457
458 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
459   return SIMIX_sem_get_capacity(sem);
460 }
461 /** @brief Returns the current capacity of the semaphore */
462 int SIMIX_sem_get_capacity(smx_sem_t sem)
463 {
464   XBT_IN("(%p)",sem);
465   XBT_OUT();
466   return sem->value;
467 }
468
469 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
470                             smx_simcall_t simcall)
471 {
472   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
473   smx_synchro_t synchro = NULL;
474
475   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
476   if (sem->value <= 0) {
477     synchro = SIMIX_synchro_wait(issuer->smx_host, timeout);
478     xbt_fifo_unshift(synchro->simcalls, simcall);
479     issuer->waiting_synchro = synchro;
480     xbt_swag_insert(issuer, sem->sleeping);
481   } else {
482     sem->value--;
483     SIMIX_simcall_answer(simcall);
484   }
485   XBT_OUT();
486 }
487
488 /**
489  * \brief Handles a sem acquire simcall without timeout.
490  * \param simcall the simcall
491  */
492 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
493 {
494   XBT_IN("(%p)",simcall);
495   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
496   XBT_OUT();
497 }
498
499 /**
500  * \brief Handles a sem acquire simcall with timeout.
501  * \param simcall the simcall
502  */
503 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
504 {
505   XBT_IN("(%p)",simcall);
506   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);  
507   XBT_OUT();
508 }
509 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
510   return SIMIX_sem_would_block(sem);
511 }