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::Raw *sync = new simgrid::kernel::activity::Raw();
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);
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;
82 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
90 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
91 simcall->issuer->waiting_synchro = nullptr;
93 SIMIX_simcall_answer(simcall);
96 /*********************************** Mutex ************************************/
101 MutexImpl::MutexImpl() : mutex_(this)
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));
110 MutexImpl::~MutexImpl()
112 XBT_IN("(%p)", this);
113 xbt_swag_free(this->sleeping);
117 void MutexImpl::lock(smx_actor_t issuer)
119 XBT_IN("(%p; %p)", this, issuer);
120 /* FIXME: check where to validate the arguments */
121 smx_activity_t synchro = nullptr;
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);
133 this->owner = issuer;
134 SIMIX_simcall_answer(&issuer->simcall);
139 /** Tries to lock the mutex for a process
141 * \param issuer the process that tries to acquire the mutex
142 * \return whether we managed to lock the mutex
144 bool MutexImpl::try_lock(smx_actor_t issuer)
146 XBT_IN("(%p, %p)", this, issuer);
153 this->owner = issuer;
158 /** Unlock a mutex for a process
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.
164 void MutexImpl::unlock(smx_actor_t issuer)
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.");
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->name.c_str(),this->owner->pid);
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 delete p->waiting_synchro;
179 p->waiting_synchro = nullptr;
181 SIMIX_simcall_answer(&p->simcall);
183 /* nobody to wake up */
184 this->locked = false;
185 this->owner = nullptr;
193 /** Increase the refcount for this mutex */
194 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
196 if (mutex != nullptr)
197 intrusive_ptr_add_ref(mutex);
201 /** Decrease the refcount for this mutex */
202 void SIMIX_mutex_unref(smx_mutex_t mutex)
204 if (mutex != nullptr)
205 intrusive_ptr_release(mutex);
208 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall)
210 return new simgrid::simix::MutexImpl();
215 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
217 mutex->lock(simcall->issuer);
220 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
222 return mutex->try_lock(simcall->issuer);
225 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
227 mutex->unlock(simcall->issuer);
230 /********************************* Condition **********************************/
233 * \brief Initialize a condition.
235 * Allocates and creates the data for the condition.
236 * It have to be called before the use of the condition.
237 * \return A condition
239 smx_cond_t SIMIX_cond_init()
242 simgrid::simix::ActorImpl p;
243 smx_cond_t cond = new s_smx_cond();
244 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
251 * \brief Handle a condition waiting simcall without timeouts
252 * \param simcall the simcall
254 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
256 XBT_IN("(%p)",simcall);
257 smx_actor_t issuer = simcall->issuer;
259 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
264 * \brief Handle a condition waiting simcall with timeouts
265 * \param simcall the simcall
267 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
268 smx_mutex_t mutex, double timeout)
270 XBT_IN("(%p)",simcall);
271 smx_actor_t issuer = simcall->issuer;
273 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
278 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
279 smx_actor_t issuer, smx_simcall_t simcall)
281 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
282 smx_activity_t synchro = nullptr;
284 XBT_DEBUG("Wait condition %p", cond);
286 /* If there is a mutex unlock it */
287 /* FIXME: what happens if the issuer is not the owner of the mutex? */
288 if (mutex != nullptr) {
290 mutex->unlock(issuer);
293 synchro = SIMIX_synchro_wait(issuer->host, timeout);
294 synchro->simcalls.push_front(simcall);
295 issuer->waiting_synchro = synchro;
296 xbt_swag_insert(simcall->issuer, cond->sleeping);
301 * \brief Signalizes a condition.
303 * Signalizes a condition and wakes up a sleeping process.
304 * If there are no process sleeping, no action is done.
305 * \param cond A condition
307 void SIMIX_cond_signal(smx_cond_t cond)
310 smx_actor_t proc = nullptr;
311 smx_mutex_t mutex = nullptr;
312 smx_simcall_t simcall = nullptr;
314 XBT_DEBUG("Signal condition %p", cond);
316 /* If there are processes waiting for the condition choose one and try
317 to make it acquire the mutex */
318 if ((proc = (smx_actor_t) xbt_swag_extract(cond->sleeping))) {
320 /* Destroy waiter's synchronization */
321 delete proc->waiting_synchro;
322 proc->waiting_synchro = nullptr;
324 /* Now transform the cond wait simcall into a mutex lock one */
325 simcall = &proc->simcall;
326 if(simcall->call == SIMCALL_COND_WAIT)
327 mutex = simcall_cond_wait__get__mutex(simcall);
329 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
330 simcall->call = SIMCALL_MUTEX_LOCK;
332 simcall_HANDLER_mutex_lock(simcall, mutex);
338 * \brief Broadcasts a condition.
340 * Signal ALL processes waiting on a condition.
341 * If there are no process waiting, no action is done.
342 * \param cond A condition
344 void SIMIX_cond_broadcast(smx_cond_t cond)
347 XBT_DEBUG("Broadcast condition %p", cond);
349 /* Signal the condition until nobody is waiting on it */
350 while (xbt_swag_size(cond->sleeping)) {
351 SIMIX_cond_signal(cond);
356 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
359 intrusive_ptr_add_ref(cond);
363 void SIMIX_cond_unref(smx_cond_t cond)
366 XBT_DEBUG("Destroy condition %p", cond);
367 if (cond != nullptr) {
368 intrusive_ptr_release(cond);
374 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
376 auto previous = (cond->refcount_)++;
377 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 /** @brief Initialize a semaphore */
393 smx_sem_t SIMIX_sem_init(unsigned int value)
395 XBT_IN("(%u)",value);
396 simgrid::simix::ActorImpl p;
398 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
399 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
405 /** @brief Destroys a semaphore */
406 void SIMIX_sem_destroy(smx_sem_t sem)
409 XBT_DEBUG("Destroy semaphore %p", sem);
410 if (sem != nullptr) {
411 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
412 "Cannot destroy semaphore since someone is still using it");
413 xbt_swag_free(sem->sleeping);
419 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
420 SIMIX_sem_release(sem);
422 /** @brief release the semaphore
424 * Unlock a process waiting on the semaphore.
425 * If no one was blocked, the semaphore capacity is increased by 1.
427 void SIMIX_sem_release(smx_sem_t sem)
432 XBT_DEBUG("Sem release semaphore %p", sem);
433 if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
434 delete proc->waiting_synchro;
435 proc->waiting_synchro = nullptr;
436 SIMIX_simcall_answer(&proc->simcall);
443 /** @brief Returns true if acquiring this semaphore would block */
444 int SIMIX_sem_would_block(smx_sem_t sem)
448 return (sem->value <= 0);
451 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
452 return SIMIX_sem_get_capacity(sem);
454 /** @brief Returns the current capacity of the semaphore */
455 int SIMIX_sem_get_capacity(smx_sem_t sem)
462 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
463 smx_simcall_t simcall)
465 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
466 smx_activity_t synchro = nullptr;
468 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
469 if (sem->value <= 0) {
470 synchro = SIMIX_synchro_wait(issuer->host, timeout);
471 synchro->simcalls.push_front(simcall);
472 issuer->waiting_synchro = synchro;
473 xbt_swag_insert(issuer, sem->sleeping);
476 SIMIX_simcall_answer(simcall);
482 * \brief Handles a sem acquire simcall without timeout.
483 * \param simcall the simcall
485 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
487 XBT_IN("(%p)",simcall);
488 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
493 * \brief Handles a sem acquire simcall with timeout.
494 * \param simcall the simcall
496 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
498 XBT_IN("(%p)",simcall);
499 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
502 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
503 return SIMIX_sem_would_block(sem);