Logo AND Algorithmique Numérique Distribuée

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