1 /* Copyright (c) 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/SemaphoreImpl.hpp"
7 #include "src/kernel/activity/SynchroRaw.hpp"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_semaphore, simix_synchro, "Semaphore kernel-space implementation");
15 void SemaphoreImpl::acquire(smx_actor_t issuer, double timeout)
17 RawImplPtr synchro = nullptr;
19 XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
21 synchro = RawImplPtr(new RawImpl())->start(issuer->get_host(), timeout);
22 synchro->simcalls_.push_front(&issuer->simcall);
23 issuer->waiting_synchro = synchro;
24 sleeping_.push_back(*issuer);
27 SIMIX_simcall_answer(&issuer->simcall);
30 void SemaphoreImpl::release()
32 XBT_DEBUG("Sem release semaphore %p", this);
34 if (not sleeping_.empty()) {
35 auto& actor = sleeping_.front();
36 sleeping_.pop_front();
37 actor.waiting_synchro = nullptr;
38 SIMIX_simcall_answer(&actor.simcall);
44 } // namespace activity
46 } // namespace simgrid
50 * @brief Handles a sem acquire simcall without timeout.
52 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
54 sem->acquire(simcall->issuer, -1);
58 * @brief Handles a sem acquire simcall with timeout.
60 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
62 simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
63 sem->acquire(simcall->issuer, timeout);