Logo AND Algorithmique Numérique Distribuée

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