Logo AND Algorithmique Numérique Distribuée

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