1 /* Copyright (c) 2007-2021. 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 "src/kernel/activity/SynchroRaw.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "src/kernel/activity/ConditionVariableImpl.hpp"
10 #include "src/kernel/activity/MutexImpl.hpp"
11 #include "src/kernel/activity/SemaphoreImpl.hpp"
12 #include "src/kernel/context/Context.hpp"
13 #include "src/surf/cpu_interface.hpp"
14 #include "src/surf/surf_interface.hpp"
15 #include <simgrid/s4u/Host.hpp>
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
23 RawImpl& RawImpl::set_host(s4u::Host* host)
28 RawImpl& RawImpl::set_timeout(double timeout)
34 RawImpl* RawImpl::start()
36 surf_action_ = host_->pimpl_cpu->sleep(timeout_);
37 surf_action_->set_activity(this);
41 void RawImpl::suspend()
43 /* The suspension of raw synchros is delayed to when the process is rescheduled. */
46 void RawImpl::resume()
48 /* I cannot resume raw synchros directly. This is delayed to when the process is rescheduled at
49 * the end of the synchro. */
52 void RawImpl::cancel()
54 /* I cannot cancel raw synchros directly. */
59 if (surf_action_->get_state() == resource::Action::State::FAILED) {
60 state_ = State::FAILED;
61 } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
62 state_ = State::SRC_TIMEOUT;
66 /* Answer all simcalls associated with the synchro */
70 void RawImpl::finish()
72 smx_simcall_t simcall = simcalls_.front();
73 simcalls_.pop_front();
75 if (state_ == State::FAILED) {
76 XBT_DEBUG("RawImpl::finish(): host '%s' failed", simcall->issuer_->get_host()->get_cname());
77 simcall->issuer_->context_->set_wannadie();
78 simcall->issuer_->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
79 } else if (state_ != State::SRC_TIMEOUT) {
80 xbt_die("Internal error in RawImpl::finish() unexpected synchro state %d", static_cast<int>(state_));
83 switch (simcall->call_) {
84 case simix::Simcall::MUTEX_LOCK:
85 simcall_mutex_lock__get__mutex(simcall)->remove_sleeping_actor(*simcall->issuer_);
88 case simix::Simcall::COND_WAIT:
89 simcall_cond_wait_timeout__get__cond(simcall)->remove_sleeping_actor(*simcall->issuer_);
92 case simix::Simcall::COND_WAIT_TIMEOUT:
93 simcall_cond_wait_timeout__get__cond(simcall)->remove_sleeping_actor(*simcall->issuer_);
94 simcall_cond_wait_timeout__set__result(simcall, 1); // signal a timeout
97 case simix::Simcall::SEM_ACQUIRE:
98 simcall_sem_acquire_timeout__get__sem(simcall)->remove_sleeping_actor(*simcall->issuer_);
101 case simix::Simcall::SEM_ACQUIRE_TIMEOUT:
102 simcall_sem_acquire_timeout__get__sem(simcall)->remove_sleeping_actor(*simcall->issuer_);
103 simcall_sem_acquire_timeout__set__result(simcall, 1); // signal a timeout
109 simcall->issuer_->waiting_synchro_ = nullptr;
110 simcall->issuer_->simcall_answer();
113 } // namespace activity
114 } // namespace kernel
115 } // namespace simgrid