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/ActivityImpl.hpp"
7 #include "simgrid/modelchecker.h"
8 #include "src/mc/mc_replay.hpp"
9 #include "src/simix/smx_private.hpp"
10 #include <cmath> // isfinite()
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
18 ActivityImpl::~ActivityImpl()
21 XBT_DEBUG("Destroy activity %p", this);
24 void ActivityImpl::register_simcall(smx_simcall_t simcall)
26 simcalls_.push_back(simcall);
27 simcall->issuer_->waiting_synchro_ = this;
30 void ActivityImpl::clean_action()
33 surf_action_->unref();
34 surf_action_ = nullptr;
38 double ActivityImpl::get_remaining() const
40 return surf_action_ ? surf_action_->get_remains() : 0;
43 const char* ActivityImpl::get_state_str() const
64 case State::SRC_HOST_FAILURE:
65 return "SRC_HOST_FAILURE";
67 case State::DST_HOST_FAILURE:
68 return "DST_HOST_FAILURE";
73 case State::SRC_TIMEOUT:
75 case State::DST_TIMEOUT:
78 case State::LINK_FAILURE:
79 return "LINK_FAILURE";
84 bool ActivityImpl::test()
86 if (state_ != State::WAITING && state_ != State::RUNNING) {
93 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
95 XBT_DEBUG("Wait for execution of synchro %p, state %d", this, (int)state_);
96 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
98 /* Associate this simcall to the synchro */
99 register_simcall(&issuer->simcall_);
101 if (MC_is_active() || MC_record_replay_is_active()) {
102 int idx = SIMCALL_GET_MC_VALUE(issuer->simcall_);
104 state_ = simgrid::kernel::activity::State::DONE;
106 /* If we reached this point, the wait simcall must have a timeout */
107 /* Otherwise it shouldn't be enabled and executed by the MC */
110 state_ = simgrid::kernel::activity::State::TIMEOUT;
116 /* If the synchro is already finished then perform the error handling */
117 if (state_ != simgrid::kernel::activity::State::RUNNING)
120 /* we need a sleep action (even when there is no timeout) to be notified of host failures */
121 set_timeout(timeout);
125 void ActivityImpl::suspend()
127 if (surf_action_ == nullptr)
129 XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
130 surf_action_->suspend();
134 void ActivityImpl::resume()
136 if (surf_action_ == nullptr)
138 XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
139 surf_action_->resume();
143 void ActivityImpl::cancel()
145 XBT_VERB("Activity %p is canceled", this);
146 if (surf_action_ != nullptr)
147 surf_action_->cancel();
148 state_ = State::CANCELED;
151 // boost::intrusive_ptr<Activity> support:
152 void intrusive_ptr_add_ref(simgrid::kernel::activity::ActivityImpl* activity)
154 activity->refcount_.fetch_add(1, std::memory_order_relaxed);
157 void intrusive_ptr_release(simgrid::kernel::activity::ActivityImpl* activity)
159 if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
160 std::atomic_thread_fence(std::memory_order_acquire);
164 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
165 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
168 } // namespace simgrid::kernel::activity::