Logo AND Algorithmique Numérique Distribuée

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