1 /* Copyright (c) 2019-2022. 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 <simgrid/Exception.hpp>
7 #include <simgrid/s4u/Host.hpp>
9 #include "src/kernel/activity/SemaphoreImpl.hpp"
10 #include "src/kernel/activity/Synchro.hpp"
11 #include "src/kernel/actor/SimcallObserver.hpp"
12 #include "src/kernel/resource/CpuImpl.hpp"
14 #include <cmath> // std::isfinite
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_semaphore, ker_synchro, "Semaphore kernel-space implementation");
22 void SemAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
24 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
25 xbt_assert(issuer == issuer_, "Cannot wait on acquisitions created by another actor (id %ld)", issuer_->get_pid());
27 XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
29 this->register_simcall(&issuer_->simcall_); // Block on that acquisition
33 } else if (timeout > 0) {
34 surf_action_ = get_issuer()->get_host()->get_cpu()->sleep(timeout);
35 surf_action_->set_activity(this);
38 // Already in the queue
41 void SemAcquisitionImpl::post()
45 void SemAcquisitionImpl::finish()
47 xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
48 smx_simcall_t simcall = simcalls_.front();
49 simcalls_.pop_front();
51 if (surf_action_ != nullptr) { // A timeout was declared
52 if (surf_action_->get_state() == resource::Action::State::FINISHED) { // The timeout elapsed
53 if (granted_) { // but we got the semaphore, just in time!
54 set_state(State::DONE);
56 } else { // we have to report that timeout
57 /* Remove myself from the list of interested parties */
58 auto issuer = get_issuer();
59 auto it = std::find_if(semaphore_->sleeping_.begin(), semaphore_->sleeping_.end(),
60 [issuer](SemAcquisitionImplPtr acqui) { return acqui->get_issuer() == issuer; });
61 xbt_assert(it != semaphore_->sleeping_.end(), "Cannot find myself in the waiting queue that I have to leave");
62 semaphore_->sleeping_.erase(it);
64 /* Return to the englobing simcall that the wait_for timeouted */
65 auto* observer = dynamic_cast<kernel::actor::SemAcquireSimcall*>(issuer->simcall_.observer_);
66 xbt_assert(observer != nullptr);
67 observer->set_result(true);
70 surf_action_->unref();
71 surf_action_ = nullptr;
74 simcall->issuer_->waiting_synchro_ = nullptr;
75 simcall->issuer_->simcall_answer();
78 SemAcquisitionImplPtr SemaphoreImpl::acquire_async(actor::ActorImpl* issuer)
80 auto res = SemAcquisitionImplPtr(new kernel::activity::SemAcquisitionImpl(issuer, this), true);
83 /* No free token in the semaphore; register the acquisition */
84 sleeping_.push_back(res);
91 void SemaphoreImpl::release()
93 XBT_DEBUG("Sem release semaphore %p", this);
95 if (not sleeping_.empty()) {
96 /* Release the first waiting actor */
98 auto acqui = sleeping_.front();
99 sleeping_.pop_front();
101 acqui->granted_ = true;
102 if (acqui == acqui->get_issuer()->waiting_synchro_)
104 // else, the issuer is not blocked on this acquisition so no need to release it
107 // nobody's waiting here
112 } // namespace activity
113 } // namespace kernel
114 } // namespace simgrid