Logo AND Algorithmique Numérique Distribuée

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