1 /* Copyright (c) 2019-2021. 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(new RawImpl([this, issuer]() {
20 this->remove_sleeping_actor(*issuer);
21 if (issuer->simcall_.call_ == simix::Simcall::SEM_ACQUIRE_TIMEOUT)
22 simcall_sem_acquire_timeout__set__result(&issuer->simcall_, 1);
24 synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
25 synchro->register_simcall(&issuer->simcall_);
26 sleeping_.push_back(*issuer);
29 issuer->simcall_answer();
32 void SemaphoreImpl::release()
34 XBT_DEBUG("Sem release semaphore %p", this);
36 if (not sleeping_.empty()) {
37 auto& actor = sleeping_.front();
38 sleeping_.pop_front();
39 actor.waiting_synchro_ = nullptr;
40 actor.simcall_answer();
46 /** Increase the refcount for this semaphore */
47 SemaphoreImpl* SemaphoreImpl::ref()
49 intrusive_ptr_add_ref(this);
53 /** Decrease the refcount for this mutex */
54 void SemaphoreImpl::unref()
56 intrusive_ptr_release(this);
59 } // namespace activity
61 } // namespace simgrid
65 * @brief Handles a sem acquire simcall with timeout.
67 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
69 simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
70 sem->acquire(simcall->issuer_, timeout);