1 /* Copyright (c) 2007-2022. 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/context/Context.hpp"
12 #include "src/kernel/resource/CpuImpl.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_synchro, kernel,
15 "Kernel synchronization activity (lock/acquire on a mutex, semaphore or condition)");
21 SynchroImpl& SynchroImpl::set_host(s4u::Host* host)
26 SynchroImpl& SynchroImpl::set_timeout(double timeout)
32 SynchroImpl* SynchroImpl::start()
34 surf_action_ = host_->get_cpu()->sleep(timeout_);
35 surf_action_->set_activity(this);
39 void SynchroImpl::suspend()
41 /* The suspension of raw synchros is delayed to when the actor is rescheduled. */
44 void SynchroImpl::resume()
46 /* I cannot resume raw synchros directly. This is delayed to when the actor is rescheduled at
47 * the end of the synchro. */
50 void SynchroImpl::cancel()
52 /* I cannot cancel raw synchros directly. */
55 void SynchroImpl::post()
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);
63 /* Answer all simcalls associated with the synchro */
66 void SynchroImpl::set_exception(actor::ActorImpl* issuer)
68 if (get_state() == State::FAILED) {
69 issuer->context_->set_wannadie();
70 issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
72 xbt_assert(get_state() == State::SRC_TIMEOUT, "Internal error in SynchroImpl::finish() unexpected synchro state %s",
77 void SynchroImpl::finish()
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();
84 set_exception(simcall->issuer_);
87 simcall->issuer_->waiting_synchro_ = nullptr;
88 simcall->issuer_->simcall_answer();
91 } // namespace activity
93 } // namespace simgrid