Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement test with wait_for...
[simgrid.git] / src / s4u / s4u_Activity.cpp
1 /* Copyright (c) 2006-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/Activity.hpp>
8 #include <simgrid/s4u/Comm.hpp>
9 #include <simgrid/s4u/Engine.hpp>
10 #include <simgrid/s4u/Exec.hpp>
11 #include <simgrid/s4u/Io.hpp>
12 #include <simgrid/s4u/Mess.hpp>
13 #include <xbt/log.h>
14
15 #include "src/kernel/activity/ActivityImpl.hpp"
16 #include "src/kernel/actor/ActorImpl.hpp"
17 #include "src/kernel/actor/CommObserver.hpp"
18
19 XBT_LOG_EXTERNAL_CATEGORY(s4u);
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
21
22 namespace simgrid {
23
24 template class xbt::Extendable<s4u::Activity>;
25
26 namespace s4u {
27
28 std::set<Activity*>* Activity::vetoed_activities_ = nullptr;
29
30 void Activity::destroy()
31 {
32   /* First Remove all dependencies */
33   while (not dependencies_.empty())
34     (*(dependencies_.begin()))->remove_successor(this);
35   while (not successors_.empty())
36     this->remove_successor(successors_.front());
37
38   cancel();
39 }
40
41 void Activity::wait_until(double time_limit)
42 {
43   double now = Engine::get_clock();
44   if (time_limit > now)
45     wait_for(time_limit - now);
46 }
47
48 Activity* Activity::wait_for(double timeout)
49 {
50   if (state_ == State::INITED)
51     start();
52
53   if (state_ == State::FAILED) {
54     if (dynamic_cast<Comm*>(this))
55       throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed comm");
56     if (dynamic_cast<Mess*>(this))
57       throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed mess");
58     if (dynamic_cast<Exec*>(this))
59       throw HostFailureException(XBT_THROW_POINT, "Cannot wait for a failed exec");
60     if (dynamic_cast<Io*>(this))
61       throw StorageFailureException(XBT_THROW_POINT, "Cannot wait for a failed I/O");
62     THROW_IMPOSSIBLE;
63   }
64
65   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
66   kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout, "wait_for"};
67   if (kernel::actor::simcall_blocking(
68           [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); }, &observer))
69     throw TimeoutException(XBT_THROW_POINT, "Timeouted");
70   complete(State::FINISHED);
71   return this;
72 }
73
74 bool Activity::test()
75 {
76   try {
77     wait_for(0.0);
78   } catch (const simgrid::TimeoutException&) {
79     return false;
80   } catch (const simgrid::Exception& e) {
81     XBT_DEBUG("Ignored exception: %s", e.what());
82   }
83   return true;
84 }
85
86 ssize_t Activity::test_any(const std::vector<ActivityPtr>& activities) // XBT_ATTRIB_DEPRECATED_v339
87 {
88   std::vector<kernel::activity::ActivityImpl*> ractivities(activities.size());
89   std::transform(begin(activities), end(activities), begin(ractivities),
90                  [](const ActivityPtr& act) { return act->pimpl_.get(); });
91
92   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
93   kernel::actor::ActivityTestanySimcall observer{issuer, ractivities, "test_any"};
94   ssize_t changed_pos = kernel::actor::simcall_answered(
95       [&observer] {
96         return kernel::activity::ActivityImpl::test_any(observer.get_issuer(), observer.get_activities());
97       },
98       &observer);
99   if (changed_pos != -1)
100     activities.at(changed_pos)->complete(State::FINISHED);
101   return changed_pos;
102 }
103
104 ssize_t Activity::deprecated_wait_any_for(const std::vector<ActivityPtr>& activities, double timeout) // XBT_ATTRIB_DEPRECATED_v339
105 {
106   std::vector<kernel::activity::ActivityImpl*> ractivities(activities.size());
107   std::transform(begin(activities), end(activities), begin(ractivities),
108                  [](const ActivityPtr& activity) { return activity->pimpl_.get(); });
109
110   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
111   kernel::actor::ActivityWaitanySimcall observer{issuer, ractivities, timeout, "wait_any_for"};
112   ssize_t changed_pos = kernel::actor::simcall_blocking(
113       [&observer] {
114         kernel::activity::ActivityImpl::wait_any_for(observer.get_issuer(), observer.get_activities(),
115                                                      observer.get_timeout());
116       },
117       &observer);
118   if (changed_pos != -1)
119     activities.at(changed_pos)->complete(State::FINISHED);
120   return changed_pos;
121 }
122
123 Activity* Activity::cancel()
124 {
125   kernel::actor::simcall_answered([this] {
126     XBT_HERE();
127     if (pimpl_)
128       pimpl_->cancel();
129   });
130   complete(State::CANCELED);
131   return this;
132 }
133
134 Activity* Activity::suspend()
135 {
136   if (suspended_)
137     return this; // Already suspended
138   suspended_ = true;
139
140   if (state_ == State::STARTED)
141     pimpl_->suspend();
142
143   return this;
144 }
145
146 Activity* Activity::resume()
147 {
148   if (not suspended_)
149     return this; // nothing to restore when it's not suspended
150
151   if (state_ == State::STARTED)
152     pimpl_->resume();
153
154   return this;
155 }
156
157 const char* Activity::get_state_str() const
158 {
159   return to_c_str(state_);
160 }
161
162 double Activity::get_remaining() const
163 {
164   if (state_ == State::INITED || state_ == State::STARTING)
165     return remains_;
166   else
167     return pimpl_->get_remaining();
168 }
169 double Activity::get_start_time() const
170 {
171   return pimpl_->get_start_time();
172 }
173 double Activity::get_finish_time() const
174 {
175   return pimpl_->get_finish_time();
176 }
177
178 Activity* Activity::set_remaining(double remains)
179 {
180   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
181              "Cannot change the remaining amount of work once the Activity is started");
182   remains_ = remains;
183   return this;
184 }
185
186 } // namespace s4u
187 } // namespace simgrid