1 /* Copyright (c) 2007-2023. 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/s4u/Host.hpp>
8 #include "src/kernel/activity/SleepImpl.hpp"
9 #include "src/kernel/actor/ActorImpl.hpp"
10 #include "src/kernel/resource/CpuImpl.hpp"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_actor);
14 namespace simgrid::kernel::activity {
16 SleepImpl& SleepImpl::set_host(s4u::Host* host)
22 SleepImpl& SleepImpl::set_duration(double duration)
28 SleepImpl* SleepImpl::start()
30 surf_action_ = host_->get_cpu()->sleep(duration_);
31 surf_action_->set_activity(this);
32 XBT_DEBUG("Create sleep synchronization %p", this);
36 void SleepImpl::post()
38 if (surf_action_->get_state() == resource::Action::State::FAILED) {
39 if (host_ && not host_->is_on())
40 set_state(State::SRC_HOST_FAILURE);
42 set_state(State::CANCELED);
43 } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
44 set_state(State::DONE);
48 /* Answer all simcalls associated with the synchro */
51 void SleepImpl::set_exception(actor::ActorImpl* issuer)
53 /* FIXME: Really, nothing bad can happen while we sleep? */
55 void SleepImpl::finish()
57 XBT_DEBUG("SleepImpl::finish() in state %s", get_state_str());
58 while (not simcalls_.empty()) {
59 const actor::Simcall* simcall = simcalls_.front();
60 simcalls_.pop_front();
62 simcall->issuer_->waiting_synchro_ = nullptr;
63 if (simcall->issuer_->is_suspended()) {
64 XBT_DEBUG("Wait! This actor is suspended and can't wake up now.");
65 simcall->issuer_->suspended_ = false;
66 simcall->issuer_->suspend();
68 simcall->issuer_->simcall_answer();
72 } // namespace simgrid::kernel::activity