Logo AND Algorithmique Numérique Distribuée

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