Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deprecate the is_off() methods.
[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
12 #include "simgrid/Exception.hpp"
13 #include "src/simix/ActorImpl.hpp"
14 #include "src/simix/popping_private.hpp"
15 #include "src/simix/smx_private.hpp"
16 #include "src/surf/cpu_interface.hpp"
17 #include "src/surf/surf_interface.hpp"
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
20 namespace simgrid {
21 namespace kernel {
22 namespace activity {
23
24 SleepImpl::~SleepImpl()
25 {
26   if (surf_action_)
27     surf_action_->unref();
28   XBT_DEBUG("Destroy activity %p", this);
29 }
30
31 SleepImpl* SleepImpl::start(double duration)
32 {
33   surf_action_ = host_->pimpl_cpu->sleep(duration);
34   surf_action_->set_data(this);
35   XBT_DEBUG("Create sleep synchronization %p", this);
36   return this;
37 }
38
39 void SleepImpl::post()
40 {
41   while (not simcalls_.empty()) {
42     smx_simcall_t simcall = simcalls_.front();
43     simcalls_.pop_front();
44     e_smx_state_t result;
45     if (host_ && not host_->is_on()) {
46       /* If the host running the synchro failed, notice it. This way, the asking
47        * actor can be killed if it runs on that host itself */
48       result = SIMIX_SRC_HOST_FAILURE;
49       simcall->issuer->throw_exception(
50           std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed")));
51     }
52
53     switch (surf_action_->get_state()) {
54       case resource::Action::State::FAILED:
55         simcall->issuer->context_->iwannadie = true;
56         result                               = SIMIX_FAILED;
57         break;
58
59       case resource::Action::State::FINISHED:
60         result = SIMIX_DONE;
61         break;
62
63       default:
64         THROW_IMPOSSIBLE;
65         break;
66     }
67     if (not simcall->issuer->host_->is_on()) {
68       simcall->issuer->context_->iwannadie = true;
69     }
70     simcall_process_sleep__set__result(simcall, result);
71     simcall->issuer->waiting_synchro = nullptr;
72     if (simcall->issuer->suspended_) {
73       XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
74       simcall->issuer->suspended_ = false;
75       simcall_HANDLER_process_suspend(simcall, simcall->issuer);
76     } else {
77       SIMIX_simcall_answer(simcall);
78     }
79   }
80
81   SIMIX_process_sleep_destroy(this);
82 }
83
84 } // namespace activity
85 } // namespace kernel
86 } // namespace simgrid