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"
12 #include <xbt/utility.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.get());
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 simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping, *process);
47 case SIMCALL_COND_WAIT:
48 simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping, *process);
51 case SIMCALL_COND_WAIT_TIMEOUT:
52 simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping, *process);
55 case SIMCALL_SEM_ACQUIRE:
56 simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping, *process);
59 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
60 simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping, *process);
69 void SIMIX_synchro_finish(smx_activity_t synchro)
71 XBT_IN("(%p)", synchro.get());
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;
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);
106 MutexImpl::~MutexImpl()
108 XBT_IN("(%p)", this);
112 void MutexImpl::lock(smx_actor_t issuer)
114 XBT_IN("(%p; %p)", this, issuer);
115 /* FIXME: check where to validate the arguments */
116 smx_activity_t synchro = nullptr;
119 /* FIXME: check if the host is active ? */
120 /* Somebody using the mutex, use a synchronization to get host failures */
121 synchro = SIMIX_synchro_wait(issuer->host, -1);
122 synchro->simcalls.push_back(&issuer->simcall);
123 issuer->waiting_synchro = synchro;
124 this->sleeping.push_back(*issuer);
128 this->owner = issuer;
129 SIMIX_simcall_answer(&issuer->simcall);
134 /** Tries to lock the mutex for a process
136 * \param issuer the process that tries to acquire the mutex
137 * \return whether we managed to lock the mutex
139 bool MutexImpl::try_lock(smx_actor_t issuer)
141 XBT_IN("(%p, %p)", this, issuer);
148 this->owner = issuer;
153 /** Unlock a mutex for a process
155 * Unlocks the mutex and gives it to a process waiting for it.
156 * If the unlocker is not the owner of the mutex nothing happens.
157 * If there are no process waiting, it sets the mutex as free.
159 void MutexImpl::unlock(smx_actor_t issuer)
161 XBT_IN("(%p, %p)", this, issuer);
162 if (not this->locked)
163 THROWF(mismatch_error, 0, "Cannot release that mutex: it was not locked.");
165 /* If the mutex is not owned by the issuer, that's not good */
166 if (issuer != this->owner)
167 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%lu), not by you.",
168 this->owner->getCname(), this->owner->pid);
170 if (not this->sleeping.empty()) {
171 /*process to wake up */
172 smx_actor_t p = &this->sleeping.front();
173 this->sleeping.pop_front();
174 p->waiting_synchro = nullptr;
176 SIMIX_simcall_answer(&p->simcall);
178 /* nobody to wake up */
179 this->locked = false;
180 this->owner = nullptr;
188 /** Increase the refcount for this mutex */
189 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
191 if (mutex != nullptr)
192 intrusive_ptr_add_ref(mutex);
196 /** Decrease the refcount for this mutex */
197 void SIMIX_mutex_unref(smx_mutex_t mutex)
199 if (mutex != nullptr)
200 intrusive_ptr_release(mutex);
205 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
207 mutex->lock(simcall->issuer);
210 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
212 return mutex->try_lock(simcall->issuer);
215 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
217 mutex->unlock(simcall->issuer);
220 /********************************* Condition **********************************/
223 * \brief Initialize a condition.
225 * Allocates and creates the data for the condition.
226 * It have to be called before the use of the condition.
227 * \return A condition
229 smx_cond_t SIMIX_cond_init()
232 smx_cond_t cond = new s_smx_cond_t();
239 * \brief Handle a condition waiting simcall without timeouts
240 * \param simcall the simcall
242 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
244 XBT_IN("(%p)",simcall);
245 smx_actor_t issuer = simcall->issuer;
247 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
252 * \brief Handle a condition waiting simcall with timeouts
253 * \param simcall the simcall
255 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
256 smx_mutex_t mutex, double timeout)
258 XBT_IN("(%p)",simcall);
259 smx_actor_t issuer = simcall->issuer;
261 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
266 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
267 smx_actor_t issuer, smx_simcall_t simcall)
269 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
270 smx_activity_t synchro = nullptr;
272 XBT_DEBUG("Wait condition %p", cond);
274 /* If there is a mutex unlock it */
275 /* FIXME: what happens if the issuer is not the owner of the mutex? */
276 if (mutex != nullptr) {
278 mutex->unlock(issuer);
281 synchro = SIMIX_synchro_wait(issuer->host, timeout);
282 synchro->simcalls.push_front(simcall);
283 issuer->waiting_synchro = synchro;
284 cond->sleeping.push_back(*simcall->issuer);
289 * \brief Signalizes a condition.
291 * Signalizes a condition and wakes up a sleeping process.
292 * If there are no process sleeping, no action is done.
293 * \param cond A condition
295 void SIMIX_cond_signal(smx_cond_t cond)
298 XBT_DEBUG("Signal condition %p", cond);
300 /* If there are processes waiting for the condition choose one and try
301 to make it acquire the mutex */
302 if (not cond->sleeping.empty()) {
303 auto& proc = cond->sleeping.front();
304 cond->sleeping.pop_front();
306 /* Destroy waiter's synchronization */
307 proc.waiting_synchro = nullptr;
309 /* Now transform the cond wait simcall into a mutex lock one */
310 smx_simcall_t simcall = &proc.simcall;
312 if(simcall->call == SIMCALL_COND_WAIT)
313 mutex = simcall_cond_wait__get__mutex(simcall);
315 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
316 simcall->call = SIMCALL_MUTEX_LOCK;
318 simcall_HANDLER_mutex_lock(simcall, mutex);
324 * \brief Broadcasts a condition.
326 * Signal ALL processes waiting on a condition.
327 * If there are no process waiting, no action is done.
328 * \param cond A condition
330 void SIMIX_cond_broadcast(smx_cond_t cond)
333 XBT_DEBUG("Broadcast condition %p", cond);
335 /* Signal the condition until nobody is waiting on it */
336 while (not cond->sleeping.empty()) {
337 SIMIX_cond_signal(cond);
342 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
345 intrusive_ptr_add_ref(cond);
349 void SIMIX_cond_unref(smx_cond_t cond)
352 XBT_DEBUG("Destroy condition %p", cond);
353 if (cond != nullptr) {
354 intrusive_ptr_release(cond);
360 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
362 auto previous = cond->refcount_.fetch_add(1);
363 xbt_assert(previous != 0);
366 void intrusive_ptr_release(s_smx_cond_t *cond)
368 if (cond->refcount_.fetch_sub(1) == 1) {
369 xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
374 /******************************** Semaphores **********************************/
375 /** @brief Initialize a semaphore */
376 smx_sem_t SIMIX_sem_init(unsigned int value)
378 XBT_IN("(%u)",value);
379 smx_sem_t sem = new s_smx_sem_t;
385 /** @brief Destroys a semaphore */
386 void SIMIX_sem_destroy(smx_sem_t sem)
389 XBT_DEBUG("Destroy semaphore %p", sem);
390 if (sem != nullptr) {
391 xbt_assert(sem->sleeping.empty(), "Cannot destroy semaphore since someone is still using it");
397 /** @brief release the semaphore
399 * Unlock a process waiting on the semaphore.
400 * If no one was blocked, the semaphore capacity is increased by 1.
402 void SIMIX_sem_release(smx_sem_t sem)
405 XBT_DEBUG("Sem release semaphore %p", sem);
406 if (not sem->sleeping.empty()) {
407 auto& proc = sem->sleeping.front();
408 sem->sleeping.pop_front();
409 proc.waiting_synchro = nullptr;
410 SIMIX_simcall_answer(&proc.simcall);
417 /** @brief Returns true if acquiring this semaphore would block */
418 int SIMIX_sem_would_block(smx_sem_t sem)
422 return (sem->value <= 0);
425 /** @brief Returns the current capacity of the semaphore */
426 int SIMIX_sem_get_capacity(smx_sem_t sem)
433 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
434 smx_simcall_t simcall)
436 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
437 smx_activity_t synchro = nullptr;
439 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
440 if (sem->value <= 0) {
441 synchro = SIMIX_synchro_wait(issuer->host, timeout);
442 synchro->simcalls.push_front(simcall);
443 issuer->waiting_synchro = synchro;
444 sem->sleeping.push_back(*issuer);
447 SIMIX_simcall_answer(simcall);
453 * \brief Handles a sem acquire simcall without timeout.
454 * \param simcall the simcall
456 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
458 XBT_IN("(%p)",simcall);
459 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
464 * \brief Handles a sem acquire simcall with timeout.
465 * \param simcall the simcall
467 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
469 XBT_IN("(%p)",simcall);
470 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);