Logo AND Algorithmique Numérique Distribuée

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