Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[CR] Minor improvement.
[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     pimpl_->cancel();
80   });
81   complete(State::CANCELED);
82   return this;
83 }
84
85 Activity* Activity::suspend()
86 {
87   if (suspended_)
88     return this; // Already suspended
89   suspended_ = true;
90
91   if (state_ == State::STARTED)
92     pimpl_->suspend();
93
94   return this;
95 }
96
97 Activity* Activity::resume()
98 {
99   if (not suspended_)
100     return this; // nothing to restore when it's not suspended
101
102   if (state_ == State::STARTED)
103     pimpl_->resume();
104
105   return this;
106 }
107
108 const char* Activity::get_state_str() const
109 {
110   return to_c_str(state_);
111 }
112
113 double Activity::get_remaining() const
114 {
115   if (state_ == State::INITED || state_ == State::STARTING)
116     return remains_;
117   else
118     return pimpl_->get_remaining();
119 }
120 double Activity::get_start_time() const
121 {
122   return pimpl_->get_start_time();
123 }
124 double Activity::get_finish_time() const
125 {
126   return pimpl_->get_finish_time();
127 }
128
129 Activity* Activity::set_remaining(double remains)
130 {
131   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
132              "Cannot change the remaining amount of work once the Activity is started");
133   remains_ = remains;
134   return this;
135 }
136
137 } // namespace s4u
138 } // namespace simgrid