Logo AND Algorithmique Numérique Distribuée

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