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 XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
19 RawImplPtr synchro = RawImplPtr(new RawImpl());
20 synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
21 synchro->register_simcall(&issuer->simcall);
22 sleeping_.push_back(*issuer);
25 issuer->simcall_answer();
28 void SemaphoreImpl::release()
30 XBT_DEBUG("Sem release semaphore %p", this);
32 if (not sleeping_.empty()) {
33 auto& actor = sleeping_.front();
34 sleeping_.pop_front();
35 actor.waiting_synchro = nullptr;
36 actor.simcall_answer();
42 } // namespace activity
44 } // namespace simgrid
48 * @brief Handles a sem acquire simcall without timeout.
50 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
52 sem->acquire(simcall->issuer_, -1);
56 * @brief Handles a sem acquire simcall with timeout.
58 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
60 simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
61 sem->acquire(simcall->issuer_, timeout);