1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/kernel/activity/ConditionVariableImpl.hpp"
7 #include "src/kernel/activity/MutexImpl.hpp"
8 #include "src/kernel/activity/SemaphoreImpl.hpp"
9 #include "src/kernel/activity/SynchroRaw.hpp"
10 #include "src/kernel/context/Context.hpp"
11 #include "src/simix/smx_synchro_private.hpp"
12 #include "src/surf/cpu_interface.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
16 /***************************** Raw synchronization *********************************/
18 smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
20 XBT_IN("(%p, %f)",smx_host,timeout);
22 simgrid::kernel::activity::RawImplPtr sync =
23 simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl());
24 sync->surf_action_ = smx_host->pimpl_cpu->sleep(timeout);
25 sync->surf_action_->set_data(sync.get());
30 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
32 XBT_IN("(%p, %p)",process,simcall);
33 switch (simcall->call) {
35 case SIMCALL_MUTEX_LOCK:
36 simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping, *process);
39 case SIMCALL_COND_WAIT:
40 simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping, *process);
43 case SIMCALL_COND_WAIT_TIMEOUT:
44 simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping, *process);
45 simcall_cond_wait_timeout__set__result(simcall, 1); // signal a timeout
48 case SIMCALL_SEM_ACQUIRE:
49 simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping_, *process);
52 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
53 simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping_, *process);
54 simcall_sem_acquire_timeout__set__result(simcall, 1); // signal a timeout
63 void SIMIX_synchro_finish(smx_activity_t synchro)
65 XBT_IN("(%p)", synchro.get());
66 smx_simcall_t simcall = synchro->simcalls_.front();
67 synchro->simcalls_.pop_front();
69 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
70 simcall->issuer->waiting_synchro = nullptr;
72 if (synchro->state_ != SIMIX_SRC_TIMEOUT) {
73 xbt_assert(synchro->state_ == SIMIX_FAILED);
74 simcall->issuer->context_->iwannadie = true;
76 SIMIX_simcall_answer(simcall);
81 /******************************** Semaphores **********************************/
83 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
84 smx_simcall_t simcall)
86 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
87 smx_activity_t synchro = nullptr;
89 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
90 if (sem->value_ <= 0) {
91 synchro = SIMIX_synchro_wait(issuer->host_, timeout);
92 synchro->simcalls_.push_front(simcall);
93 issuer->waiting_synchro = synchro;
94 sem->sleeping_.push_back(*issuer);
97 SIMIX_simcall_answer(simcall);
103 * @brief Handles a sem acquire simcall without timeout.
105 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
107 XBT_IN("(%p)",simcall);
108 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
113 * @brief Handles a sem acquire simcall with timeout.
115 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
117 XBT_IN("(%p)",simcall);
118 simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
119 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);