Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change cname() to getCname() and add getName() in simix::ActorImpl.
[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
13 #include "src/kernel/activity/SynchroRaw.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
16
17 static smx_activity_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_actor_t issuer, smx_simcall_t simcall);
20 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
21                             smx_simcall_t simcall);
22
23 /***************************** Raw synchronization *********************************/
24
25 static smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
26 {
27   XBT_IN("(%p, %f)",smx_host,timeout);
28
29   simgrid::kernel::activity::RawImplPtr sync =
30       simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl());
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.get());
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       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   SIMIX_simcall_answer(simcall);
92   XBT_OUT();
93 }
94 /*********************************** Mutex ************************************/
95
96 namespace simgrid {
97 namespace simix {
98
99 MutexImpl::MutexImpl() : mutex_(this)
100 {
101   XBT_IN("(%p)", this);
102   // Useful to initialize sleeping swag:
103   simgrid::simix::ActorImpl p;
104   this->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
105   XBT_OUT();
106 }
107
108 MutexImpl::~MutexImpl()
109 {
110   XBT_IN("(%p)", this);
111   xbt_swag_free(this->sleeping);
112   XBT_OUT();
113 }
114
115 void MutexImpl::lock(smx_actor_t issuer)
116 {
117   XBT_IN("(%p; %p)", this, issuer);
118   /* FIXME: check where to validate the arguments */
119   smx_activity_t synchro = nullptr;
120
121   if (this->locked) {
122     /* FIXME: check if the host is active ? */
123     /* Somebody using the mutex, use a synchronization to get host failures */
124     synchro = SIMIX_synchro_wait(issuer->host, -1);
125     synchro->simcalls.push_back(&issuer->simcall);
126     issuer->waiting_synchro = synchro;
127     xbt_swag_insert(issuer, this->sleeping);
128   } else {
129     /* mutex free */
130     this->locked = true;
131     this->owner = issuer;
132     SIMIX_simcall_answer(&issuer->simcall);
133   }
134   XBT_OUT();
135 }
136
137 /** Tries to lock the mutex for a process
138  *
139  * \param  issuer  the process that tries to acquire the mutex
140  * \return whether we managed to lock the mutex
141  */
142 bool MutexImpl::try_lock(smx_actor_t issuer)
143 {
144   XBT_IN("(%p, %p)", this, issuer);
145   if (this->locked) {
146     XBT_OUT();
147     return false;
148   }
149
150   this->locked = true;
151   this->owner = issuer;
152   XBT_OUT();
153   return true;
154 }
155
156 /** Unlock a mutex for a process
157  *
158  * Unlocks the mutex and gives it to a process waiting for it.
159  * If the unlocker is not the owner of the mutex nothing happens.
160  * If there are no process waiting, it sets the mutex as free.
161  */
162 void MutexImpl::unlock(smx_actor_t issuer)
163 {
164   XBT_IN("(%p, %p)", this, issuer);
165   if (not this->locked)
166     THROWF(mismatch_error, 0, "Cannot release that mutex: it was not locked.");
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:%lu), not by you.",
171            this->owner->getCname(), this->owner->pid);
172
173   if (xbt_swag_size(this->sleeping) > 0) {
174     /*process to wake up */
175     smx_actor_t p = (smx_actor_t) xbt_swag_extract(this->sleeping);
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 // Simcall handlers:
206
207 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
208 {
209   mutex->lock(simcall->issuer);
210 }
211
212 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
213 {
214   return mutex->try_lock(simcall->issuer);
215 }
216
217 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
218 {
219   mutex->unlock(simcall->issuer);
220 }
221
222 /********************************* Condition **********************************/
223
224 /**
225  * \brief Initialize a condition.
226  *
227  * Allocates and creates the data for the condition.
228  * It have to be called before the use of the condition.
229  * \return A condition
230  */
231 smx_cond_t SIMIX_cond_init()
232 {
233   XBT_IN("()");
234   simgrid::simix::ActorImpl p;
235   smx_cond_t cond = new s_smx_cond_t();
236   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
237   cond->refcount_ = 1;
238   XBT_OUT();
239   return cond;
240 }
241
242 /**
243  * \brief Handle a condition waiting simcall without timeouts
244  * \param simcall the simcall
245  */
246 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
247 {
248   XBT_IN("(%p)",simcall);
249   smx_actor_t issuer = simcall->issuer;
250
251   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
252   XBT_OUT();
253 }
254
255 /**
256  * \brief Handle a condition waiting simcall with timeouts
257  * \param simcall the simcall
258  */
259 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
260                      smx_mutex_t mutex, double timeout)
261 {
262   XBT_IN("(%p)",simcall);
263   smx_actor_t issuer = simcall->issuer;
264
265   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
266   XBT_OUT();
267 }
268
269
270 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
271                              smx_actor_t issuer, smx_simcall_t simcall)
272 {
273   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
274   smx_activity_t synchro = nullptr;
275
276   XBT_DEBUG("Wait condition %p", cond);
277
278   /* If there is a mutex unlock it */
279   /* FIXME: what happens if the issuer is not the owner of the mutex? */
280   if (mutex != nullptr) {
281     cond->mutex = mutex;
282     mutex->unlock(issuer);
283   }
284
285   synchro = SIMIX_synchro_wait(issuer->host, timeout);
286   synchro->simcalls.push_front(simcall);
287   issuer->waiting_synchro = synchro;
288   xbt_swag_insert(simcall->issuer, cond->sleeping);
289   XBT_OUT();
290 }
291
292 /**
293  * \brief Signalizes a condition.
294  *
295  * Signalizes a condition and wakes up a sleeping process.
296  * If there are no process sleeping, no action is done.
297  * \param cond A condition
298  */
299 void SIMIX_cond_signal(smx_cond_t cond)
300 {
301   XBT_IN("(%p)",cond);
302   smx_actor_t proc = nullptr;
303   smx_mutex_t mutex = nullptr;
304   smx_simcall_t simcall = nullptr;
305
306   XBT_DEBUG("Signal condition %p", cond);
307
308   /* If there are processes waiting for the condition choose one and try
309      to make it acquire the mutex */
310   if ((proc = (smx_actor_t) xbt_swag_extract(cond->sleeping))) {
311
312     /* Destroy waiter's synchronization */
313     proc->waiting_synchro = nullptr;
314
315     /* Now transform the cond wait simcall into a mutex lock one */
316     simcall = &proc->simcall;
317     if(simcall->call == SIMCALL_COND_WAIT)
318       mutex = simcall_cond_wait__get__mutex(simcall);
319     else
320       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
321     simcall->call = SIMCALL_MUTEX_LOCK;
322
323     simcall_HANDLER_mutex_lock(simcall, mutex);
324   }
325   XBT_OUT();
326 }
327
328 /**
329  * \brief Broadcasts a condition.
330  *
331  * Signal ALL processes waiting on a condition.
332  * If there are no process waiting, no action is done.
333  * \param cond A condition
334  */
335 void SIMIX_cond_broadcast(smx_cond_t cond)
336 {
337   XBT_IN("(%p)",cond);
338   XBT_DEBUG("Broadcast condition %p", cond);
339
340   /* Signal the condition until nobody is waiting on it */
341   while (xbt_swag_size(cond->sleeping)) {
342     SIMIX_cond_signal(cond);
343   }
344   XBT_OUT();
345 }
346
347 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
348 {
349   if (cond != nullptr)
350     intrusive_ptr_add_ref(cond);
351   return cond;
352 }
353
354 void SIMIX_cond_unref(smx_cond_t cond)
355 {
356   XBT_IN("(%p)",cond);
357   XBT_DEBUG("Destroy condition %p", cond);
358   if (cond != nullptr) {
359     intrusive_ptr_release(cond);
360   }
361   XBT_OUT();
362 }
363
364
365 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
366 {
367   auto previous = (cond->refcount_)++;
368   xbt_assert(previous != 0);
369 }
370
371 void intrusive_ptr_release(s_smx_cond_t *cond)
372 {
373   auto count = --(cond->refcount_);
374   if (count == 0) {
375     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
376                 "Cannot destroy conditional since someone is still using it");
377     xbt_swag_free(cond->sleeping);
378     delete cond;
379   }
380 }
381
382 /******************************** Semaphores **********************************/
383 /** @brief Initialize a semaphore */
384 smx_sem_t SIMIX_sem_init(unsigned int value)
385 {
386   XBT_IN("(%u)",value);
387   simgrid::simix::ActorImpl p;
388
389   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
390   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
391   sem->value = value;
392   XBT_OUT();
393   return sem;
394 }
395
396 /** @brief Destroys a semaphore */
397 void SIMIX_sem_destroy(smx_sem_t sem)
398 {
399   XBT_IN("(%p)",sem);
400   XBT_DEBUG("Destroy semaphore %p", sem);
401   if (sem != nullptr) {
402     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
403                 "Cannot destroy semaphore since someone is still using it");
404     xbt_swag_free(sem->sleeping);
405     xbt_free(sem);
406   }
407   XBT_OUT();
408 }
409
410 /** @brief release the semaphore
411  *
412  * Unlock a process waiting on the semaphore.
413  * If no one was blocked, the semaphore capacity is increased by 1.
414  */
415 void SIMIX_sem_release(smx_sem_t sem)
416 {
417   XBT_IN("(%p)",sem);
418   smx_actor_t proc;
419
420   XBT_DEBUG("Sem release semaphore %p", sem);
421   if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
422     proc->waiting_synchro = nullptr;
423     SIMIX_simcall_answer(&proc->simcall);
424   } else {
425     sem->value++;
426   }
427   XBT_OUT();
428 }
429
430 /** @brief Returns true if acquiring this semaphore would block */
431 int SIMIX_sem_would_block(smx_sem_t sem)
432 {
433   XBT_IN("(%p)",sem);
434   XBT_OUT();
435   return (sem->value <= 0);
436 }
437
438 /** @brief Returns the current capacity of the semaphore */
439 int SIMIX_sem_get_capacity(smx_sem_t sem)
440 {
441   XBT_IN("(%p)",sem);
442   XBT_OUT();
443   return sem->value;
444 }
445
446 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
447                             smx_simcall_t simcall)
448 {
449   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
450   smx_activity_t synchro = nullptr;
451
452   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
453   if (sem->value <= 0) {
454     synchro = SIMIX_synchro_wait(issuer->host, timeout);
455     synchro->simcalls.push_front(simcall);
456     issuer->waiting_synchro = synchro;
457     xbt_swag_insert(issuer, sem->sleeping);
458   } else {
459     sem->value--;
460     SIMIX_simcall_answer(simcall);
461   }
462   XBT_OUT();
463 }
464
465 /**
466  * \brief Handles a sem acquire simcall without timeout.
467  * \param simcall the simcall
468  */
469 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
470 {
471   XBT_IN("(%p)",simcall);
472   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
473   XBT_OUT();
474 }
475
476 /**
477  * \brief Handles a sem acquire simcall with timeout.
478  * \param simcall the simcall
479  */
480 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
481 {
482   XBT_IN("(%p)",simcall);
483   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
484   XBT_OUT();
485 }