Logo AND Algorithmique Numérique Distribuée

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