Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3937fd092b894c0305bcb5848cd3e49b24f248b0
[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 /**
172  * \brief Tries to lock a mutex.
173  *
174  * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
175  * This function does not block and wait for the mutex to be unlocked.
176  * \param mutex The mutex
177  * \param issuer The process that tries to acquire the mutex
178  * \return 1 - mutex free, 0 - mutex used
179  */
180 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
181 {
182   XBT_IN("(%p, %p)",mutex,issuer);
183   if (mutex->locked){
184     XBT_OUT();
185     return 0;
186   }
187
188   mutex->locked = 1;
189   mutex->owner = issuer;
190   XBT_OUT();
191   return 1;
192 }
193
194 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
195    SIMIX_mutex_unlock(mutex, simcall->issuer);
196 }
197 /**
198  * \brief Unlocks a mutex.
199  *
200  * Unlocks the mutex and gives it to a process waiting for it. 
201  * If the unlocker is not the owner of the mutex nothing happens.
202  * If there are no process waiting, it sets the mutex as free.
203  * \param mutex The mutex
204  * \param issuer The process trying to unlock the mutex
205  */
206 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
207 {
208   XBT_IN("(%p, %p)",mutex,issuer);
209   smx_process_t p;              /*process to wake up */
210
211   /* If the mutex is not owned by the issuer do nothing */
212   if (issuer != mutex->owner){
213     XBT_OUT();
214     return;
215   }
216
217   if (xbt_swag_size(mutex->sleeping) > 0) {
218     p = xbt_swag_extract(mutex->sleeping);
219     SIMIX_synchro_destroy(p->waiting_synchro);
220     p->waiting_synchro = NULL;
221     mutex->owner = p;
222     SIMIX_simcall_answer(&p->simcall);
223   } else {
224     /* nobody to wake up */
225     mutex->locked = 0;
226     mutex->owner = NULL;
227   }
228   XBT_OUT();
229 }
230
231 void simcall_HANDLER_mutex_destroy(smx_simcall_t simcall, smx_mutex_t mutex){
232   SIMIX_mutex_destroy(mutex);
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 smx_cond_t simcall_HANDLER_cond_init(smx_simcall_t simcall){
253   return SIMIX_cond_init();
254 }
255 /**
256  * \brief Initialize a condition.
257  *
258  * Allocates and creates the data for the condition.
259  * It have to be called before the use of the condition.
260  * \return A condition
261  */
262 smx_cond_t SIMIX_cond_init(void)
263 {
264   XBT_IN("()");
265   s_smx_process_t p;
266   smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
267   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
268   cond->mutex = NULL;
269   XBT_OUT();
270   return cond;
271 }
272
273 /**
274  * \brief Handle a condition waiting simcall without timeouts
275  * \param simcall the simcall
276  */
277 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
278 {
279   XBT_IN("(%p)",simcall);
280   smx_process_t issuer = simcall->issuer;
281
282   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
283   XBT_OUT();
284 }
285
286 /**
287  * \brief Handle a condition waiting simcall with timeouts
288  * \param simcall the simcall
289  */
290 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
291                                  smx_mutex_t mutex, double timeout)
292 {
293   XBT_IN("(%p)",simcall);
294   smx_process_t issuer = simcall->issuer;
295
296   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
297   XBT_OUT();
298 }
299
300
301 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
302                              smx_process_t issuer, smx_simcall_t simcall)
303 {
304   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
305   smx_synchro_t synchro = NULL;
306
307   XBT_DEBUG("Wait condition %p", cond);
308
309   /* If there is a mutex unlock it */
310   /* FIXME: what happens if the issuer is not the owner of the mutex? */
311   if (mutex != NULL) {
312     cond->mutex = mutex;
313     SIMIX_mutex_unlock(mutex, issuer);
314   }
315
316   synchro = SIMIX_synchro_wait(issuer->smx_host, timeout);
317   xbt_fifo_unshift(synchro->simcalls, simcall);
318   issuer->waiting_synchro = synchro;
319   xbt_swag_insert(simcall->issuer, cond->sleeping);   
320   XBT_OUT();
321 }
322
323 void simcall_HANDLER_cond_signal(smx_simcall_t simcall, smx_cond_t cond){
324   SIMIX_cond_signal(cond);
325 }
326 /**
327  * \brief Signalizes a condition.
328  *
329  * Signalizes a condition and wakes up a sleeping process. 
330  * If there are no process sleeping, no action is done.
331  * \param cond A condition
332  */
333 void SIMIX_cond_signal(smx_cond_t cond)
334 {
335   XBT_IN("(%p)",cond);
336   smx_process_t proc = NULL;
337   smx_mutex_t mutex = NULL;
338   smx_simcall_t simcall = NULL;
339
340   XBT_DEBUG("Signal condition %p", cond);
341
342   /* If there are processes waiting for the condition choose one and try 
343      to make it acquire the mutex */
344   if ((proc = xbt_swag_extract(cond->sleeping))) {
345
346     /* Destroy waiter's synchronization */
347     SIMIX_synchro_destroy(proc->waiting_synchro);
348     proc->waiting_synchro = NULL;
349
350     /* Now transform the cond wait simcall into a mutex lock one */
351     simcall = &proc->simcall;
352     if(simcall->call == SIMCALL_COND_WAIT)
353       mutex = simcall_cond_wait__get__mutex(simcall);
354     else
355       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
356     simcall->call = SIMCALL_MUTEX_LOCK;
357
358     simcall_HANDLER_mutex_lock(simcall, mutex);
359   }
360   XBT_OUT();
361 }
362
363 void simcall_HANDLER_cond_broadcast(smx_simcall_t simcall, smx_cond_t cond){
364   SIMIX_cond_broadcast(cond);
365 }
366 /**
367  * \brief Broadcasts a condition.
368  *
369  * Signal ALL processes waiting on a condition.
370  * If there are no process waiting, no action is done.
371  * \param cond A condition
372  */
373 void SIMIX_cond_broadcast(smx_cond_t cond)
374 {
375   XBT_IN("(%p)",cond);
376   XBT_DEBUG("Broadcast condition %p", cond);
377
378   /* Signal the condition until nobody is waiting on it */
379   while (xbt_swag_size(cond->sleeping)) {
380     SIMIX_cond_signal(cond);
381   }
382   XBT_OUT();
383 }
384
385 void simcall_HANDLER_cond_destroy(smx_simcall_t simcall, smx_cond_t cond){
386   SIMIX_cond_destroy(cond);
387 }
388 /**
389  * \brief Destroys a contidion.
390  *
391  * Destroys and frees the condition's memory. 
392  * \param cond A condition
393  */
394 void SIMIX_cond_destroy(smx_cond_t cond)
395 {
396   XBT_IN("(%p)",cond);
397   XBT_DEBUG("Destroy condition %p", cond);
398
399   if (cond != NULL) {
400     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
401                 "Cannot destroy conditional since someone is still using it");
402
403     xbt_swag_free(cond->sleeping);
404     xbt_free(cond);
405   }
406   XBT_OUT();
407 }
408
409 /******************************** Semaphores **********************************/
410 #define SMX_SEM_NOLIMIT 99999
411 smx_sem_t simcall_HANDLER_sem_init(smx_simcall_t simcall, unsigned int value){
412   return SIMIX_sem_init(value);
413 }
414 /** @brief Initialize a semaphore */
415 smx_sem_t SIMIX_sem_init(unsigned int value)
416 {
417   XBT_IN("(%u)",value);
418   s_smx_process_t p;
419
420   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
421   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
422   sem->value = value;
423   XBT_OUT();
424   return sem;
425 }
426
427 void simcall_HANDLER_sem_destroy(smx_simcall_t simcall, smx_sem_t sem){
428   SIMIX_sem_destroy(sem);
429 }
430 /** @brief Destroys a semaphore */
431 void SIMIX_sem_destroy(smx_sem_t sem)
432 {
433   XBT_IN("(%p)",sem);
434   XBT_DEBUG("Destroy semaphore %p", sem);
435   if (sem != NULL) {
436     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
437                 "Cannot destroy semaphore since someone is still using it");
438     xbt_swag_free(sem->sleeping);
439     xbt_free(sem);
440   }
441   XBT_OUT();
442 }
443
444 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
445   SIMIX_sem_release(sem);
446 }
447 /** @brief release the semaphore
448  *
449  * Unlock a process waiting on the semaphore.
450  * If no one was blocked, the semaphore capacity is increased by 1.
451  */
452 void SIMIX_sem_release(smx_sem_t sem)
453 {
454   XBT_IN("(%p)",sem);
455   smx_process_t proc;
456
457   XBT_DEBUG("Sem release semaphore %p", sem);
458   if ((proc = xbt_swag_extract(sem->sleeping))) {
459     SIMIX_synchro_destroy(proc->waiting_synchro);
460     proc->waiting_synchro = NULL;
461     SIMIX_simcall_answer(&proc->simcall);
462   } else if (sem->value < SMX_SEM_NOLIMIT) {
463     sem->value++;
464   }
465   XBT_OUT();
466 }
467
468 /** @brief Returns true if acquiring this semaphore would block */
469 int SIMIX_sem_would_block(smx_sem_t sem)
470 {
471   XBT_IN("(%p)",sem);
472   XBT_OUT();
473   return (sem->value <= 0);
474 }
475
476 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
477   return SIMIX_sem_get_capacity(sem);
478 }
479 /** @brief Returns the current capacity of the semaphore */
480 int SIMIX_sem_get_capacity(smx_sem_t sem)
481 {
482   XBT_IN("(%p)",sem);
483   XBT_OUT();
484   return sem->value;
485 }
486
487 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
488                             smx_simcall_t simcall)
489 {
490   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
491   smx_synchro_t synchro = NULL;
492
493   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
494   if (sem->value <= 0) {
495     synchro = SIMIX_synchro_wait(issuer->smx_host, timeout);
496     xbt_fifo_unshift(synchro->simcalls, simcall);
497     issuer->waiting_synchro = synchro;
498     xbt_swag_insert(issuer, sem->sleeping);
499   } else {
500     sem->value--;
501     SIMIX_simcall_answer(simcall);
502   }
503   XBT_OUT();
504 }
505
506 /**
507  * \brief Handles a sem acquire simcall without timeout.
508  * \param simcall the simcall
509  */
510 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
511 {
512   XBT_IN("(%p)",simcall);
513   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
514   XBT_OUT();
515 }
516
517 /**
518  * \brief Handles a sem acquire simcall with timeout.
519  * \param simcall the simcall
520  */
521 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
522 {
523   XBT_IN("(%p)",simcall);
524   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);  
525   XBT_OUT();
526 }
527 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
528   return SIMIX_sem_would_block(sem);
529 }