Logo AND Algorithmique Numérique Distribuée

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