Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / activity / SemaphoreImpl.cpp
1 /* Copyright (c) 2019-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/s4u/Host.hpp>
8
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"
13
14 #include <cmath> // std::isfinite
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_semaphore, ker_synchro, "Semaphore kernel-space implementation");
17
18 namespace simgrid::kernel::activity {
19
20 /* -------- Acquisition -------- */
21
22 void SemAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
23 {
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());
26
27   XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
28
29   this->register_simcall(&issuer_->simcall_); // Block on that acquisition
30
31   if (granted_) {
32     finish();
33   } else if (timeout > 0) {
34     model_action_ = get_issuer()->get_host()->get_cpu()->sleep(timeout);
35     model_action_->set_activity(this);
36
37   } else {
38     // Already in the queue
39   }
40 }
41 void SemAcquisitionImpl::finish()
42 {
43   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
44   actor::Simcall* simcall = simcalls_.front();
45   simcalls_.pop_front();
46
47   if (model_action_ != nullptr) {                                          // A timeout was declared
48     if (model_action_->get_state() == resource::Action::State::FINISHED) { // The timeout elapsed
49       if (granted_) { // but we got the semaphore, just in time!
50         set_state(State::DONE);
51
52       } else { // we have to report that timeout
53         cancel(); // Unregister the acquisition from the semaphore
54
55         /* Return to the englobing simcall that the wait_for timeouted */
56         auto* observer = dynamic_cast<kernel::actor::SemaphoreAcquisitionObserver*>(get_issuer()->simcall_.observer_);
57         xbt_assert(observer != nullptr);
58         observer->set_result(true);
59       }
60     }
61     model_action_->unref();
62     model_action_ = nullptr;
63   }
64
65   simcall->issuer_->waiting_synchro_ = nullptr;
66   simcall->issuer_->simcall_answer();
67 }
68 void SemAcquisitionImpl::cancel()
69 {
70   /* Remove myself from the list of interested parties */
71   const auto* issuer = get_issuer();
72   auto it     = std::find_if(semaphore_->ongoing_acquisitions_.begin(), semaphore_->ongoing_acquisitions_.end(),
73                              [issuer](SemAcquisitionImplPtr acqui) { return acqui->get_issuer() == issuer; });
74   xbt_assert(it != semaphore_->ongoing_acquisitions_.end(),
75              "Cannot find myself in the waiting queue that I have to leave");
76   semaphore_->ongoing_acquisitions_.erase(it);
77 }
78
79 /* -------- Semaphore -------- */
80 unsigned SemaphoreImpl::next_id_ = 0;
81
82 SemAcquisitionImplPtr SemaphoreImpl::acquire_async(actor::ActorImpl* issuer)
83 {
84   auto res = SemAcquisitionImplPtr(new kernel::activity::SemAcquisitionImpl(issuer, this), true);
85
86   if (value_ > 0) {
87     value_--;
88     res->granted_ = true;
89   } else {
90     /* No free token in the semaphore; register the acquisition */
91     ongoing_acquisitions_.push_back(res);
92   }
93   return res;
94 }
95 void SemaphoreImpl::release()
96 {
97   XBT_DEBUG("Sem release semaphore %p", this);
98
99   if (not ongoing_acquisitions_.empty()) {
100     /* Release the first waiting actor */
101
102     auto acqui = ongoing_acquisitions_.front();
103     ongoing_acquisitions_.pop_front();
104
105     acqui->granted_ = true;
106     if (acqui == acqui->get_issuer()->waiting_synchro_)
107       acqui->finish();
108     // else, the issuer is not blocked on this acquisition so no need to release it
109
110   } else {
111     // nobody's waiting here
112     value_++;
113   }
114 }
115
116 } // namespace simgrid::kernel::activity