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/SynchroRaw.hpp"
9 #include "src/kernel/context/Context.hpp"
10 #include "src/simix/smx_synchro_private.hpp"
11 #include "src/surf/cpu_interface.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
15 /***************************** Raw synchronization *********************************/
17 smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
19 XBT_IN("(%p, %f)",smx_host,timeout);
21 simgrid::kernel::activity::RawImplPtr sync =
22 simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl());
23 sync->surf_action_ = smx_host->pimpl_cpu->sleep(timeout);
24 sync->surf_action_->set_data(sync.get());
29 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
31 XBT_IN("(%p, %p)",process,simcall);
32 switch (simcall->call) {
34 case SIMCALL_MUTEX_LOCK:
35 simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping, *process);
38 case SIMCALL_COND_WAIT:
39 simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping, *process);
42 case SIMCALL_COND_WAIT_TIMEOUT:
43 simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping, *process);
44 simcall_cond_wait_timeout__set__result(simcall, 1); // signal a timeout
47 case SIMCALL_SEM_ACQUIRE:
48 simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping, *process);
51 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
52 simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping, *process);
53 simcall_sem_acquire_timeout__set__result(simcall, 1); // signal a timeout
62 void SIMIX_synchro_finish(smx_activity_t synchro)
64 XBT_IN("(%p)", synchro.get());
65 smx_simcall_t simcall = synchro->simcalls_.front();
66 synchro->simcalls_.pop_front();
68 if (synchro->state_ != SIMIX_SRC_TIMEOUT) {
69 if (synchro->state_ == SIMIX_FAILED)
70 simcall->issuer->context_->iwannadie = true;
75 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
76 simcall->issuer->waiting_synchro = nullptr;
77 SIMIX_simcall_answer(simcall);
81 /******************************** Semaphores **********************************/
82 /** @brief Initialize a semaphore */
83 smx_sem_t SIMIX_sem_init(unsigned int value)
86 smx_sem_t sem = new s_smx_sem_t;
92 /** @brief Destroys a semaphore */
93 void SIMIX_sem_destroy(smx_sem_t sem)
96 XBT_DEBUG("Destroy semaphore %p", sem);
98 xbt_assert(sem->sleeping.empty(), "Cannot destroy semaphore since someone is still using it");
104 /** @brief release the semaphore
106 * Unlock a process waiting on the semaphore.
107 * If no one was blocked, the semaphore capacity is increased by 1.
109 void SIMIX_sem_release(smx_sem_t sem)
112 XBT_DEBUG("Sem release semaphore %p", sem);
113 if (not sem->sleeping.empty()) {
114 auto& proc = sem->sleeping.front();
115 sem->sleeping.pop_front();
116 proc.waiting_synchro = nullptr;
117 SIMIX_simcall_answer(&proc.simcall);
124 /** @brief Returns true if acquiring this semaphore would block */
125 int SIMIX_sem_would_block(smx_sem_t sem)
129 return (sem->value <= 0);
132 /** @brief Returns the current capacity of the semaphore */
133 int SIMIX_sem_get_capacity(smx_sem_t sem)
140 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
141 smx_simcall_t simcall)
143 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
144 smx_activity_t synchro = nullptr;
146 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
147 if (sem->value <= 0) {
148 synchro = SIMIX_synchro_wait(issuer->host_, timeout);
149 synchro->simcalls_.push_front(simcall);
150 issuer->waiting_synchro = synchro;
151 sem->sleeping.push_back(*issuer);
154 SIMIX_simcall_answer(simcall);
160 * @brief Handles a sem acquire simcall without timeout.
162 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
164 XBT_IN("(%p)",simcall);
165 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
170 * @brief Handles a sem acquire simcall with timeout.
172 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
174 XBT_IN("(%p)",simcall);
175 simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
176 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);