Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
61fd9456660f500373eb8b3a93d6ee32c19b0a7d
[simgrid.git] / src / kernel / activity / SemaphoreImpl.cpp
1 /* Copyright (c) 2019-2022. 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 {
19 namespace kernel {
20 namespace activity {
21
22 /* -------- Acquisition -------- */
23
24 void SemAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
25 {
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());
28
29   XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
30
31   this->register_simcall(&issuer_->simcall_); // Block on that acquisition
32
33   if (granted_) {
34     post();
35   } else if (timeout > 0) {
36     surf_action_ = get_issuer()->get_host()->get_cpu()->sleep(timeout);
37     surf_action_->set_activity(this);
38
39   } else {
40     // Already in the queue
41   }
42 }
43 void SemAcquisitionImpl::post()
44 {
45   finish();
46 }
47 void SemAcquisitionImpl::finish()
48 {
49   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
50   smx_simcall_t simcall = simcalls_.front();
51   simcalls_.pop_front();
52
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);
57
58       } else { // we have to report that timeout
59         cancel(); // Unregister the acquisition from the semaphore
60
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);
65       }
66     }
67     surf_action_->unref();
68     surf_action_ = nullptr;
69   }
70
71   simcall->issuer_->waiting_synchro_ = nullptr;
72   simcall->issuer_->simcall_answer();
73 }
74 void SemAcquisitionImpl::cancel()
75 {
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);
83 }
84
85 /* -------- Semaphore -------- */
86 unsigned SemaphoreImpl::next_id_ = 0;
87
88 SemAcquisitionImplPtr SemaphoreImpl::acquire_async(actor::ActorImpl* issuer)
89 {
90   auto res = SemAcquisitionImplPtr(new kernel::activity::SemAcquisitionImpl(issuer, this), true);
91
92   if (value_ <= 0) {
93     /* No free token in the semaphore; register the acquisition */
94     ongoing_acquisitions_.push_back(res);
95   } else {
96     value_--;
97     res->granted_ = true;
98   }
99   return res;
100 }
101 void SemaphoreImpl::release()
102 {
103   XBT_DEBUG("Sem release semaphore %p", this);
104
105   if (not ongoing_acquisitions_.empty()) {
106     /* Release the first waiting actor */
107
108     auto acqui = ongoing_acquisitions_.front();
109     ongoing_acquisitions_.pop_front();
110
111     acqui->granted_ = true;
112     if (acqui == acqui->get_issuer()->waiting_synchro_)
113       acqui->post();
114     // else, the issuer is not blocked on this acquisition so no need to release it
115
116   } else {
117     // nobody's waiting here
118     value_++;
119   }
120 }
121
122 } // namespace activity
123 } // namespace kernel
124 } // namespace simgrid