1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved. */
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. */
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>
13 #include "src/kernel/activity/ActivityImpl.hpp"
14 #include "src/kernel/actor/ActorImpl.hpp"
15 #include "src/kernel/actor/SimcallObserver.hpp"
17 XBT_LOG_EXTERNAL_CATEGORY(s4u);
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
22 template class xbt::Extendable<s4u::Activity>;
26 xbt::signal<void(Activity&)> Activity::on_veto;
27 xbt::signal<void(Activity&)> Activity::on_completion;
29 std::set<Activity*>* Activity::vetoed_activities_ = nullptr;
31 void Activity::destroy()
33 /* First Remove all dependencies */
34 while (not dependencies_.empty())
35 (*(dependencies_.begin()))->remove_successor(this);
36 while (not successors_.empty())
37 this->remove_successor(successors_.front());
42 void Activity::wait_until(double time_limit)
44 double now = Engine::get_clock();
46 wait_for(time_limit - now);
49 Activity* Activity::wait_for(double timeout)
51 if (state_ == State::INITED)
54 if (state_ == State::FAILED) {
55 if (dynamic_cast<Exec*>(this))
56 throw HostFailureException(XBT_THROW_POINT, "Cannot wait for a failed exec");
57 if (dynamic_cast<Io*>(this))
58 throw StorageFailureException(XBT_THROW_POINT, "Cannot wait for a failed I/O");
61 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
62 kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
63 if (kernel::actor::simcall_blocking(
64 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); }, &observer))
65 throw TimeoutException(XBT_THROW_POINT, "Timeouted");
66 complete(State::FINISHED);
72 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
73 state_ == State::CANCELED || state_ == State::FINISHED);
75 if (state_ == State::CANCELED || state_ == State::FINISHED)
78 if (state_ == State::INITED || state_ == State::STARTING)
79 this->vetoable_start();
81 if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
82 complete(State::FINISHED);
89 Activity* Activity::cancel()
91 kernel::actor::simcall([this] {
96 complete(State::CANCELED);
100 Activity* Activity::suspend()
103 return this; // Already suspended
106 if (state_ == State::STARTED)
112 Activity* Activity::resume()
115 return this; // nothing to restore when it's not suspended
117 if (state_ == State::STARTED)
123 const char* Activity::get_state_str() const
125 return to_c_str(state_);
128 double Activity::get_remaining() const
130 if (state_ == State::INITED || state_ == State::STARTING)
133 return pimpl_->get_remaining();
135 double Activity::get_start_time() const
137 return pimpl_->get_start_time();
139 double Activity::get_finish_time() const
141 return pimpl_->get_finish_time();
144 Activity* Activity::set_remaining(double remains)
146 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
147 "Cannot change the remaining amount of work once the Activity is started");
153 } // namespace simgrid