Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Activity refactoring
[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/actor/ActorImpl.hpp"
14 #include "src/kernel/actor/SimcallObserver.hpp"
15
16 XBT_LOG_EXTERNAL_CATEGORY(s4u);
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
18
19 namespace simgrid {
20 namespace s4u {
21
22 xbt::signal<void(Activity&)> Activity::on_veto;
23 xbt::signal<void(Activity&)> Activity::on_completion;
24
25 std::set<Activity*>* Activity::vetoed_activities_ = nullptr;
26
27 void Activity::wait_until(double time_limit)
28 {
29   double now = Engine::get_clock();
30   if (time_limit > now)
31     wait_for(time_limit - now);
32 }
33
34 Activity* Activity::wait_for(double timeout)
35 {
36   if (state_ == State::INITED)
37     vetoable_start();
38
39   if (state_ == State::FAILED) {
40     if (dynamic_cast<Exec*>(this))
41       throw HostFailureException(XBT_THROW_POINT, "Cannot wait for a failed exec");
42     if (dynamic_cast<Io*>(this))
43       throw StorageFailureException(XBT_THROW_POINT, "Cannot wait for a failed I/O");
44   }
45
46   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
47   kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
48   if (kernel::actor::simcall_blocking(
49           [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); }, &observer))
50     throw TimeoutException(XBT_THROW_POINT, "Timeouted");
51   complete(State::FINISHED);
52   return this;
53 }
54
55 bool Activity::test()
56 {
57   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
58              state_ == State::CANCELED || state_ == State::FINISHED);
59
60   if (state_ == State::CANCELED || state_ == State::FINISHED)
61     return true;
62
63   if (state_ == State::INITED || state_ == State::STARTING)
64     this->vetoable_start();
65
66   if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
67     complete(State::FINISHED);
68     return true;
69   }
70
71   return false;
72 }
73
74 Activity* Activity::cancel()
75 {
76   kernel::actor::simcall([this] {
77     XBT_HERE();
78     pimpl_->cancel();
79   });
80   complete(State::CANCELED);
81   return this;
82 }
83
84 Activity* Activity::suspend()
85 {
86   if (suspended_)
87     return this; // Already suspended
88   suspended_ = true;
89
90   if (state_ == State::STARTED)
91     pimpl_->suspend();
92
93   return this;
94 }
95
96 Activity* Activity::resume()
97 {
98   if (not suspended_)
99     return this; // nothing to restore when it's not suspended
100
101   if (state_ == State::STARTED)
102     pimpl_->resume();
103
104   return this;
105 }
106
107 const char* Activity::get_state_str() const
108 {
109   return to_c_str(state_);
110 }
111
112 double Activity::get_remaining() const
113 {
114   if (state_ == State::INITED || state_ == State::STARTING)
115     return remains_;
116   else
117     return pimpl_->get_remaining();
118 }
119
120 Activity* Activity::set_remaining(double remains)
121 {
122   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
123              "Cannot change the remaining amount of work once the Activity is started");
124   remains_ = remains;
125   return this;
126 }
127
128 } // namespace s4u
129 } // namespace simgrid