2 /* Copyright (c) 2007-2015. The SimGrid Team.
3 * All rights reserved. */
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. */
8 #include "src/surf/surf_interface.hpp"
9 #include "smx_private.h"
13 #include "src/kernel/activity/SynchroRaw.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
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);
23 /***************************** Raw synchronization *********************************/
25 static smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
27 XBT_IN("(%p, %f)",smx_host,timeout);
29 simgrid::kernel::activity::Raw *sync = new simgrid::kernel::activity::Raw();
30 sync->sleep = surf_host_sleep(smx_host, timeout);
31 sync->sleep->setData(sync);
36 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
38 XBT_IN("(%p, %p)",process,simcall);
39 switch (simcall->call) {
41 case SIMCALL_MUTEX_LOCK:
42 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
45 case SIMCALL_COND_WAIT:
46 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
49 case SIMCALL_COND_WAIT_TIMEOUT:
50 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
53 case SIMCALL_SEM_ACQUIRE:
54 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
57 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
58 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
67 void SIMIX_synchro_finish(smx_activity_t synchro)
69 XBT_IN("(%p)",synchro);
70 smx_simcall_t simcall = synchro->simcalls.front();
71 synchro->simcalls.pop_front();
73 switch (synchro->state) {
75 case SIMIX_SRC_TIMEOUT:
76 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
80 simcall->issuer->context->iwannadie = 1;
81 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
89 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
90 simcall->issuer->waiting_synchro = nullptr;
92 SIMIX_simcall_answer(simcall);
95 /*********************************** Mutex ************************************/
100 Mutex::Mutex() : mutex_(this)
102 XBT_IN("(%p)", this);
103 // Useful to initialize sleeping swag:
104 simgrid::simix::ActorImpl p;
105 this->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
111 XBT_IN("(%p)", this);
112 xbt_swag_free(this->sleeping);
116 void Mutex::lock(smx_actor_t issuer)
118 XBT_IN("(%p; %p)", this, issuer);
119 /* FIXME: check where to validate the arguments */
120 smx_activity_t synchro = nullptr;
123 /* FIXME: check if the host is active ? */
124 /* Somebody using the mutex, use a synchronization to get host failures */
125 synchro = SIMIX_synchro_wait(issuer->host, -1);
126 synchro->simcalls.push_back(&issuer->simcall);
127 issuer->waiting_synchro = synchro;
128 xbt_swag_insert(issuer, this->sleeping);
132 this->owner = issuer;
133 SIMIX_simcall_answer(&issuer->simcall);
138 /** Tries to lock the mutex for a process
140 * \param issuer the process that tries to acquire the mutex
141 * \return whether we managed to lock the mutex
143 bool Mutex::try_lock(smx_actor_t issuer)
145 XBT_IN("(%p, %p)", this, issuer);
152 this->owner = issuer;
157 /** Unlock a mutex for a process
159 * Unlocks the mutex and gives it to a process waiting for it.
160 * If the unlocker is not the owner of the mutex nothing happens.
161 * If there are no process waiting, it sets the mutex as free.
163 void Mutex::unlock(smx_actor_t issuer)
165 XBT_IN("(%p, %p)", this, issuer);
167 THROWF(mismatch_error, 0, "Cannot release that mutex: it was not locked.");
169 /* If the mutex is not owned by the issuer, that's not good */
170 if (issuer != this->owner)
171 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%ld), not by you.",
172 this->owner->name.c_str(),this->owner->pid);
174 if (xbt_swag_size(this->sleeping) > 0) {
175 /*process to wake up */
176 smx_actor_t p = (smx_actor_t) xbt_swag_extract(this->sleeping);
177 delete p->waiting_synchro;
178 p->waiting_synchro = nullptr;
180 SIMIX_simcall_answer(&p->simcall);
182 /* nobody to wake up */
183 this->locked = false;
184 this->owner = nullptr;
192 /** Increase the refcount for this mutex */
193 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
195 if (mutex != nullptr)
196 intrusive_ptr_add_ref(mutex);
200 /** Decrease the refcount for this mutex */
201 void SIMIX_mutex_unref(smx_mutex_t mutex)
203 if (mutex != nullptr)
204 intrusive_ptr_release(mutex);
207 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall)
209 return new simgrid::simix::Mutex();
214 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
216 mutex->lock(simcall->issuer);
219 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
221 return mutex->try_lock(simcall->issuer);
224 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
226 mutex->unlock(simcall->issuer);
229 /********************************* Condition **********************************/
232 * \brief Initialize a condition.
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
238 smx_cond_t SIMIX_cond_init()
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));
250 * \brief Handle a condition waiting simcall without timeouts
251 * \param simcall the simcall
253 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
255 XBT_IN("(%p)",simcall);
256 smx_actor_t issuer = simcall->issuer;
258 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
263 * \brief Handle a condition waiting simcall with timeouts
264 * \param simcall the simcall
266 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
267 smx_mutex_t mutex, double timeout)
269 XBT_IN("(%p)",simcall);
270 smx_actor_t issuer = simcall->issuer;
272 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
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)
280 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
281 smx_activity_t synchro = nullptr;
283 XBT_DEBUG("Wait condition %p", cond);
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) {
289 mutex->unlock(issuer);
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);
300 * \brief Signalizes a condition.
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
306 void SIMIX_cond_signal(smx_cond_t cond)
309 smx_actor_t proc = nullptr;
310 smx_mutex_t mutex = nullptr;
311 smx_simcall_t simcall = nullptr;
313 XBT_DEBUG("Signal condition %p", cond);
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))) {
319 /* Destroy waiter's synchronization */
320 delete proc->waiting_synchro;
321 proc->waiting_synchro = nullptr;
323 /* Now transform the cond wait simcall into a mutex lock one */
324 simcall = &proc->simcall;
325 if(simcall->call == SIMCALL_COND_WAIT)
326 mutex = simcall_cond_wait__get__mutex(simcall);
328 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
329 simcall->call = SIMCALL_MUTEX_LOCK;
331 simcall_HANDLER_mutex_lock(simcall, mutex);
337 * \brief Broadcasts a condition.
339 * Signal ALL processes waiting on a condition.
340 * If there are no process waiting, no action is done.
341 * \param cond A condition
343 void SIMIX_cond_broadcast(smx_cond_t cond)
346 XBT_DEBUG("Broadcast condition %p", cond);
348 /* Signal the condition until nobody is waiting on it */
349 while (xbt_swag_size(cond->sleeping)) {
350 SIMIX_cond_signal(cond);
355 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
358 intrusive_ptr_add_ref(cond);
362 void SIMIX_cond_unref(smx_cond_t cond)
365 XBT_DEBUG("Destroy condition %p", cond);
366 if (cond != nullptr) {
367 intrusive_ptr_release(cond);
373 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
375 auto previous = (cond->refcount_)++;
376 xbt_assert(previous != 0);
380 void intrusive_ptr_release(s_smx_cond_t *cond)
382 auto count = --(cond->refcount_);
384 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
385 "Cannot destroy conditional since someone is still using it");
386 xbt_swag_free(cond->sleeping);
391 /******************************** Semaphores **********************************/
392 #define SMX_SEM_NOLIMIT 99999
393 /** @brief Initialize a semaphore */
394 smx_sem_t SIMIX_sem_init(unsigned int value)
396 XBT_IN("(%u)",value);
397 simgrid::simix::ActorImpl p;
399 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
400 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
406 /** @brief Destroys a semaphore */
407 void SIMIX_sem_destroy(smx_sem_t 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);
420 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
421 SIMIX_sem_release(sem);
423 /** @brief release the semaphore
425 * Unlock a process waiting on the semaphore.
426 * If no one was blocked, the semaphore capacity is increased by 1.
428 void SIMIX_sem_release(smx_sem_t sem)
433 XBT_DEBUG("Sem release semaphore %p", sem);
434 if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
435 delete proc->waiting_synchro;
436 proc->waiting_synchro = nullptr;
437 SIMIX_simcall_answer(&proc->simcall);
438 } else if (sem->value < SMX_SEM_NOLIMIT) {
444 /** @brief Returns true if acquiring this semaphore would block */
445 int SIMIX_sem_would_block(smx_sem_t sem)
449 return (sem->value <= 0);
452 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
453 return SIMIX_sem_get_capacity(sem);
455 /** @brief Returns the current capacity of the semaphore */
456 int SIMIX_sem_get_capacity(smx_sem_t sem)
463 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
464 smx_simcall_t simcall)
466 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
467 smx_activity_t synchro = nullptr;
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);
477 SIMIX_simcall_answer(simcall);
483 * \brief Handles a sem acquire simcall without timeout.
484 * \param simcall the simcall
486 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
488 XBT_IN("(%p)",simcall);
489 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
494 * \brief Handles a sem acquire simcall with timeout.
495 * \param simcall the simcall
497 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
499 XBT_IN("(%p)",simcall);
500 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
503 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
504 return SIMIX_sem_would_block(sem);