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
12 #include <simgrid/forward.h>
16 #include <xbt/Extendable.hpp>
17 #include <xbt/asserts.h>
18 #include <xbt/signal.hpp>
19 #include <xbt/utility.hpp>
21 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
25 extern template class XBT_PUBLIC xbt::Extendable<s4u::Activity>;
31 * This class is the ancestor of every activities that an actor can undertake.
32 * That is, activities are all the things that do take time to the actor in the simulated world.
34 class XBT_PUBLIC Activity : public xbt::Extendable<Activity> {
39 friend std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
40 friend std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename);
44 // enum class State { ... }
45 XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, FAILED, CANCELED, FINISHED);
47 virtual bool is_assigned() const = 0;
48 virtual bool dependencies_solved() const { return dependencies_.empty(); }
49 virtual unsigned long is_waited_by() const { return successors_.size(); }
50 const std::set<ActivityPtr>& get_dependencies() const { return dependencies_; }
51 const std::vector<ActivityPtr>& get_successors() const { return successors_; }
55 virtual ~Activity() = default;
58 void release_dependencies()
60 while (not successors_.empty()) {
61 ActivityPtr b = successors_.back();
62 XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
63 b->dependencies_.erase(this);
64 if (b->dependencies_solved()) {
67 successors_.pop_back();
71 void add_successor(ActivityPtr a)
74 throw std::invalid_argument("Cannot be its own successor");
75 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
76 if (p != successors_.end())
77 throw std::invalid_argument("Dependency already exists");
79 successors_.push_back(a);
80 a->dependencies_.insert({this});
83 void remove_successor(ActivityPtr a)
86 throw std::invalid_argument("Cannot ask to remove itself from successors list");
88 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
89 if (p != successors_.end()){
91 a->dependencies_.erase({this});
93 throw std::invalid_argument("Dependency does not exist. Can not be removed.");
96 static std::set<Activity*>* vetoed_activities_;
99 /*! Signal fired each time that the activity fails to start because of a veto (e.g., unsolved dependency or no
100 * resource assigned) */
101 static xbt::signal<void(Activity&)> on_veto;
102 /*! Signal fired when theactivity completes (either normally, cancelled or failed) */
103 static xbt::signal<void(Activity&)> on_completion;
105 void vetoable_start()
107 state_ = State::STARTING;
108 if (dependencies_solved() && is_assigned()) {
109 XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
112 if (vetoed_activities_ != nullptr)
113 vetoed_activities_->insert(this);
118 void complete(Activity::State state)
121 on_completion(*this);
122 if (state == State::FINISHED)
123 release_dependencies();
126 static std::set<Activity*>* get_vetoed_activities() { return vetoed_activities_; }
127 static void set_vetoed_activities(std::set<Activity*>* whereto) { vetoed_activities_ = whereto; }
130 Activity(Activity const&) = delete;
131 Activity& operator=(Activity const&) = delete;
134 /** Starts a previously created activity.
136 * This function is optional: you can call wait() even if you didn't call start()
138 virtual Activity* start() = 0;
139 /** Blocks the current actor until the activity is terminated */
140 Activity* wait() { return wait_for(-1.0); }
141 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
142 * Raises: timeout exception.*/
143 Activity* wait_for(double timeout);
144 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
145 * Raises: timeout exception. */
146 void wait_until(double time_limit);
148 /** Cancel that activity */
150 /** Retrieve the current state of the activity */
151 Activity::State get_state() const { return state_; }
152 /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
153 const char* get_state_str() const;
154 void set_state(Activity::State state) { state_ = state; }
155 /** Tests whether the given activity is terminated yet. */
158 /** Blocks the progression of this activity until it gets resumed */
159 virtual Activity* suspend();
160 /** Unblock the progression of this activity if it was suspended previously */
161 virtual Activity* resume();
162 /** Whether or not the progression of this activity is blocked */
163 bool is_suspended() const { return suspended_; }
165 virtual const char* get_cname() const = 0;
166 virtual const std::string& get_name() const = 0;
168 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
169 virtual double get_remaining() const;
170 /** Set the [remaining] amount of work that this Activity will entail
172 * It is forbidden to change the amount of work once the Activity is started */
173 Activity* set_remaining(double remains);
175 double get_start_time() const;
176 double get_finish_time() const;
177 void mark() { marked_ = true; }
178 bool is_marked() const { return marked_; }
180 /** Returns the internal implementation of this Activity */
181 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
184 friend void intrusive_ptr_release(Activity* a)
186 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
187 std::atomic_thread_fence(std::memory_order_acquire);
191 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
195 intrusive_ptr_add_ref(this);
198 void unref() { intrusive_ptr_release(this); }
201 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
202 Activity::State state_ = Activity::State::INITED;
204 bool suspended_ = false;
205 bool marked_ = false;
206 std::vector<ActivityPtr> successors_;
207 std::set<ActivityPtr> dependencies_;
208 std::atomic_int_fast32_t refcount_{0};
211 template <class AnyActivity> class Activity_T : public Activity {
212 std::string name_ = "unnamed";
213 std::string tracing_category_ = "";
214 void* user_data_ = nullptr;
217 AnyActivity* add_successor(ActivityPtr a)
219 Activity::add_successor(a);
220 return static_cast<AnyActivity*>(this);
222 AnyActivity* remove_successor(ActivityPtr a)
224 Activity::remove_successor(a);
225 return static_cast<AnyActivity*>(this);
227 AnyActivity* set_name(const std::string& name)
229 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
231 return static_cast<AnyActivity*>(this);
233 const std::string& get_name() const override { return name_; }
234 const char* get_cname() const override { return name_.c_str(); }
236 AnyActivity* set_tracing_category(const std::string& category)
238 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
239 tracing_category_ = category;
240 return static_cast<AnyActivity*>(this);
242 const std::string& get_tracing_category() const { return tracing_category_; }
244 AnyActivity* set_user_data(void* data)
247 return static_cast<AnyActivity*>(this);
250 void* get_user_data() const { return user_data_; }
252 AnyActivity* vetoable_start()
254 Activity::vetoable_start();
255 return static_cast<AnyActivity*>(this);
258 AnyActivity* cancel() { return static_cast<AnyActivity*>(Activity::cancel()); }
259 AnyActivity* wait() { return wait_for(-1.0); }
260 virtual AnyActivity* wait_for(double timeout) { return static_cast<AnyActivity*>(Activity::wait_for(timeout)); }
263 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
264 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
265 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
267 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
268 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
273 } // namespace simgrid
275 #endif /* SIMGRID_S4U_ACTIVITY_HPP */