Logo AND Algorithmique Numérique Distribuée

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