Logo AND Algorithmique Numérique Distribuée

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