Logo AND Algorithmique Numérique Distribuée

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