1 /* Copyright (c) 2006-2021. 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");
23 xbt::signal<void(Activity&)> Activity::on_veto;
24 xbt::signal<void(Activity&)> Activity::on_completion;
26 std::set<Activity*>* Activity::vetoed_activities_ = nullptr;
28 void Activity::wait_until(double time_limit)
30 double now = Engine::get_clock();
32 wait_for(time_limit - now);
35 Activity* Activity::wait_for(double timeout)
37 if (state_ == State::INITED)
40 if (state_ == State::FAILED) {
41 if (dynamic_cast<Exec*>(this))
42 throw HostFailureException(XBT_THROW_POINT, "Cannot wait for a failed exec");
43 if (dynamic_cast<Io*>(this))
44 throw StorageFailureException(XBT_THROW_POINT, "Cannot wait for a failed I/O");
47 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
48 kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
49 if (kernel::actor::simcall_blocking(
50 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); }, &observer))
51 throw TimeoutException(XBT_THROW_POINT, "Timeouted");
52 complete(State::FINISHED);
58 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
59 state_ == State::CANCELED || state_ == State::FINISHED);
61 if (state_ == State::CANCELED || state_ == State::FINISHED)
64 if (state_ == State::INITED || state_ == State::STARTING)
65 this->vetoable_start();
67 if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
68 complete(State::FINISHED);
75 Activity* Activity::cancel()
77 kernel::actor::simcall([this] {
81 complete(State::CANCELED);
85 Activity* Activity::suspend()
88 return this; // Already suspended
91 if (state_ == State::STARTED)
97 Activity* Activity::resume()
100 return this; // nothing to restore when it's not suspended
102 if (state_ == State::STARTED)
108 const char* Activity::get_state_str() const
110 return to_c_str(state_);
113 double Activity::get_remaining() const
115 if (state_ == State::INITED || state_ == State::STARTING)
118 return pimpl_->get_remaining();
120 double Activity::get_start_time() const
122 return pimpl_->get_start_time();
124 double Activity::get_finish_time() const
126 return pimpl_->get_finish_time();
129 Activity* Activity::set_remaining(double remains)
131 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
132 "Cannot change the remaining amount of work once the Activity is started");
138 } // namespace simgrid