Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further reduce the intrusive_ptr churn
[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.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 //      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   SIMIX_simcall_answer(simcall);
94   XBT_OUT();
95 }
96 /*********************************** Mutex ************************************/
97
98 namespace simgrid {
99 namespace simix {
100
101 MutexImpl::MutexImpl() : 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 MutexImpl::~MutexImpl()
111 {
112   XBT_IN("(%p)", this);
113   xbt_swag_free(this->sleeping);
114   XBT_OUT();
115 }
116
117 void MutexImpl::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 MutexImpl::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 MutexImpl::unlock(smx_actor_t issuer)
165 {
166   XBT_IN("(%p, %p)", this, issuer);
167   if (not 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->cname(), 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     p->waiting_synchro = nullptr;
179     this->owner = p;
180     SIMIX_simcall_answer(&p->simcall);
181   } else {
182     /* nobody to wake up */
183     this->locked = false;
184     this->owner = nullptr;
185   }
186   XBT_OUT();
187 }
188
189 }
190 }
191
192 /** Increase the refcount for this mutex */
193 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
194 {
195   if (mutex != nullptr)
196     intrusive_ptr_add_ref(mutex);
197   return mutex;
198 }
199
200 /** Decrease the refcount for this mutex */
201 void SIMIX_mutex_unref(smx_mutex_t mutex)
202 {
203   if (mutex != nullptr)
204     intrusive_ptr_release(mutex);
205 }
206
207 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall)
208 {
209   return new simgrid::simix::MutexImpl();
210 }
211
212 // Simcall handlers:
213
214 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
215 {
216   mutex->lock(simcall->issuer);
217 }
218
219 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
220 {
221   return mutex->try_lock(simcall->issuer);
222 }
223
224 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
225 {
226   mutex->unlock(simcall->issuer);
227 }
228
229 /********************************* Condition **********************************/
230
231 /**
232  * \brief Initialize a condition.
233  *
234  * Allocates and creates the data for the condition.
235  * It have to be called before the use of the condition.
236  * \return A condition
237  */
238 smx_cond_t SIMIX_cond_init()
239 {
240   XBT_IN("()");
241   simgrid::simix::ActorImpl p;
242   smx_cond_t cond = new s_smx_cond();
243   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
244   cond->refcount_ = 1;
245   XBT_OUT();
246   return cond;
247 }
248
249 /**
250  * \brief Handle a condition waiting simcall without timeouts
251  * \param simcall the simcall
252  */
253 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
254 {
255   XBT_IN("(%p)",simcall);
256   smx_actor_t issuer = simcall->issuer;
257
258   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
259   XBT_OUT();
260 }
261
262 /**
263  * \brief Handle a condition waiting simcall with timeouts
264  * \param simcall the simcall
265  */
266 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
267                      smx_mutex_t mutex, double timeout)
268 {
269   XBT_IN("(%p)",simcall);
270   smx_actor_t issuer = simcall->issuer;
271
272   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
273   XBT_OUT();
274 }
275
276
277 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
278                              smx_actor_t issuer, smx_simcall_t simcall)
279 {
280   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
281   smx_activity_t synchro = nullptr;
282
283   XBT_DEBUG("Wait condition %p", cond);
284
285   /* If there is a mutex unlock it */
286   /* FIXME: what happens if the issuer is not the owner of the mutex? */
287   if (mutex != nullptr) {
288     cond->mutex = mutex;
289     mutex->unlock(issuer);
290   }
291
292   synchro = SIMIX_synchro_wait(issuer->host, timeout);
293   synchro->simcalls.push_front(simcall);
294   issuer->waiting_synchro = synchro;
295   xbt_swag_insert(simcall->issuer, cond->sleeping);
296   XBT_OUT();
297 }
298
299 /**
300  * \brief Signalizes a condition.
301  *
302  * Signalizes a condition and wakes up a sleeping process.
303  * If there are no process sleeping, no action is done.
304  * \param cond A condition
305  */
306 void SIMIX_cond_signal(smx_cond_t cond)
307 {
308   XBT_IN("(%p)",cond);
309   smx_actor_t proc = nullptr;
310   smx_mutex_t mutex = nullptr;
311   smx_simcall_t simcall = nullptr;
312
313   XBT_DEBUG("Signal condition %p", cond);
314
315   /* If there are processes waiting for the condition choose one and try
316      to make it acquire the mutex */
317   if ((proc = (smx_actor_t) xbt_swag_extract(cond->sleeping))) {
318
319     /* Destroy waiter's synchronization */
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 }
377
378 void intrusive_ptr_release(s_smx_cond_t *cond)
379 {
380   auto count = --(cond->refcount_);
381   if (count == 0) {
382     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
383                 "Cannot destroy conditional since someone is still using it");
384     xbt_swag_free(cond->sleeping);
385     delete cond;
386   }
387 }
388
389 /******************************** Semaphores **********************************/
390 /** @brief Initialize a semaphore */
391 smx_sem_t SIMIX_sem_init(unsigned int value)
392 {
393   XBT_IN("(%u)",value);
394   simgrid::simix::ActorImpl p;
395
396   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
397   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
398   sem->value = value;
399   XBT_OUT();
400   return sem;
401 }
402
403 /** @brief Destroys a semaphore */
404 void SIMIX_sem_destroy(smx_sem_t sem)
405 {
406   XBT_IN("(%p)",sem);
407   XBT_DEBUG("Destroy semaphore %p", sem);
408   if (sem != nullptr) {
409     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
410                 "Cannot destroy semaphore since someone is still using it");
411     xbt_swag_free(sem->sleeping);
412     xbt_free(sem);
413   }
414   XBT_OUT();
415 }
416
417 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
418   SIMIX_sem_release(sem);
419 }
420 /** @brief release the semaphore
421  *
422  * Unlock a process waiting on the semaphore.
423  * If no one was blocked, the semaphore capacity is increased by 1.
424  */
425 void SIMIX_sem_release(smx_sem_t sem)
426 {
427   XBT_IN("(%p)",sem);
428   smx_actor_t proc;
429
430   XBT_DEBUG("Sem release semaphore %p", sem);
431   if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
432     proc->waiting_synchro = nullptr;
433     SIMIX_simcall_answer(&proc->simcall);
434   } else {
435     sem->value++;
436   }
437   XBT_OUT();
438 }
439
440 /** @brief Returns true if acquiring this semaphore would block */
441 int SIMIX_sem_would_block(smx_sem_t sem)
442 {
443   XBT_IN("(%p)",sem);
444   XBT_OUT();
445   return (sem->value <= 0);
446 }
447
448 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
449   return SIMIX_sem_get_capacity(sem);
450 }
451 /** @brief Returns the current capacity of the semaphore */
452 int SIMIX_sem_get_capacity(smx_sem_t sem)
453 {
454   XBT_IN("(%p)",sem);
455   XBT_OUT();
456   return sem->value;
457 }
458
459 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
460                             smx_simcall_t simcall)
461 {
462   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
463   smx_activity_t synchro = nullptr;
464
465   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
466   if (sem->value <= 0) {
467     synchro = SIMIX_synchro_wait(issuer->host, timeout);
468     synchro->simcalls.push_front(simcall);
469     issuer->waiting_synchro = synchro;
470     xbt_swag_insert(issuer, sem->sleeping);
471   } else {
472     sem->value--;
473     SIMIX_simcall_answer(simcall);
474   }
475   XBT_OUT();
476 }
477
478 /**
479  * \brief Handles a sem acquire simcall without timeout.
480  * \param simcall the simcall
481  */
482 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
483 {
484   XBT_IN("(%p)",simcall);
485   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
486   XBT_OUT();
487 }
488
489 /**
490  * \brief Handles a sem acquire simcall with timeout.
491  * \param simcall the simcall
492  */
493 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
494 {
495   XBT_IN("(%p)",simcall);
496   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
497   XBT_OUT();
498 }
499 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
500   return SIMIX_sem_would_block(sem);
501 }