1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/s4u/Host.hpp>
9 #include "src/kernel/activity/Synchro.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/resource/CpuImpl.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_synchro, kernel,
14 "Kernel synchronization activity (lock/acquire on a mutex, semaphore or condition)");
16 namespace simgrid::kernel::activity {
18 SynchroImpl& SynchroImpl::set_host(s4u::Host* host)
23 SynchroImpl& SynchroImpl::set_timeout(double timeout)
29 SynchroImpl* SynchroImpl::start()
31 surf_action_ = host_->get_cpu()->sleep(timeout_);
32 surf_action_->set_activity(this);
36 void SynchroImpl::suspend()
38 /* The suspension of raw synchros is delayed to when the actor is rescheduled. */
41 void SynchroImpl::resume()
43 /* I cannot resume raw synchros directly. This is delayed to when the actor is rescheduled at
44 * the end of the synchro. */
47 void SynchroImpl::cancel()
49 /* I cannot cancel raw synchros directly. */
52 void SynchroImpl::post()
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);
60 /* Answer all simcalls associated with the synchro */
63 void SynchroImpl::set_exception(actor::ActorImpl* issuer)
65 if (get_state() == State::FAILED) {
66 issuer->set_wannadie();
67 issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
69 xbt_assert(get_state() == State::SRC_TIMEOUT, "Internal error in SynchroImpl::finish() unexpected synchro state %s",
74 void SynchroImpl::finish()
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();
81 set_exception(simcall->issuer_);
84 simcall->issuer_->waiting_synchro_ = nullptr;
85 simcall->issuer_->simcall_answer();
88 } // namespace simgrid::kernel::activity