Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / kernel / activity / SynchroRaw.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/Exception.hpp>
7 #include <simgrid/s4u/Host.hpp>
8
9 #include "src/kernel/activity/SynchroRaw.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/context/Context.hpp"
12 #include "src/kernel/resource/CpuImpl.hpp"
13 #include "src/simix/popping_private.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_synchro, kernel,
16                                 "Kernel synchronization mechanisms (mutex, semaphores and conditions)");
17
18 namespace simgrid {
19 namespace kernel {
20 namespace activity {
21
22 RawImpl& RawImpl::set_host(s4u::Host* host)
23 {
24   host_ = host;
25   return *this;
26 }
27 RawImpl& RawImpl::set_timeout(double timeout)
28 {
29   timeout_ = timeout;
30   return *this;
31 }
32
33 RawImpl* RawImpl::start()
34 {
35   surf_action_ = host_->get_cpu()->sleep(timeout_);
36   surf_action_->set_activity(this);
37   return this;
38 }
39
40 void RawImpl::suspend()
41 {
42   /* The suspension of raw synchros is delayed to when the actor is rescheduled. */
43 }
44
45 void RawImpl::resume()
46 {
47   /* I cannot resume raw synchros directly. This is delayed to when the actor is rescheduled at
48    * the end of the synchro. */
49 }
50
51 void RawImpl::cancel()
52 {
53   /* I cannot cancel raw synchros directly. */
54 }
55
56 void RawImpl::post()
57 {
58   if (surf_action_->get_state() == resource::Action::State::FAILED) {
59     state_ = State::FAILED;
60   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
61     state_ = State::SRC_TIMEOUT;
62   }
63
64   clean_action();
65   /* Answer all simcalls associated with the synchro */
66   finish();
67 }
68 void RawImpl::set_exception(actor::ActorImpl* issuer)
69 {
70   if (state_ == State::FAILED) {
71     issuer->context_->set_wannadie();
72     issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
73   } else {
74     xbt_assert(state_ == State::SRC_TIMEOUT, "Internal error in RawImpl::finish() unexpected synchro state %s",
75                to_c_str(state_));
76   }
77 }
78
79 void RawImpl::finish()
80 {
81   XBT_DEBUG("RawImpl::finish() in state %s", to_c_str(state_));
82   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
83   smx_simcall_t simcall = simcalls_.front();
84   simcalls_.pop_front();
85
86   set_exception(simcall->issuer_);
87
88   finish_callback_();
89   simcall->issuer_->waiting_synchro_ = nullptr;
90   simcall->issuer_->simcall_answer();
91 }
92
93 } // namespace activity
94 } // namespace kernel
95 } // namespace simgrid