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/resource/CpuImpl.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_synchro, kernel,
14 "Kernel synchronization activity (lock/acquire on a mutex, semaphore or condition)");
20 SynchroImpl& SynchroImpl::set_host(s4u::Host* host)
25 SynchroImpl& SynchroImpl::set_timeout(double timeout)
31 SynchroImpl* SynchroImpl::start()
33 surf_action_ = host_->get_cpu()->sleep(timeout_);
34 surf_action_->set_activity(this);
38 void SynchroImpl::suspend()
40 /* The suspension of raw synchros is delayed to when the actor is rescheduled. */
43 void SynchroImpl::resume()
45 /* I cannot resume raw synchros directly. This is delayed to when the actor is rescheduled at
46 * the end of the synchro. */
49 void SynchroImpl::cancel()
51 /* I cannot cancel raw synchros directly. */
54 void SynchroImpl::post()
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);
62 /* Answer all simcalls associated with the synchro */
65 void SynchroImpl::set_exception(actor::ActorImpl* issuer)
67 if (get_state() == State::FAILED) {
68 issuer->set_wannadie();
69 issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
71 xbt_assert(get_state() == State::SRC_TIMEOUT, "Internal error in SynchroImpl::finish() unexpected synchro state %s",
76 void SynchroImpl::finish()
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();
83 set_exception(simcall->issuer_);
86 simcall->issuer_->waiting_synchro_ = nullptr;
87 simcall->issuer_->simcall_answer();
90 } // namespace activity
92 } // namespace simgrid