Logo AND Algorithmique Numérique Distribuée

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