Logo AND Algorithmique Numérique Distribuée

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