Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement MutexImpl::lock_async
[simgrid.git] / src / kernel / activity / ActivityImpl.cpp
1 /* Copyright (c) 2007-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/modelchecker.h>
7 #include <simgrid/s4u/Engine.hpp>
8
9 #include "src/kernel/activity/ActivityImpl.hpp"
10 #include "src/kernel/activity/CommImpl.hpp"
11 #include "src/kernel/activity/Synchro.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/resource/CpuImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
16
17 #include <boost/range/algorithm.hpp>
18 #include <cmath> // isfinite()
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_activity, kernel, "Kernel activity-related synchronization");
21
22 namespace simgrid {
23 namespace kernel {
24 namespace activity {
25
26 ActivityImpl::~ActivityImpl()
27 {
28   clean_action();
29   XBT_DEBUG("Destroy activity %p", this);
30 }
31
32 void ActivityImpl::register_simcall(smx_simcall_t simcall)
33 {
34   simcalls_.push_back(simcall);
35   simcall->issuer_->waiting_synchro_ = this;
36 }
37
38 void ActivityImpl::unregister_simcall(smx_simcall_t simcall)
39 {
40   // Remove the first occurrence of simcall:
41   auto j = boost::range::find(simcalls_, simcall);
42   if (j != simcalls_.end())
43     simcalls_.erase(j);
44 }
45
46 void ActivityImpl::clean_action()
47 {
48   if (surf_action_) {
49     surf_action_->unref();
50     surf_action_ = nullptr;
51   }
52 }
53
54 double ActivityImpl::get_remaining() const
55 {
56   return surf_action_ ? surf_action_->get_remains() : 0;
57 }
58
59 const char* ActivityImpl::get_state_str() const
60 {
61   return to_c_str(state_);
62 }
63
64 bool ActivityImpl::test(actor::ActorImpl* issuer)
65 {
66   if (state_ != State::WAITING && state_ != State::RUNNING) {
67     finish();
68     issuer->exception_ = nullptr; // Do not propagate exception in that case
69     return true;
70   }
71
72   if (auto* observer = dynamic_cast<kernel::actor::ActivityTestSimcall*>(issuer->simcall_.observer_))
73     observer->set_result(false);
74
75   return false;
76 }
77
78 ssize_t ActivityImpl::test_any(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities)
79 {
80   auto* observer = dynamic_cast<kernel::actor::ActivityTestanySimcall*>(issuer->simcall_.observer_);
81   xbt_assert(observer != nullptr);
82
83   if (MC_is_active() || MC_record_replay_is_active()) {
84     int idx = observer->get_value();
85     xbt_assert(idx == -1 || activities[idx]->test(issuer));
86     return idx;
87   }
88
89   for (std::size_t i = 0; i < activities.size(); ++i) {
90     if (activities[i]->test(issuer)) {
91       observer->set_result(i);
92       return i;
93     }
94   }
95   return -1;
96 }
97
98 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
99 {
100   XBT_DEBUG("Wait for execution of synchro %p, state %s", this, get_state_str());
101   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
102
103   /* Associate this simcall to the synchro */
104   register_simcall(&issuer->simcall_);
105
106   xbt_assert(not MC_is_active() && not MC_record_replay_is_active(), "MC is currently not supported here.");
107
108   /* If the synchro is already finished then perform the error handling */
109   if (state_ != State::WAITING && state_ != State::RUNNING) {
110     finish();
111   } else {
112     /* we need a sleep action (even when the timeout is infinite) to be notified of host failures */
113     /* Comms handle that a bit differently of the other activities */
114     if (auto* comm = dynamic_cast<CommImpl*>(this)) {
115       resource::Action* sleep = issuer->get_host()->get_cpu()->sleep(timeout);
116       sleep->set_activity(comm);
117
118       if (issuer == comm->src_actor_)
119         comm->src_timeout_ = sleep;
120       else
121         comm->dst_timeout_ = sleep;
122     } else {
123       SynchroImplPtr synchro(new SynchroImpl([this, issuer]() {
124         this->unregister_simcall(&issuer->simcall_);
125         issuer->waiting_synchro_ = nullptr;
126         issuer->exception_       = nullptr;
127         auto* observer           = dynamic_cast<kernel::actor::ActivityWaitSimcall*>(issuer->simcall_.observer_);
128         xbt_assert(observer != nullptr);
129         observer->set_result(true);
130       }));
131       synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
132       synchro->register_simcall(&issuer->simcall_);
133     }
134   }
135 }
136
137 void ActivityImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities, double timeout)
138 {
139   XBT_DEBUG("Wait for execution of any synchro");
140   if (MC_is_active() || MC_record_replay_is_active()) {
141     auto* observer = dynamic_cast<kernel::actor::ActivityWaitanySimcall*>(issuer->simcall_.observer_);
142     xbt_assert(observer != nullptr);
143     xbt_assert(timeout <= 0.0, "Timeout not implemented for waitany in the model-checker");
144     int idx   = observer->get_value();
145     auto* act = activities[idx];
146     act->simcalls_.push_back(&issuer->simcall_);
147     observer->set_result(idx);
148     act->set_state(State::DONE);
149     act->finish();
150     return;
151   }
152
153   if (timeout < 0.0) {
154     issuer->simcall_.timeout_cb_ = nullptr;
155   } else {
156     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &activities]() {
157       issuer->simcall_.timeout_cb_ = nullptr;
158       for (auto* act : activities)
159         act->unregister_simcall(&issuer->simcall_);
160       // default result (-1) is set in actor::ActivityWaitanySimcall
161       issuer->simcall_answer();
162     });
163   }
164
165   for (auto* act : activities) {
166     /* associate this simcall to the the synchro */
167     act->simcalls_.push_back(&issuer->simcall_);
168     /* see if the synchro is already finished */
169     if (act->get_state() != State::WAITING && act->get_state() != State::RUNNING) {
170       act->finish();
171       break;
172     }
173   }
174   XBT_DEBUG("Exit from ActivityImlp::wait_any_for");
175 }
176
177 void ActivityImpl::suspend()
178 {
179   if (surf_action_ == nullptr)
180     return;
181   XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
182   surf_action_->suspend();
183   on_suspended(*this);
184 }
185
186 void ActivityImpl::resume()
187 {
188   if (surf_action_ == nullptr)
189     return;
190   XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
191   surf_action_->resume();
192   on_resumed(*this);
193 }
194
195 void ActivityImpl::cancel()
196 {
197   XBT_VERB("Activity %p is canceled", this);
198   if (surf_action_ != nullptr)
199     surf_action_->cancel();
200   state_ = State::CANCELED;
201 }
202
203 void ActivityImpl::handle_activity_waitany(smx_simcall_t simcall)
204 {
205   /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
206    * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
207    * simcall */
208   if (auto* observer = dynamic_cast<actor::ActivityWaitanySimcall*>(simcall->observer_)) {
209     if (simcall->timeout_cb_) {
210       simcall->timeout_cb_->remove();
211       simcall->timeout_cb_ = nullptr;
212     }
213
214     auto activities = observer->get_activities();
215     for (auto* act : activities)
216       act->unregister_simcall(simcall);
217
218     if (not MC_is_active() && not MC_record_replay_is_active()) {
219       auto element   = std::find(activities.begin(), activities.end(), this);
220       int rank       = element != activities.end() ? static_cast<int>(std::distance(activities.begin(), element)) : -1;
221       observer->set_result(rank);
222     }
223   }
224 }
225
226 // boost::intrusive_ptr<Activity> support:
227 void intrusive_ptr_add_ref(ActivityImpl* activity)
228 {
229   activity->refcount_.fetch_add(1, std::memory_order_relaxed);
230 }
231
232 void intrusive_ptr_release(ActivityImpl* activity)
233 {
234   if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
235     std::atomic_thread_fence(std::memory_order_acquire);
236     delete activity;
237   }
238 }
239 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
240 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
241 }
242 }
243 } // namespace simgrid::kernel::activity::