Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / kernel / activity / Synchro.cpp
1 /* Copyright (c) 2007-2023. 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::kernel::activity {
17
18 SynchroImpl& SynchroImpl::set_host(s4u::Host* host)
19 {
20   host_ = host;
21   return *this;
22 }
23 SynchroImpl& SynchroImpl::set_timeout(double timeout)
24 {
25   timeout_ = timeout;
26   return *this;
27 }
28
29 SynchroImpl* SynchroImpl::start()
30 {
31   model_action_ = host_->get_cpu()->sleep(timeout_);
32   model_action_->set_activity(this);
33   return this;
34 }
35
36 void SynchroImpl::suspend()
37 {
38   /* The suspension of raw synchros is delayed to when the actor is rescheduled. */
39 }
40
41 void SynchroImpl::resume()
42 {
43   /* I cannot resume raw synchros directly. This is delayed to when the actor is rescheduled at
44    * the end of the synchro. */
45 }
46
47 void SynchroImpl::cancel()
48 {
49   /* I cannot cancel raw synchros directly. */
50 }
51
52 void SynchroImpl::set_exception(actor::ActorImpl* issuer)
53 {
54   if (get_state() == State::FAILED) {
55     issuer->set_wannadie();
56     issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
57   } else {
58     xbt_assert(get_state() == State::SRC_TIMEOUT, "Internal error in SynchroImpl::finish() unexpected synchro state %s",
59                get_state_str());
60   }
61 }
62
63 void SynchroImpl::finish()
64 {
65   XBT_DEBUG("SynchroImpl::finish() in state %s", get_state_str());
66   if (model_action_->get_state() == resource::Action::State::FAILED)
67     set_state(State::FAILED);
68   else if (model_action_->get_state() == resource::Action::State::FINISHED)
69     set_state(State::SRC_TIMEOUT);
70
71   clean_action();
72
73   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
74   actor::Simcall* simcall = simcalls_.front();
75   simcalls_.pop_front();
76
77   set_exception(simcall->issuer_);
78
79   finish_callback_();
80   simcall->issuer_->waiting_synchro_ = nullptr;
81   simcall->issuer_->simcall_answer();
82 }
83
84 } // namespace simgrid::kernel::activity