Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::fill instead of memset.
[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::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   surf_action_ = host_->get_cpu()->sleep(duration_);
31   surf_action_->set_activity(this);
32   XBT_DEBUG("Create sleep synchronization %p", this);
33   return this;
34 }
35
36 void SleepImpl::post()
37 {
38   if (surf_action_->get_state() == resource::Action::State::FAILED) {
39     if (host_ && not host_->is_on())
40       set_state(State::SRC_HOST_FAILURE);
41     else
42       set_state(State::CANCELED);
43   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
44     set_state(State::DONE);
45   }
46
47   clean_action();
48   /* Answer all simcalls associated with the synchro */
49   finish();
50 }
51 void SleepImpl::set_exception(actor::ActorImpl* issuer)
52 {
53   /* FIXME: Really, nothing bad can happen while we sleep? */
54 }
55 void SleepImpl::finish()
56 {
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();
61
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();
67     } else {
68       simcall->issuer_->simcall_answer();
69     }
70   }
71 }
72 } // namespace simgrid::kernel::activity