Logo AND Algorithmique Numérique Distribuée

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