Logo AND Algorithmique Numérique Distribuée

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