1 /* Copyright (c) 2006-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 #ifndef SIMGRID_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
9 #include <xbt/asserts.h>
13 #include <simgrid/forward.h>
17 #include <xbt/signal.hpp>
18 #include <xbt/utility.hpp>
20 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
27 * This class is the ancestor of every activities that an actor can undertake.
28 * That is, activities are all the things that do take time to the actor in the simulated world.
30 class XBT_PUBLIC Activity {
36 // enum class State { ... }
37 XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, FAILED, CANCELED, FINISHED);
39 virtual bool is_assigned() const = 0;
40 virtual bool dependencies_solved() const { return dependencies_.empty(); }
44 virtual ~Activity() = default;
46 virtual void complete(Activity::State state)
49 if (state == State::FINISHED)
50 release_dependencies();
53 void release_dependencies()
55 while (not successors_.empty()) {
56 ActivityPtr b = successors_.back();
57 XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
58 b->dependencies_.erase(this);
59 if (b->dependencies_solved()) {
62 successors_.pop_back();
66 void add_successor(ActivityPtr a)
69 throw std::invalid_argument("Cannot be its own successor");
70 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
71 if (p != successors_.end())
72 throw std::invalid_argument("Dependency already exists");
74 successors_.push_back(a);
75 a->dependencies_.insert({this});
78 void remove_successor(ActivityPtr a)
81 throw std::invalid_argument("Cannot ask to remove itself from successors list");
83 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
84 if (p != successors_.end()){
86 a->dependencies_.erase({this});
88 throw std::invalid_argument("Dependency does not exist. Can not be removed.");
91 static std::set<Activity*>* vetoed_activities_;
94 /*! Signal fired each time that the activity fails to start because of a veto (e.g., unsolved dependency or no
95 * resource assigned) */
96 static xbt::signal<void(Activity&)> on_veto;
100 state_ = State::STARTING;
101 if (dependencies_solved() && is_assigned()) {
102 XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
105 if (vetoed_activities_ != nullptr)
106 vetoed_activities_->insert(this);
111 static std::set<Activity*>* get_vetoed_activities() { return vetoed_activities_; }
112 static void set_vetoed_activities(std::set<Activity*>* whereto) { vetoed_activities_ = whereto; }
115 Activity(Activity const&) = delete;
116 Activity& operator=(Activity const&) = delete;
119 /** Starts a previously created activity.
121 * This function is optional: you can call wait() even if you didn't call start()
123 virtual Activity* start() = 0;
124 /** Blocks the current actor until the activity is terminated */
125 Activity* wait() { return wait_for(-1.0); }
126 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
127 * Raises: timeout exception.*/
128 Activity* wait_for(double timeout);
129 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
130 * Raises: timeout exception. */
131 void wait_until(double time_limit);
133 /** Cancel that activity */
135 /** Retrieve the current state of the activity */
136 Activity::State get_state() const { return state_; }
137 /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
138 const char* get_state_str() const;
139 void set_state(Activity::State state) { state_ = state; }
140 /** Tests whether the given activity is terminated yet. */
143 /** Blocks the progression of this activity until it gets resumed */
144 virtual Activity* suspend();
145 /** Unblock the progression of this activity if it was suspended previously */
146 virtual Activity* resume();
147 /** Whether or not the progression of this activity is blocked */
148 bool is_suspended() const { return suspended_; }
150 virtual const char* get_cname() const = 0;
151 virtual const std::string& get_name() const = 0;
153 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
154 virtual double get_remaining() const;
155 /** Set the [remaining] amount of work that this Activity will entail
157 * It is forbidden to change the amount of work once the Activity is started */
158 Activity* set_remaining(double remains);
160 /** Returns the internal implementation of this Activity */
161 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
164 friend void intrusive_ptr_release(Activity* a)
166 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
167 std::atomic_thread_fence(std::memory_order_acquire);
171 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
175 intrusive_ptr_add_ref(this);
178 void unref() { intrusive_ptr_release(this); }
181 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
182 Activity::State state_ = Activity::State::INITED;
184 bool suspended_ = false;
185 std::vector<ActivityPtr> successors_;
186 std::set<ActivityPtr> dependencies_;
187 std::atomic_int_fast32_t refcount_{0};
190 template <class AnyActivity> class Activity_T : public Activity {
191 std::string name_ = "unnamed";
192 std::string tracing_category_ = "";
193 void* user_data_ = nullptr;
196 AnyActivity* add_successor(ActivityPtr a)
198 Activity::add_successor(a);
199 return static_cast<AnyActivity*>(this);
201 AnyActivity* remove_successor(ActivityPtr a)
203 Activity::remove_successor(a);
204 return static_cast<AnyActivity*>(this);
206 AnyActivity* set_name(const std::string& name)
208 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
210 return static_cast<AnyActivity*>(this);
212 const std::string& get_name() const override { return name_; }
213 const char* get_cname() const override { return name_.c_str(); }
215 AnyActivity* set_tracing_category(const std::string& category)
217 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
218 tracing_category_ = category;
219 return static_cast<AnyActivity*>(this);
221 const std::string& get_tracing_category() const { return tracing_category_; }
223 AnyActivity* set_user_data(void* data)
226 return static_cast<AnyActivity*>(this);
229 void* get_user_data() const { return user_data_; }
231 AnyActivity* vetoable_start()
233 Activity::vetoable_start();
234 return static_cast<AnyActivity*>(this);
237 AnyActivity* cancel() { return static_cast<AnyActivity*>(Activity::cancel()); }
238 AnyActivity* wait() { return wait_for(-1.0); }
239 virtual AnyActivity* wait_for(double timeout) { return static_cast<AnyActivity*>(Activity::wait_for(timeout)); }
242 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
243 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
244 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
246 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
247 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
252 } // namespace simgrid
254 #endif /* SIMGRID_S4U_ACTIVITY_HPP */