Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 "smx_private.h"
9 #include "src/surf/cpu_interface.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include <xbt/ex.hpp>
12 #include <xbt/log.h>
13
14 #include "src/kernel/activity/SynchroRaw.hpp"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
17
18 static smx_activity_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_actor_t issuer, smx_simcall_t simcall);
21 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
22                             smx_simcall_t simcall);
23
24 /***************************** Raw synchronization *********************************/
25
26 static smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
27 {
28   XBT_IN("(%p, %f)",smx_host,timeout);
29
30   simgrid::kernel::activity::Raw *sync = new simgrid::kernel::activity::Raw();
31   sync->sleep                          = smx_host->pimpl_cpu->sleep(timeout);
32   sync->sleep->setData(sync);
33   XBT_OUT();
34   return sync;
35 }
36
37 void SIMIX_synchro_stop_waiting(smx_actor_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_activity_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() : mutex_(this)
102 {
103   XBT_IN("(%p)", this);
104   // Useful to initialize sleeping swag:
105   simgrid::simix::ActorImpl 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_actor_t issuer)
118 {
119   XBT_IN("(%p; %p)", this, issuer);
120   /* FIXME: check where to validate the arguments */
121   smx_activity_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_actor_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_actor_t issuer)
165 {
166   XBT_IN("(%p, %p)", this, issuer);
167   if(!this->locked)
168     THROWF(mismatch_error, 0, "Cannot release that mutex: it was not locked.");
169
170   /* If the mutex is not owned by the issuer, that's not good */
171   if (issuer != this->owner)
172     THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%ld), not by you.",
173         this->owner->name.c_str(),this->owner->pid);
174
175   if (xbt_swag_size(this->sleeping) > 0) {
176     /*process to wake up */
177     smx_actor_t p = (smx_actor_t) xbt_swag_extract(this->sleeping);
178     delete p->waiting_synchro;
179     p->waiting_synchro = nullptr;
180     this->owner = p;
181     SIMIX_simcall_answer(&p->simcall);
182   } else {
183     /* nobody to wake up */
184     this->locked = false;
185     this->owner = nullptr;
186   }
187   XBT_OUT();
188 }
189
190 }
191 }
192
193 /** Increase the refcount for this mutex */
194 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
195 {
196   if (mutex != nullptr)
197     intrusive_ptr_add_ref(mutex);
198   return mutex;
199 }
200
201 /** Decrease the refcount for this mutex */
202 void SIMIX_mutex_unref(smx_mutex_t mutex)
203 {
204   if (mutex != nullptr)
205     intrusive_ptr_release(mutex);
206 }
207
208 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall)
209 {
210   return new simgrid::simix::Mutex();
211 }
212
213 // Simcall handlers:
214
215 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
216 {
217   mutex->lock(simcall->issuer);
218 }
219
220 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
221 {
222   return mutex->try_lock(simcall->issuer);
223 }
224
225 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
226 {
227   mutex->unlock(simcall->issuer);
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()
240 {
241   XBT_IN("()");
242   simgrid::simix::ActorImpl p;
243   smx_cond_t cond = new s_smx_cond();
244   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
245   cond->refcount_ = 1;
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_actor_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_actor_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_actor_t issuer, smx_simcall_t simcall)
280 {
281   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
282   smx_activity_t synchro = nullptr;
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 != nullptr) {
289     cond->mutex = mutex;
290     mutex->unlock(issuer);
291   }
292
293   synchro = SIMIX_synchro_wait(issuer->host, timeout);
294   synchro->simcalls.push_front(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_actor_t proc = nullptr;
311   smx_mutex_t mutex = nullptr;
312   smx_simcall_t simcall = nullptr;
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_actor_t) xbt_swag_extract(cond->sleeping))) {
319
320     /* Destroy waiter's synchronization */
321     delete proc->waiting_synchro;
322     proc->waiting_synchro = nullptr;
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 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
357 {
358   if (cond != nullptr)
359     intrusive_ptr_add_ref(cond);
360   return cond;
361 }
362
363 void SIMIX_cond_unref(smx_cond_t cond)
364 {
365   XBT_IN("(%p)",cond);
366   XBT_DEBUG("Destroy condition %p", cond);
367   if (cond != nullptr) {
368     intrusive_ptr_release(cond);
369   }
370   XBT_OUT();
371 }
372
373
374 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
375 {
376   auto previous = (cond->refcount_)++;
377   xbt_assert(previous != 0);
378   (void) previous;
379 }
380
381 void intrusive_ptr_release(s_smx_cond_t *cond)
382 {
383   auto count = --(cond->refcount_);
384   if (count == 0) {
385     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
386                 "Cannot destroy conditional since someone is still using it");
387     xbt_swag_free(cond->sleeping);
388     delete cond;
389   }
390 }
391
392 /******************************** Semaphores **********************************/
393 /** @brief Initialize a semaphore */
394 smx_sem_t SIMIX_sem_init(unsigned int value)
395 {
396   XBT_IN("(%u)",value);
397   simgrid::simix::ActorImpl 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_actor_t proc;
432
433   XBT_DEBUG("Sem release semaphore %p", sem);
434   if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
435     delete proc->waiting_synchro;
436     proc->waiting_synchro = nullptr;
437     SIMIX_simcall_answer(&proc->simcall);
438   } else {
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_actor_t issuer,
464                             smx_simcall_t simcall)
465 {
466   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
467   smx_activity_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 }