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.h"
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;
82 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
90 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
91 simcall->issuer->waiting_synchro = nullptr;
92 SIMIX_simcall_answer(simcall);
95 /*********************************** Mutex ************************************/
100 MutexImpl::MutexImpl() : 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));
109 MutexImpl::~MutexImpl()
111 XBT_IN("(%p)", this);
112 xbt_swag_free(this->sleeping);
116 void MutexImpl::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 MutexImpl::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 MutexImpl::unlock(smx_actor_t issuer)
165 XBT_IN("(%p, %p)", this, issuer);
166 if (not this->locked)
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:%lu), not by you.",
172 this->owner->cname(), 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 p->waiting_synchro = nullptr;
179 SIMIX_simcall_answer(&p->simcall);
181 /* nobody to wake up */
182 this->locked = false;
183 this->owner = nullptr;
191 /** Increase the refcount for this mutex */
192 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
194 if (mutex != nullptr)
195 intrusive_ptr_add_ref(mutex);
199 /** Decrease the refcount for this mutex */
200 void SIMIX_mutex_unref(smx_mutex_t mutex)
202 if (mutex != nullptr)
203 intrusive_ptr_release(mutex);
208 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
210 mutex->lock(simcall->issuer);
213 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
215 return mutex->try_lock(simcall->issuer);
218 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
220 mutex->unlock(simcall->issuer);
223 /********************************* Condition **********************************/
226 * \brief Initialize a condition.
228 * Allocates and creates the data for the condition.
229 * It have to be called before the use of the condition.
230 * \return A condition
232 smx_cond_t SIMIX_cond_init()
235 simgrid::simix::ActorImpl p;
236 smx_cond_t cond = new s_smx_cond();
237 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
244 * \brief Handle a condition waiting simcall without timeouts
245 * \param simcall the simcall
247 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
249 XBT_IN("(%p)",simcall);
250 smx_actor_t issuer = simcall->issuer;
252 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
257 * \brief Handle a condition waiting simcall with timeouts
258 * \param simcall the simcall
260 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
261 smx_mutex_t mutex, double timeout)
263 XBT_IN("(%p)",simcall);
264 smx_actor_t issuer = simcall->issuer;
266 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
271 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
272 smx_actor_t issuer, smx_simcall_t simcall)
274 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
275 smx_activity_t synchro = nullptr;
277 XBT_DEBUG("Wait condition %p", cond);
279 /* If there is a mutex unlock it */
280 /* FIXME: what happens if the issuer is not the owner of the mutex? */
281 if (mutex != nullptr) {
283 mutex->unlock(issuer);
286 synchro = SIMIX_synchro_wait(issuer->host, timeout);
287 synchro->simcalls.push_front(simcall);
288 issuer->waiting_synchro = synchro;
289 xbt_swag_insert(simcall->issuer, cond->sleeping);
294 * \brief Signalizes a condition.
296 * Signalizes a condition and wakes up a sleeping process.
297 * If there are no process sleeping, no action is done.
298 * \param cond A condition
300 void SIMIX_cond_signal(smx_cond_t cond)
303 smx_actor_t proc = nullptr;
304 smx_mutex_t mutex = nullptr;
305 smx_simcall_t simcall = nullptr;
307 XBT_DEBUG("Signal condition %p", cond);
309 /* If there are processes waiting for the condition choose one and try
310 to make it acquire the mutex */
311 if ((proc = (smx_actor_t) xbt_swag_extract(cond->sleeping))) {
313 /* Destroy waiter's synchronization */
314 proc->waiting_synchro = nullptr;
316 /* Now transform the cond wait simcall into a mutex lock one */
317 simcall = &proc->simcall;
318 if(simcall->call == SIMCALL_COND_WAIT)
319 mutex = simcall_cond_wait__get__mutex(simcall);
321 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
322 simcall->call = SIMCALL_MUTEX_LOCK;
324 simcall_HANDLER_mutex_lock(simcall, mutex);
330 * \brief Broadcasts a condition.
332 * Signal ALL processes waiting on a condition.
333 * If there are no process waiting, no action is done.
334 * \param cond A condition
336 void SIMIX_cond_broadcast(smx_cond_t cond)
339 XBT_DEBUG("Broadcast condition %p", cond);
341 /* Signal the condition until nobody is waiting on it */
342 while (xbt_swag_size(cond->sleeping)) {
343 SIMIX_cond_signal(cond);
348 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
351 intrusive_ptr_add_ref(cond);
355 void SIMIX_cond_unref(smx_cond_t cond)
358 XBT_DEBUG("Destroy condition %p", cond);
359 if (cond != nullptr) {
360 intrusive_ptr_release(cond);
366 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
368 auto previous = (cond->refcount_)++;
369 xbt_assert(previous != 0);
372 void intrusive_ptr_release(s_smx_cond_t *cond)
374 auto count = --(cond->refcount_);
376 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
377 "Cannot destroy conditional since someone is still using it");
378 xbt_swag_free(cond->sleeping);
383 /******************************** Semaphores **********************************/
384 /** @brief Initialize a semaphore */
385 smx_sem_t SIMIX_sem_init(unsigned int value)
387 XBT_IN("(%u)",value);
388 simgrid::simix::ActorImpl p;
390 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
391 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
397 /** @brief Destroys a semaphore */
398 void SIMIX_sem_destroy(smx_sem_t sem)
401 XBT_DEBUG("Destroy semaphore %p", sem);
402 if (sem != nullptr) {
403 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
404 "Cannot destroy semaphore since someone is still using it");
405 xbt_swag_free(sem->sleeping);
411 /** @brief release the semaphore
413 * Unlock a process waiting on the semaphore.
414 * If no one was blocked, the semaphore capacity is increased by 1.
416 void SIMIX_sem_release(smx_sem_t sem)
421 XBT_DEBUG("Sem release semaphore %p", sem);
422 if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
423 proc->waiting_synchro = nullptr;
424 SIMIX_simcall_answer(&proc->simcall);
431 /** @brief Returns true if acquiring this semaphore would block */
432 int SIMIX_sem_would_block(smx_sem_t sem)
436 return (sem->value <= 0);
439 /** @brief Returns the current capacity of the semaphore */
440 int SIMIX_sem_get_capacity(smx_sem_t sem)
447 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
448 smx_simcall_t simcall)
450 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
451 smx_activity_t synchro = nullptr;
453 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
454 if (sem->value <= 0) {
455 synchro = SIMIX_synchro_wait(issuer->host, timeout);
456 synchro->simcalls.push_front(simcall);
457 issuer->waiting_synchro = synchro;
458 xbt_swag_insert(issuer, sem->sleeping);
461 SIMIX_simcall_answer(simcall);
467 * \brief Handles a sem acquire simcall without timeout.
468 * \param simcall the simcall
470 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
472 XBT_IN("(%p)",simcall);
473 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
478 * \brief Handles a sem acquire simcall with timeout.
479 * \param simcall the simcall
481 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
483 XBT_IN("(%p)",simcall);
484 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);