Logo AND Algorithmique Numérique Distribuée

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