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(actor::ActorImpl* issuer, double timeout)
17 RawImplPtr synchro = nullptr;
19 XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
21 synchro = RawImplPtr(new RawImpl());
22 (*synchro).set_host(issuer->get_host()).set_timeout(timeout).start();
23 synchro->simcalls_.push_front(&issuer->simcall);
24 issuer->waiting_synchro = synchro;
25 sleeping_.push_back(*issuer);
28 SIMIX_simcall_answer(&issuer->simcall);
31 void SemaphoreImpl::release()
33 XBT_DEBUG("Sem release semaphore %p", this);
35 if (not sleeping_.empty()) {
36 auto& actor = sleeping_.front();
37 sleeping_.pop_front();
38 actor.waiting_synchro = nullptr;
39 SIMIX_simcall_answer(&actor.simcall);
45 } // namespace activity
47 } // namespace simgrid
51 * @brief Handles a sem acquire simcall without timeout.
53 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
55 sem->acquire(simcall->issuer, -1);
59 * @brief Handles a sem acquire simcall with timeout.
61 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
63 simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
64 sem->acquire(simcall->issuer, timeout);