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/SynchroObserver.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 /* -------- Acquisition -------- */
24 void SemAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
26 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
27 xbt_assert(issuer == issuer_, "Cannot wait on acquisitions created by another actor (id %ld)", issuer_->get_pid());
29 XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
31 this->register_simcall(&issuer_->simcall_); // Block on that acquisition
35 } else if (timeout > 0) {
36 surf_action_ = get_issuer()->get_host()->get_cpu()->sleep(timeout);
37 surf_action_->set_activity(this);
40 // Already in the queue
43 void SemAcquisitionImpl::post()
47 void SemAcquisitionImpl::finish()
49 xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
50 actor::Simcall* simcall = simcalls_.front();
51 simcalls_.pop_front();
53 if (surf_action_ != nullptr) { // A timeout was declared
54 if (surf_action_->get_state() == resource::Action::State::FINISHED) { // The timeout elapsed
55 if (granted_) { // but we got the semaphore, just in time!
56 set_state(State::DONE);
58 } else { // we have to report that timeout
59 cancel(); // Unregister the acquisition from the semaphore
61 /* Return to the englobing simcall that the wait_for timeouted */
62 auto* observer = dynamic_cast<kernel::actor::SemaphoreAcquisitionObserver*>(get_issuer()->simcall_.observer_);
63 xbt_assert(observer != nullptr);
64 observer->set_result(true);
67 surf_action_->unref();
68 surf_action_ = nullptr;
71 simcall->issuer_->waiting_synchro_ = nullptr;
72 simcall->issuer_->simcall_answer();
74 void SemAcquisitionImpl::cancel()
76 /* Remove myself from the list of interested parties */
77 auto issuer = get_issuer();
78 auto it = std::find_if(semaphore_->ongoing_acquisitions_.begin(), semaphore_->ongoing_acquisitions_.end(),
79 [issuer](SemAcquisitionImplPtr acqui) { return acqui->get_issuer() == issuer; });
80 xbt_assert(it != semaphore_->ongoing_acquisitions_.end(),
81 "Cannot find myself in the waiting queue that I have to leave");
82 semaphore_->ongoing_acquisitions_.erase(it);
85 /* -------- Semaphore -------- */
86 unsigned SemaphoreImpl::next_id_ = 0;
88 SemAcquisitionImplPtr SemaphoreImpl::acquire_async(actor::ActorImpl* issuer)
90 auto res = SemAcquisitionImplPtr(new kernel::activity::SemAcquisitionImpl(issuer, this), true);
93 /* No free token in the semaphore; register the acquisition */
94 ongoing_acquisitions_.push_back(res);
101 void SemaphoreImpl::release()
103 XBT_DEBUG("Sem release semaphore %p", this);
105 if (not ongoing_acquisitions_.empty()) {
106 /* Release the first waiting actor */
108 auto acqui = ongoing_acquisitions_.front();
109 ongoing_acquisitions_.pop_front();
111 acqui->granted_ = true;
112 if (acqui == acqui->get_issuer()->waiting_synchro_)
114 // else, the issuer is not blocked on this acquisition so no need to release it
117 // nobody's waiting here
122 } // namespace activity
123 } // namespace kernel
124 } // namespace simgrid