Logo AND Algorithmique Numérique Distribuée

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