1 /* Copyright (c) 2007-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smx_private.hpp"
8 #include "src/surf/cpu_interface.hpp"
9 #include "src/surf/surf_interface.hpp"
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::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);
37 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
39 XBT_IN("(%p, %p)",process,simcall);
40 switch (simcall->call) {
42 case SIMCALL_MUTEX_LOCK:
43 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
46 case SIMCALL_COND_WAIT:
47 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
50 case SIMCALL_COND_WAIT_TIMEOUT:
51 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
54 case SIMCALL_SEM_ACQUIRE:
55 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
58 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
59 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
68 void SIMIX_synchro_finish(smx_activity_t synchro)
70 XBT_IN("(%p)", synchro.get());
71 smx_simcall_t simcall = synchro->simcalls.front();
72 synchro->simcalls.pop_front();
74 switch (synchro->state) {
76 case SIMIX_SRC_TIMEOUT:
77 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
81 simcall->issuer->context->iwannadie = 1;
89 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
90 simcall->issuer->waiting_synchro = nullptr;
91 SIMIX_simcall_answer(simcall);
94 /*********************************** Mutex ************************************/
99 MutexImpl::MutexImpl() : mutex_(this)
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));
108 MutexImpl::~MutexImpl()
110 XBT_IN("(%p)", this);
111 xbt_swag_free(this->sleeping);
115 void MutexImpl::lock(smx_actor_t issuer)
117 XBT_IN("(%p; %p)", this, issuer);
118 /* FIXME: check where to validate the arguments */
119 smx_activity_t synchro = nullptr;
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);
131 this->owner = issuer;
132 SIMIX_simcall_answer(&issuer->simcall);
137 /** Tries to lock the mutex for a process
139 * \param issuer the process that tries to acquire the mutex
140 * \return whether we managed to lock the mutex
142 bool MutexImpl::try_lock(smx_actor_t issuer)
144 XBT_IN("(%p, %p)", this, issuer);
151 this->owner = issuer;
156 /** Unlock a mutex for a process
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.
162 void MutexImpl::unlock(smx_actor_t issuer)
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.");
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);
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;
178 SIMIX_simcall_answer(&p->simcall);
180 /* nobody to wake up */
181 this->locked = false;
182 this->owner = nullptr;
190 /** Increase the refcount for this mutex */
191 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
193 if (mutex != nullptr)
194 intrusive_ptr_add_ref(mutex);
198 /** Decrease the refcount for this mutex */
199 void SIMIX_mutex_unref(smx_mutex_t mutex)
201 if (mutex != nullptr)
202 intrusive_ptr_release(mutex);
207 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
209 mutex->lock(simcall->issuer);
212 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
214 return mutex->try_lock(simcall->issuer);
217 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
219 mutex->unlock(simcall->issuer);
222 /********************************* Condition **********************************/
225 * \brief Initialize a condition.
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
231 smx_cond_t SIMIX_cond_init()
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));
243 * \brief Handle a condition waiting simcall without timeouts
244 * \param simcall the simcall
246 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
248 XBT_IN("(%p)",simcall);
249 smx_actor_t issuer = simcall->issuer;
251 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
256 * \brief Handle a condition waiting simcall with timeouts
257 * \param simcall the simcall
259 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
260 smx_mutex_t mutex, double timeout)
262 XBT_IN("(%p)",simcall);
263 smx_actor_t issuer = simcall->issuer;
265 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
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)
273 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
274 smx_activity_t synchro = nullptr;
276 XBT_DEBUG("Wait condition %p", cond);
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) {
282 mutex->unlock(issuer);
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);
293 * \brief Signalizes a condition.
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
299 void SIMIX_cond_signal(smx_cond_t cond)
302 smx_actor_t proc = nullptr;
303 smx_mutex_t mutex = nullptr;
304 smx_simcall_t simcall = nullptr;
306 XBT_DEBUG("Signal condition %p", cond);
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))) {
312 /* Destroy waiter's synchronization */
313 proc->waiting_synchro = nullptr;
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);
320 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
321 simcall->call = SIMCALL_MUTEX_LOCK;
323 simcall_HANDLER_mutex_lock(simcall, mutex);
329 * \brief Broadcasts a condition.
331 * Signal ALL processes waiting on a condition.
332 * If there are no process waiting, no action is done.
333 * \param cond A condition
335 void SIMIX_cond_broadcast(smx_cond_t cond)
338 XBT_DEBUG("Broadcast condition %p", cond);
340 /* Signal the condition until nobody is waiting on it */
341 while (xbt_swag_size(cond->sleeping)) {
342 SIMIX_cond_signal(cond);
347 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
350 intrusive_ptr_add_ref(cond);
354 void SIMIX_cond_unref(smx_cond_t cond)
357 XBT_DEBUG("Destroy condition %p", cond);
358 if (cond != nullptr) {
359 intrusive_ptr_release(cond);
365 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
367 auto previous = cond->refcount_.fetch_add(1);
368 xbt_assert(previous != 0);
371 void intrusive_ptr_release(s_smx_cond_t *cond)
373 if (cond->refcount_.fetch_sub(1) == 1) {
374 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
375 "Cannot destroy conditional since someone is still using it");
376 xbt_swag_free(cond->sleeping);
381 /******************************** Semaphores **********************************/
382 /** @brief Initialize a semaphore */
383 smx_sem_t SIMIX_sem_init(unsigned int value)
385 XBT_IN("(%u)",value);
386 simgrid::simix::ActorImpl p;
388 smx_sem_t sem = new s_smx_sem_t;
389 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
395 /** @brief Destroys a semaphore */
396 void SIMIX_sem_destroy(smx_sem_t sem)
399 XBT_DEBUG("Destroy semaphore %p", sem);
400 if (sem != nullptr) {
401 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
402 "Cannot destroy semaphore since someone is still using it");
403 xbt_swag_free(sem->sleeping);
409 /** @brief release the semaphore
411 * Unlock a process waiting on the semaphore.
412 * If no one was blocked, the semaphore capacity is increased by 1.
414 void SIMIX_sem_release(smx_sem_t sem)
419 XBT_DEBUG("Sem release semaphore %p", sem);
420 if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
421 proc->waiting_synchro = nullptr;
422 SIMIX_simcall_answer(&proc->simcall);
429 /** @brief Returns true if acquiring this semaphore would block */
430 int SIMIX_sem_would_block(smx_sem_t sem)
434 return (sem->value <= 0);
437 /** @brief Returns the current capacity of the semaphore */
438 int SIMIX_sem_get_capacity(smx_sem_t sem)
445 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
446 smx_simcall_t simcall)
448 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
449 smx_activity_t synchro = nullptr;
451 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
452 if (sem->value <= 0) {
453 synchro = SIMIX_synchro_wait(issuer->host, timeout);
454 synchro->simcalls.push_front(simcall);
455 issuer->waiting_synchro = synchro;
456 xbt_swag_insert(issuer, sem->sleeping);
459 SIMIX_simcall_answer(simcall);
465 * \brief Handles a sem acquire simcall without timeout.
466 * \param simcall the simcall
468 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
470 XBT_IN("(%p)",simcall);
471 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
476 * \brief Handles a sem acquire simcall with timeout.
477 * \param simcall the simcall
479 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
481 XBT_IN("(%p)",simcall);
482 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);