Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / activity / SleepImpl.cpp
1 /* Copyright (c) 2007-2023. 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/s4u/Host.hpp>
7
8 #include "src/kernel/activity/SleepImpl.hpp"
9 #include "src/kernel/actor/ActorImpl.hpp"
10 #include "src/kernel/resource/CpuImpl.hpp"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_actor);
13
14 namespace simgrid::kernel::activity {
15
16 SleepImpl& SleepImpl::set_host(s4u::Host* host)
17 {
18   host_ = host;
19   return *this;
20 }
21
22 SleepImpl& SleepImpl::set_duration(double duration)
23 {
24   duration_ = duration;
25   return *this;
26 }
27
28 SleepImpl* SleepImpl::start()
29 {
30   model_action_ = host_->get_cpu()->sleep(duration_);
31   model_action_->set_activity(this);
32   XBT_DEBUG("Create sleep synchronization %p", this);
33   return this;
34 }
35
36 void SleepImpl::set_exception(actor::ActorImpl* issuer)
37 {
38   /* FIXME: Really, nothing bad can happen while we sleep? */
39 }
40 void SleepImpl::finish()
41 {
42   if (model_action_->get_state() == resource::Action::State::FAILED) {
43     if (host_ && not host_->is_on())
44       set_state(State::SRC_HOST_FAILURE);
45     else
46       set_state(State::CANCELED);
47   } else if (model_action_->get_state() == resource::Action::State::FINISHED) {
48     set_state(State::DONE);
49   }
50
51   clean_action();
52   XBT_DEBUG("SleepImpl::finish() in state %s", get_state_str());
53   while (not simcalls_.empty()) {
54     const actor::Simcall* simcall = simcalls_.front();
55     simcalls_.pop_front();
56
57     simcall->issuer_->waiting_synchro_ = nullptr;
58     if (simcall->issuer_->is_suspended()) {
59       XBT_DEBUG("Wait! This actor is suspended and can't wake up now.");
60       simcall->issuer_->suspended_ = false;
61       simcall->issuer_->suspend();
62     } else {
63       simcall->issuer_->simcall_answer();
64     }
65   }
66 }
67 } // namespace simgrid::kernel::activity