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 <simgrid/modelchecker.h>
8 #include "src/kernel/activity/ActivityImpl.hpp"
9 #include "src/kernel/activity/SynchroRaw.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/actor/SimcallObserver.hpp"
12 #include "src/mc/mc_replay.hpp"
14 #include <boost/range/algorithm.hpp>
15 #include <cmath> // isfinite()
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_actor);
23 ActivityImpl::~ActivityImpl()
26 XBT_DEBUG("Destroy activity %p", this);
29 void ActivityImpl::register_simcall(smx_simcall_t simcall)
31 simcalls_.push_back(simcall);
32 simcall->issuer_->waiting_synchro_ = this;
35 void ActivityImpl::unregister_simcall(smx_simcall_t simcall)
37 // Remove the first occurrence of simcall:
38 auto j = boost::range::find(simcalls_, simcall);
39 if (j != simcalls_.end())
43 void ActivityImpl::clean_action()
46 surf_action_->unref();
47 surf_action_ = nullptr;
51 double ActivityImpl::get_remaining() const
53 return surf_action_ ? surf_action_->get_remains() : 0;
56 const char* ActivityImpl::get_state_str() const
58 return to_c_str(state_);
61 bool ActivityImpl::test()
63 if (state_ != State::WAITING && state_ != State::RUNNING) {
70 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
72 XBT_DEBUG("Wait for execution of synchro %p, state %s", this, to_c_str(state_));
73 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
75 /* Associate this simcall to the synchro */
76 register_simcall(&issuer->simcall_);
78 xbt_assert(not MC_is_active() && not MC_record_replay_is_active(), "MC is currently not supported here.");
80 /* If the synchro is already finished then perform the error handling */
81 if (state_ != State::RUNNING) {
84 /* we need a sleep action (even when the timeout is infinite) to be notified of host failures */
85 RawImplPtr synchro(new RawImpl([this, issuer]() {
86 this->unregister_simcall(&issuer->simcall_);
87 issuer->waiting_synchro_ = nullptr;
88 auto* observer = dynamic_cast<kernel::actor::ActivityWaitSimcall*>(issuer->simcall_.observer_);
89 xbt_assert(observer != nullptr);
90 observer->set_result(true);
92 synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
93 synchro->register_simcall(&issuer->simcall_);
97 void ActivityImpl::suspend()
99 if (surf_action_ == nullptr)
101 XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
102 surf_action_->suspend();
106 void ActivityImpl::resume()
108 if (surf_action_ == nullptr)
110 XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
111 surf_action_->resume();
115 void ActivityImpl::cancel()
117 XBT_VERB("Activity %p is canceled", this);
118 if (surf_action_ != nullptr)
119 surf_action_->cancel();
120 state_ = State::CANCELED;
123 // boost::intrusive_ptr<Activity> support:
124 void intrusive_ptr_add_ref(ActivityImpl* activity)
126 activity->refcount_.fetch_add(1, std::memory_order_relaxed);
129 void intrusive_ptr_release(ActivityImpl* activity)
131 if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
132 std::atomic_thread_fence(std::memory_order_acquire);
136 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
137 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
140 } // namespace simgrid::kernel::activity::