Logo AND Algorithmique Numérique Distribuée

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