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 <boost/range/algorithm.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::unregister_simcall(smx_simcall_t simcall)
33 // Remove the first occurrence of simcall:
34 auto j = boost::range::find(simcalls_, simcall);
35 if (j != simcalls_.end())
39 void ActivityImpl::clean_action()
42 surf_action_->unref();
43 surf_action_ = nullptr;
47 double ActivityImpl::get_remaining() const
49 return surf_action_ ? surf_action_->get_remains() : 0;
52 const char* ActivityImpl::get_state_str() const
54 return to_c_str(state_);
57 bool ActivityImpl::test()
59 if (state_ != State::WAITING && state_ != State::RUNNING) {
66 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
68 XBT_DEBUG("Wait for execution of synchro %p, state %s", this, to_c_str(state_));
69 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
71 /* Associate this simcall to the synchro */
72 register_simcall(&issuer->simcall_);
74 xbt_assert(not MC_is_active() && not MC_record_replay_is_active(), "MC is currently not supported here.");
76 /* If the synchro is already finished then perform the error handling */
77 if (state_ != State::RUNNING)
79 else if (timeout == 0.) {
80 // still running and timeout == 0 ? We need to report a timeout
81 state_ = State::TIMEOUT;
84 /* we need a sleep action (even when the timeout is infinite) to be notified of host failures */
89 void ActivityImpl::suspend()
91 if (surf_action_ == nullptr)
93 XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
94 surf_action_->suspend();
98 void ActivityImpl::resume()
100 if (surf_action_ == nullptr)
102 XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
103 surf_action_->resume();
107 void ActivityImpl::cancel()
109 XBT_VERB("Activity %p is canceled", this);
110 if (surf_action_ != nullptr)
111 surf_action_->cancel();
112 state_ = State::CANCELED;
115 // boost::intrusive_ptr<Activity> support:
116 void intrusive_ptr_add_ref(ActivityImpl* activity)
118 activity->refcount_.fetch_add(1, std::memory_order_relaxed);
121 void intrusive_ptr_release(ActivityImpl* activity)
123 if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
124 std::atomic_thread_fence(std::memory_order_acquire);
128 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
129 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
132 } // namespace simgrid::kernel::activity::