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. */
8 #include "simgrid/Exception.hpp"
9 #include "simgrid/s4u/Activity.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "src/kernel/activity/ActivityImpl.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
15 XBT_LOG_EXTERNAL_CATEGORY(s4u);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
21 void Activity::wait_until(double time_limit)
23 double now = Engine::get_clock();
25 wait_for(time_limit - now);
28 Activity* Activity::wait_for(double timeout)
30 if (state_ == State::INITED)
33 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
34 kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
35 if (kernel::actor::simcall_blocking(
36 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); }, &observer))
37 throw TimeoutException(XBT_THROW_POINT, "Timeouted");
38 complete(State::FINISHED);
44 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
45 state_ == State::CANCELED || state_ == State::FINISHED);
47 if (state_ == State::CANCELED || state_ == State::FINISHED)
50 if (state_ == State::INITED || state_ == State::STARTING)
51 this->vetoable_start();
53 if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
54 complete(State::FINISHED);
61 Activity* Activity::cancel()
63 kernel::actor::simcall([this] {
67 complete(State::CANCELED);
71 Activity* Activity::suspend()
74 return this; // Already suspended
77 if (state_ == State::STARTED)
83 Activity* Activity::resume()
86 return this; // nothing to restore when it's not suspended
88 if (state_ == State::STARTED)
94 const char* Activity::get_state_str() const
96 return to_c_str(state_);
99 double Activity::get_remaining() const
101 if (state_ == State::INITED || state_ == State::STARTING)
104 return pimpl_->get_remaining();
107 Activity* Activity::set_remaining(double remains)
109 xbt_assert(state_ == State::INITED, "Cannot change the remaining amount of work once the Activity is started");
115 } // namespace simgrid