Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correctly deal with simcall returning a intrusive_ptr of nullptr
[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::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);
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       xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
45       break;
46
47     case SIMCALL_COND_WAIT:
48       xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
49       break;
50
51     case SIMCALL_COND_WAIT_TIMEOUT:
52       xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
53       break;
54
55     case SIMCALL_SEM_ACQUIRE:
56       xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
57       break;
58
59     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
60       xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
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);
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 //      SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
84       break;
85
86     default:
87       THROW_IMPOSSIBLE;
88       break;
89   }
90
91   SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
92   simcall->issuer->waiting_synchro = nullptr;
93   synchro->unref();
94   SIMIX_simcall_answer(simcall);
95   XBT_OUT();
96 }
97 /*********************************** Mutex ************************************/
98
99 namespace simgrid {
100 namespace simix {
101
102 MutexImpl::MutexImpl() : mutex_(this)
103 {
104   XBT_IN("(%p)", this);
105   // Useful to initialize sleeping swag:
106   simgrid::simix::ActorImpl p;
107   this->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
108   XBT_OUT();
109 }
110
111 MutexImpl::~MutexImpl()
112 {
113   XBT_IN("(%p)", this);
114   xbt_swag_free(this->sleeping);
115   XBT_OUT();
116 }
117
118 void MutexImpl::lock(smx_actor_t issuer)
119 {
120   XBT_IN("(%p; %p)", this, issuer);
121   /* FIXME: check where to validate the arguments */
122   smx_activity_t synchro = nullptr;
123
124   if (this->locked) {
125     /* FIXME: check if the host is active ? */
126     /* Somebody using the mutex, use a synchronization to get host failures */
127     synchro = SIMIX_synchro_wait(issuer->host, -1);
128     synchro->simcalls.push_back(&issuer->simcall);
129     issuer->waiting_synchro = synchro;
130     xbt_swag_insert(issuer, this->sleeping);
131   } else {
132     /* mutex free */
133     this->locked = true;
134     this->owner = issuer;
135     SIMIX_simcall_answer(&issuer->simcall);
136   }
137   XBT_OUT();
138 }
139
140 /** Tries to lock the mutex for a process
141  *
142  * \param  issuer  the process that tries to acquire the mutex
143  * \return whether we managed to lock the mutex
144  */
145 bool MutexImpl::try_lock(smx_actor_t issuer)
146 {
147   XBT_IN("(%p, %p)", this, issuer);
148   if (this->locked) {
149     XBT_OUT();
150     return false;
151   }
152
153   this->locked = true;
154   this->owner = issuer;
155   XBT_OUT();
156   return true;
157 }
158
159 /** Unlock a mutex for a process
160  *
161  * Unlocks the mutex and gives it to a process waiting for it.
162  * If the unlocker is not the owner of the mutex nothing happens.
163  * If there are no process waiting, it sets the mutex as free.
164  */
165 void MutexImpl::unlock(smx_actor_t issuer)
166 {
167   XBT_IN("(%p, %p)", this, issuer);
168   if (not this->locked)
169     THROWF(mismatch_error, 0, "Cannot release that mutex: it was not locked.");
170
171   /* If the mutex is not owned by the issuer, that's not good */
172   if (issuer != this->owner)
173     THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%ld), not by you.",
174         this->owner->name.c_str(),this->owner->pid);
175
176   if (xbt_swag_size(this->sleeping) > 0) {
177     /*process to wake up */
178     smx_actor_t p = (smx_actor_t) xbt_swag_extract(this->sleeping);
179     p->waiting_synchro->unref();
180     p->waiting_synchro = nullptr;
181     this->owner = p;
182     SIMIX_simcall_answer(&p->simcall);
183   } else {
184     /* nobody to wake up */
185     this->locked = false;
186     this->owner = nullptr;
187   }
188   XBT_OUT();
189 }
190
191 }
192 }
193
194 /** Increase the refcount for this mutex */
195 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
196 {
197   if (mutex != nullptr)
198     intrusive_ptr_add_ref(mutex);
199   return mutex;
200 }
201
202 /** Decrease the refcount for this mutex */
203 void SIMIX_mutex_unref(smx_mutex_t mutex)
204 {
205   if (mutex != nullptr)
206     intrusive_ptr_release(mutex);
207 }
208
209 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall)
210 {
211   return new simgrid::simix::MutexImpl();
212 }
213
214 // Simcall handlers:
215
216 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
217 {
218   mutex->lock(simcall->issuer);
219 }
220
221 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
222 {
223   return mutex->try_lock(simcall->issuer);
224 }
225
226 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
227 {
228   mutex->unlock(simcall->issuer);
229 }
230
231 /********************************* Condition **********************************/
232
233 /**
234  * \brief Initialize a condition.
235  *
236  * Allocates and creates the data for the condition.
237  * It have to be called before the use of the condition.
238  * \return A condition
239  */
240 smx_cond_t SIMIX_cond_init()
241 {
242   XBT_IN("()");
243   simgrid::simix::ActorImpl p;
244   smx_cond_t cond = new s_smx_cond();
245   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
246   cond->refcount_ = 1;
247   XBT_OUT();
248   return cond;
249 }
250
251 /**
252  * \brief Handle a condition waiting simcall without timeouts
253  * \param simcall the simcall
254  */
255 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
256 {
257   XBT_IN("(%p)",simcall);
258   smx_actor_t issuer = simcall->issuer;
259
260   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
261   XBT_OUT();
262 }
263
264 /**
265  * \brief Handle a condition waiting simcall with timeouts
266  * \param simcall the simcall
267  */
268 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
269                      smx_mutex_t mutex, double timeout)
270 {
271   XBT_IN("(%p)",simcall);
272   smx_actor_t issuer = simcall->issuer;
273
274   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
275   XBT_OUT();
276 }
277
278
279 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
280                              smx_actor_t issuer, smx_simcall_t simcall)
281 {
282   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
283   smx_activity_t synchro = nullptr;
284
285   XBT_DEBUG("Wait condition %p", cond);
286
287   /* If there is a mutex unlock it */
288   /* FIXME: what happens if the issuer is not the owner of the mutex? */
289   if (mutex != nullptr) {
290     cond->mutex = mutex;
291     mutex->unlock(issuer);
292   }
293
294   synchro = SIMIX_synchro_wait(issuer->host, timeout);
295   synchro->simcalls.push_front(simcall);
296   issuer->waiting_synchro = synchro;
297   xbt_swag_insert(simcall->issuer, cond->sleeping);
298   XBT_OUT();
299 }
300
301 /**
302  * \brief Signalizes a condition.
303  *
304  * Signalizes a condition and wakes up a sleeping process.
305  * If there are no process sleeping, no action is done.
306  * \param cond A condition
307  */
308 void SIMIX_cond_signal(smx_cond_t cond)
309 {
310   XBT_IN("(%p)",cond);
311   smx_actor_t proc = nullptr;
312   smx_mutex_t mutex = nullptr;
313   smx_simcall_t simcall = nullptr;
314
315   XBT_DEBUG("Signal condition %p", cond);
316
317   /* If there are processes waiting for the condition choose one and try
318      to make it acquire the mutex */
319   if ((proc = (smx_actor_t) xbt_swag_extract(cond->sleeping))) {
320
321     /* Destroy waiter's synchronization */
322     proc->waiting_synchro->unref();
323     proc->waiting_synchro = nullptr;
324
325     /* Now transform the cond wait simcall into a mutex lock one */
326     simcall = &proc->simcall;
327     if(simcall->call == SIMCALL_COND_WAIT)
328       mutex = simcall_cond_wait__get__mutex(simcall);
329     else
330       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
331     simcall->call = SIMCALL_MUTEX_LOCK;
332
333     simcall_HANDLER_mutex_lock(simcall, mutex);
334   }
335   XBT_OUT();
336 }
337
338 /**
339  * \brief Broadcasts a condition.
340  *
341  * Signal ALL processes waiting on a condition.
342  * If there are no process waiting, no action is done.
343  * \param cond A condition
344  */
345 void SIMIX_cond_broadcast(smx_cond_t cond)
346 {
347   XBT_IN("(%p)",cond);
348   XBT_DEBUG("Broadcast condition %p", cond);
349
350   /* Signal the condition until nobody is waiting on it */
351   while (xbt_swag_size(cond->sleeping)) {
352     SIMIX_cond_signal(cond);
353   }
354   XBT_OUT();
355 }
356
357 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
358 {
359   if (cond != nullptr)
360     intrusive_ptr_add_ref(cond);
361   return cond;
362 }
363
364 void SIMIX_cond_unref(smx_cond_t cond)
365 {
366   XBT_IN("(%p)",cond);
367   XBT_DEBUG("Destroy condition %p", cond);
368   if (cond != nullptr) {
369     intrusive_ptr_release(cond);
370   }
371   XBT_OUT();
372 }
373
374
375 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
376 {
377   auto previous = (cond->refcount_)++;
378   xbt_assert(previous != 0);
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     proc->waiting_synchro->unref();
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 }