Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the synchronization stuff to its own directory
[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/synchro/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() : mutex_(this)
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 = new s_smx_cond();
242   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
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     xbt_swag_free(cond->sleeping);
386     delete cond;
387   }
388 }
389
390 /******************************** Semaphores **********************************/
391 #define SMX_SEM_NOLIMIT 99999
392 /** @brief Initialize a semaphore */
393 smx_sem_t SIMIX_sem_init(unsigned int value)
394 {
395   XBT_IN("(%u)",value);
396   simgrid::simix::Process p;
397
398   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
399   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
400   sem->value = value;
401   XBT_OUT();
402   return sem;
403 }
404
405 /** @brief Destroys a semaphore */
406 void SIMIX_sem_destroy(smx_sem_t sem)
407 {
408   XBT_IN("(%p)",sem);
409   XBT_DEBUG("Destroy semaphore %p", sem);
410   if (sem != nullptr) {
411     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
412                 "Cannot destroy semaphore since someone is still using it");
413     xbt_swag_free(sem->sleeping);
414     xbt_free(sem);
415   }
416   XBT_OUT();
417 }
418
419 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
420   SIMIX_sem_release(sem);
421 }
422 /** @brief release the semaphore
423  *
424  * Unlock a process waiting on the semaphore.
425  * If no one was blocked, the semaphore capacity is increased by 1.
426  */
427 void SIMIX_sem_release(smx_sem_t sem)
428 {
429   XBT_IN("(%p)",sem);
430   smx_process_t proc;
431
432   XBT_DEBUG("Sem release semaphore %p", sem);
433   if ((proc = (smx_process_t) xbt_swag_extract(sem->sleeping))) {
434     delete proc->waiting_synchro;
435     proc->waiting_synchro = nullptr;
436     SIMIX_simcall_answer(&proc->simcall);
437   } else if (sem->value < SMX_SEM_NOLIMIT) {
438     sem->value++;
439   }
440   XBT_OUT();
441 }
442
443 /** @brief Returns true if acquiring this semaphore would block */
444 int SIMIX_sem_would_block(smx_sem_t sem)
445 {
446   XBT_IN("(%p)",sem);
447   XBT_OUT();
448   return (sem->value <= 0);
449 }
450
451 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
452   return SIMIX_sem_get_capacity(sem);
453 }
454 /** @brief Returns the current capacity of the semaphore */
455 int SIMIX_sem_get_capacity(smx_sem_t sem)
456 {
457   XBT_IN("(%p)",sem);
458   XBT_OUT();
459   return sem->value;
460 }
461
462 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
463                             smx_simcall_t simcall)
464 {
465   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
466   smx_synchro_t synchro = nullptr;
467
468   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
469   if (sem->value <= 0) {
470     synchro = SIMIX_synchro_wait(issuer->host, timeout);
471     synchro->simcalls.push_front(simcall);
472     issuer->waiting_synchro = synchro;
473     xbt_swag_insert(issuer, sem->sleeping);
474   } else {
475     sem->value--;
476     SIMIX_simcall_answer(simcall);
477   }
478   XBT_OUT();
479 }
480
481 /**
482  * \brief Handles a sem acquire simcall without timeout.
483  * \param simcall the simcall
484  */
485 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
486 {
487   XBT_IN("(%p)",simcall);
488   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
489   XBT_OUT();
490 }
491
492 /**
493  * \brief Handles a sem acquire simcall with timeout.
494  * \param simcall the simcall
495  */
496 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
497 {
498   XBT_IN("(%p)",simcall);
499   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);  
500   XBT_OUT();
501 }
502 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
503   return SIMIX_sem_would_block(sem);
504 }