Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
forgot that in making Activities extendable. Should please Asan
[simgrid.git] / src / s4u / s4u_Activity.cpp
1 /* Copyright (c) 2006-2021. 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/Engine.hpp>
9 #include <simgrid/s4u/Exec.hpp>
10 #include <simgrid/s4u/Io.hpp>
11 #include <xbt/log.h>
12
13 #include "src/kernel/activity/ActivityImpl.hpp"
14 #include "src/kernel/actor/ActorImpl.hpp"
15 #include "src/kernel/actor/SimcallObserver.hpp"
16
17 XBT_LOG_EXTERNAL_CATEGORY(s4u);
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
19
20 namespace simgrid {
21
22 template class xbt::Extendable<s4u::Activity>;
23
24 namespace s4u {
25
26 xbt::signal<void(Activity&)> Activity::on_veto;
27 xbt::signal<void(Activity&)> Activity::on_completion;
28
29 std::set<Activity*>* Activity::vetoed_activities_ = nullptr;
30
31 void Activity::destroy()
32 {
33   /* First Remove all dependencies */
34   while (not dependencies_.empty())
35     (*(dependencies_.begin()))->remove_successor(this);
36   while (not successors_.empty())
37     this->remove_successor(successors_.front());
38
39   cancel();
40 }
41
42 void Activity::wait_until(double time_limit)
43 {
44   double now = Engine::get_clock();
45   if (time_limit > now)
46     wait_for(time_limit - now);
47 }
48
49 Activity* Activity::wait_for(double timeout)
50 {
51   if (state_ == State::INITED)
52     vetoable_start();
53
54   if (state_ == State::FAILED) {
55     if (dynamic_cast<Exec*>(this))
56       throw HostFailureException(XBT_THROW_POINT, "Cannot wait for a failed exec");
57     if (dynamic_cast<Io*>(this))
58       throw StorageFailureException(XBT_THROW_POINT, "Cannot wait for a failed I/O");
59   }
60
61   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
62   kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
63   if (kernel::actor::simcall_blocking(
64           [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); }, &observer))
65     throw TimeoutException(XBT_THROW_POINT, "Timeouted");
66   complete(State::FINISHED);
67   return this;
68 }
69
70 bool Activity::test()
71 {
72   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
73              state_ == State::CANCELED || state_ == State::FINISHED);
74
75   if (state_ == State::CANCELED || state_ == State::FINISHED)
76     return true;
77
78   if (state_ == State::INITED || state_ == State::STARTING)
79     this->vetoable_start();
80
81   if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
82     complete(State::FINISHED);
83     return true;
84   }
85
86   return false;
87 }
88
89 Activity* Activity::cancel()
90 {
91   kernel::actor::simcall([this] {
92     XBT_HERE();
93     if (pimpl_)
94       pimpl_->cancel();
95   });
96   complete(State::CANCELED);
97   return this;
98 }
99
100 Activity* Activity::suspend()
101 {
102   if (suspended_)
103     return this; // Already suspended
104   suspended_ = true;
105
106   if (state_ == State::STARTED)
107     pimpl_->suspend();
108
109   return this;
110 }
111
112 Activity* Activity::resume()
113 {
114   if (not suspended_)
115     return this; // nothing to restore when it's not suspended
116
117   if (state_ == State::STARTED)
118     pimpl_->resume();
119
120   return this;
121 }
122
123 const char* Activity::get_state_str() const
124 {
125   return to_c_str(state_);
126 }
127
128 double Activity::get_remaining() const
129 {
130   if (state_ == State::INITED || state_ == State::STARTING)
131     return remains_;
132   else
133     return pimpl_->get_remaining();
134 }
135 double Activity::get_start_time() const
136 {
137   return pimpl_->get_start_time();
138 }
139 double Activity::get_finish_time() const
140 {
141   return pimpl_->get_finish_time();
142 }
143
144 Activity* Activity::set_remaining(double remains)
145 {
146   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
147              "Cannot change the remaining amount of work once the Activity is started");
148   remains_ = remains;
149   return this;
150 }
151
152 } // namespace s4u
153 } // namespace simgrid