Logo AND Algorithmique Numérique Distribuée

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