Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Completely remove detached comms when they are finished.
[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::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   surf_action_ = host_->get_cpu()->sleep(timeout_);
32   surf_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::post()
53 {
54   if (surf_action_->get_state() == resource::Action::State::FAILED)
55     set_state(State::FAILED);
56   else if (surf_action_->get_state() == resource::Action::State::FINISHED)
57     set_state(State::SRC_TIMEOUT);
58
59   clean_action();
60   /* Answer all simcalls associated with the synchro */
61   finish();
62 }
63 void SynchroImpl::set_exception(actor::ActorImpl* issuer)
64 {
65   if (get_state() == State::FAILED) {
66     issuer->set_wannadie();
67     issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
68   } else {
69     xbt_assert(get_state() == State::SRC_TIMEOUT, "Internal error in SynchroImpl::finish() unexpected synchro state %s",
70                get_state_str());
71   }
72 }
73
74 void SynchroImpl::finish()
75 {
76   XBT_DEBUG("SynchroImpl::finish() in state %s", get_state_str());
77   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
78   actor::Simcall* simcall = simcalls_.front();
79   simcalls_.pop_front();
80
81   set_exception(simcall->issuer_);
82
83   finish_callback_();
84   simcall->issuer_->waiting_synchro_ = nullptr;
85   simcall->issuer_->simcall_answer();
86 }
87
88 } // namespace simgrid::kernel::activity