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 "smx_private.h"
9 #include "src/surf/cpu_interface.hpp"
10 #include "src/surf/surf_interface.hpp"
14 #include "src/kernel/activity/SynchroRaw.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
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);
24 /***************************** Raw synchronization *********************************/
26 static smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
28 XBT_IN("(%p, %f)",smx_host,timeout);
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);
38 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
40 XBT_IN("(%p, %p)",process,simcall);
41 switch (simcall->call) {
43 case SIMCALL_MUTEX_LOCK:
44 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
47 case SIMCALL_COND_WAIT:
48 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
51 case SIMCALL_COND_WAIT_TIMEOUT:
52 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
55 case SIMCALL_SEM_ACQUIRE:
56 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
59 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
60 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
69 void SIMIX_synchro_finish(smx_activity_t synchro)
71 XBT_IN("(%p)",synchro);
72 smx_simcall_t simcall = synchro->simcalls.front();
73 synchro->simcalls.pop_front();
75 switch (synchro->state) {
77 case SIMIX_SRC_TIMEOUT:
78 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
82 simcall->issuer->context->iwannadie = 1;
83 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
91 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
92 simcall->issuer->waiting_synchro = nullptr;
94 SIMIX_simcall_answer(simcall);
97 /*********************************** Mutex ************************************/
102 MutexImpl::MutexImpl() : mutex_(this)
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));
111 MutexImpl::~MutexImpl()
113 XBT_IN("(%p)", this);
114 xbt_swag_free(this->sleeping);
118 void MutexImpl::lock(smx_actor_t issuer)
120 XBT_IN("(%p; %p)", this, issuer);
121 /* FIXME: check where to validate the arguments */
122 smx_activity_t synchro = nullptr;
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);
134 this->owner = issuer;
135 SIMIX_simcall_answer(&issuer->simcall);
140 /** Tries to lock the mutex for a process
142 * \param issuer the process that tries to acquire the mutex
143 * \return whether we managed to lock the mutex
145 bool MutexImpl::try_lock(smx_actor_t issuer)
147 XBT_IN("(%p, %p)", this, issuer);
154 this->owner = issuer;
159 /** Unlock a mutex for a process
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.
165 void MutexImpl::unlock(smx_actor_t issuer)
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.");
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);
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;
182 SIMIX_simcall_answer(&p->simcall);
184 /* nobody to wake up */
185 this->locked = false;
186 this->owner = nullptr;
194 /** Increase the refcount for this mutex */
195 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
197 if (mutex != nullptr)
198 intrusive_ptr_add_ref(mutex);
202 /** Decrease the refcount for this mutex */
203 void SIMIX_mutex_unref(smx_mutex_t mutex)
205 if (mutex != nullptr)
206 intrusive_ptr_release(mutex);
209 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall)
211 return new simgrid::simix::MutexImpl();
216 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
218 mutex->lock(simcall->issuer);
221 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
223 return mutex->try_lock(simcall->issuer);
226 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
228 mutex->unlock(simcall->issuer);
231 /********************************* Condition **********************************/
234 * \brief Initialize a condition.
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
240 smx_cond_t SIMIX_cond_init()
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));
252 * \brief Handle a condition waiting simcall without timeouts
253 * \param simcall the simcall
255 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
257 XBT_IN("(%p)",simcall);
258 smx_actor_t issuer = simcall->issuer;
260 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
265 * \brief Handle a condition waiting simcall with timeouts
266 * \param simcall the simcall
268 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
269 smx_mutex_t mutex, double timeout)
271 XBT_IN("(%p)",simcall);
272 smx_actor_t issuer = simcall->issuer;
274 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
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)
282 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
283 smx_activity_t synchro = nullptr;
285 XBT_DEBUG("Wait condition %p", cond);
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) {
291 mutex->unlock(issuer);
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);
302 * \brief Signalizes a condition.
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
308 void SIMIX_cond_signal(smx_cond_t cond)
311 smx_actor_t proc = nullptr;
312 smx_mutex_t mutex = nullptr;
313 smx_simcall_t simcall = nullptr;
315 XBT_DEBUG("Signal condition %p", cond);
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))) {
321 /* Destroy waiter's synchronization */
322 proc->waiting_synchro->unref();
323 proc->waiting_synchro = nullptr;
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);
330 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
331 simcall->call = SIMCALL_MUTEX_LOCK;
333 simcall_HANDLER_mutex_lock(simcall, mutex);
339 * \brief Broadcasts a condition.
341 * Signal ALL processes waiting on a condition.
342 * If there are no process waiting, no action is done.
343 * \param cond A condition
345 void SIMIX_cond_broadcast(smx_cond_t cond)
348 XBT_DEBUG("Broadcast condition %p", cond);
350 /* Signal the condition until nobody is waiting on it */
351 while (xbt_swag_size(cond->sleeping)) {
352 SIMIX_cond_signal(cond);
357 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
360 intrusive_ptr_add_ref(cond);
364 void SIMIX_cond_unref(smx_cond_t cond)
367 XBT_DEBUG("Destroy condition %p", cond);
368 if (cond != nullptr) {
369 intrusive_ptr_release(cond);
375 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
377 auto previous = (cond->refcount_)++;
378 xbt_assert(previous != 0);
381 void intrusive_ptr_release(s_smx_cond_t *cond)
383 auto count = --(cond->refcount_);
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);
392 /******************************** Semaphores **********************************/
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 proc->waiting_synchro->unref();
436 proc->waiting_synchro = nullptr;
437 SIMIX_simcall_answer(&proc->simcall);
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);