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"
11 #include <cmath> // isfinite()
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
19 ActivityImpl::~ActivityImpl()
22 XBT_DEBUG("Destroy activity %p", this);
25 void ActivityImpl::register_simcall(smx_simcall_t simcall)
27 simcalls_.push_back(simcall);
28 simcall->issuer_->waiting_synchro_ = this;
31 void ActivityImpl::clean_action()
34 surf_action_->unref();
35 surf_action_ = nullptr;
39 double ActivityImpl::get_remaining() const
41 return surf_action_ ? surf_action_->get_remains() : 0;
44 const char* ActivityImpl::get_state_str() const
46 constexpr std::array<const char*, 12> names{{"WAITING", "READY", "RUNNING", "DONE", "CANCELED", "FAILED",
47 "SRC_HOST_FAILURE", "DST_HOST_FAILURE", "TIMEOUT", "SRC_TIMEOUT",
48 "DST_TIMEOUT", "LINK_FAILURE"}};
49 return names[static_cast<int>(state_)];
52 bool ActivityImpl::test()
54 if (state_ != State::WAITING && state_ != State::RUNNING) {
61 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
63 XBT_DEBUG("Wait for execution of synchro %p, state %d", this, (int)state_);
64 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
66 /* Associate this simcall to the synchro */
67 register_simcall(&issuer->simcall_);
69 if (MC_is_active() || MC_record_replay_is_active()) {
70 int idx = SIMCALL_GET_MC_VALUE(issuer->simcall_);
72 state_ = simgrid::kernel::activity::State::DONE;
74 /* If we reached this point, the wait simcall must have a timeout */
75 /* Otherwise it shouldn't be enabled and executed by the MC */
78 state_ = simgrid::kernel::activity::State::TIMEOUT;
84 /* If the synchro is already finished then perform the error handling */
85 if (state_ != simgrid::kernel::activity::State::RUNNING)
88 /* we need a sleep action (even when there is no timeout) to be notified of host failures */
93 void ActivityImpl::suspend()
95 if (surf_action_ == nullptr)
97 XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
98 surf_action_->suspend();
102 void ActivityImpl::resume()
104 if (surf_action_ == nullptr)
106 XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
107 surf_action_->resume();
111 void ActivityImpl::cancel()
113 XBT_VERB("Activity %p is canceled", this);
114 if (surf_action_ != nullptr)
115 surf_action_->cancel();
116 state_ = State::CANCELED;
119 // boost::intrusive_ptr<Activity> support:
120 void intrusive_ptr_add_ref(simgrid::kernel::activity::ActivityImpl* activity)
122 activity->refcount_.fetch_add(1, std::memory_order_relaxed);
125 void intrusive_ptr_release(simgrid::kernel::activity::ActivityImpl* activity)
127 if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
128 std::atomic_thread_fence(std::memory_order_acquire);
132 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
133 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
136 } // namespace simgrid::kernel::activity::