Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3072fb9ac3749a1737cf3c5ad240aff14e18cbbb
[simgrid.git] / src / kernel / activity / SleepImpl.cpp
1 /* Copyright (c) 2007-2019. 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_name(const std::string& name)
22 {
23   ActivityImpl::set_name(name);
24   return *this;
25 }
26
27 SleepImpl& SleepImpl::set_host(s4u::Host* host)
28 {
29   host_ = host;
30   return *this;
31 }
32
33 SleepImpl& SleepImpl::set_duration(double duration)
34 {
35   duration_ = duration;
36   return *this;
37 }
38
39 SleepImpl* SleepImpl::start()
40 {
41   surf_action_ = host_->pimpl_cpu->sleep(duration_);
42   surf_action_->set_data(this);
43   XBT_DEBUG("Create sleep synchronization %p", this);
44   return this;
45 }
46
47 void SleepImpl::post()
48 {
49   while (not simcalls_.empty()) {
50     smx_simcall_t simcall = simcalls_.front();
51     simcalls_.pop_front();
52     e_smx_state_t result;
53     if (host_ && not host_->is_on()) {
54       /* If the host running the synchro failed, notice it. This way, the asking
55        * actor can be killed if it runs on that host itself */
56       result = SIMIX_SRC_HOST_FAILURE;
57       simcall->issuer->throw_exception(
58           std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed")));
59     }
60
61     switch (surf_action_->get_state()) {
62       case resource::Action::State::FAILED:
63         simcall->issuer->context_->iwannadie = true;
64         result                               = SIMIX_FAILED;
65         break;
66
67       case resource::Action::State::FINISHED:
68         result = SIMIX_DONE;
69         break;
70
71       default:
72         THROW_IMPOSSIBLE;
73     }
74     if (not simcall->issuer->get_host()->is_on()) {
75       simcall->issuer->context_->iwannadie = true;
76     }
77     simcall_process_sleep__set__result(simcall, result);
78     simcall->issuer->waiting_synchro = nullptr;
79     if (simcall->issuer->is_suspended()) {
80       XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
81       simcall->issuer->suspended_ = false;
82       simcall_HANDLER_process_suspend(simcall, simcall->issuer);
83     } else {
84       SIMIX_simcall_answer(simcall);
85     }
86   }
87   SIMIX_process_sleep_destroy(this);
88 }
89 void SleepImpl::finish()
90 {
91   /* FIXME some part of post should move to finish */
92 }
93 } // namespace activity
94 } // namespace kernel
95 } // namespace simgrid