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 {
35 friend std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
39 // enum class State { ... }
40 XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, FAILED, CANCELED, FINISHED);
42 virtual bool is_assigned() const = 0;
43 virtual bool dependencies_solved() const { return dependencies_.empty(); }
44 virtual unsigned long is_waited_by() const { return successors_.size(); }
48 virtual ~Activity() = default;
50 void release_dependencies()
52 while (not successors_.empty()) {
53 ActivityPtr b = successors_.back();
54 XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
55 b->dependencies_.erase(this);
56 if (b->dependencies_solved()) {
59 successors_.pop_back();
63 void add_successor(ActivityPtr a)
66 throw std::invalid_argument("Cannot be its own successor");
67 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
68 if (p != successors_.end())
69 throw std::invalid_argument("Dependency already exists");
71 successors_.push_back(a);
72 a->dependencies_.insert({this});
75 void remove_successor(ActivityPtr a)
78 throw std::invalid_argument("Cannot ask to remove itself from successors list");
80 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
81 if (p != successors_.end()){
83 a->dependencies_.erase({this});
85 throw std::invalid_argument("Dependency does not exist. Can not be removed.");
88 static std::set<Activity*>* vetoed_activities_;
91 /*! Signal fired each time that the activity fails to start because of a veto (e.g., unsolved dependency or no
92 * resource assigned) */
93 static xbt::signal<void(Activity&)> on_veto;
94 /*! Signal fired when theactivity completes (either normally, cancelled or failed) */
95 static xbt::signal<void(Activity&)> on_completion;
99 state_ = State::STARTING;
100 if (dependencies_solved() && is_assigned()) {
101 XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
104 if (vetoed_activities_ != nullptr)
105 vetoed_activities_->insert(this);
110 void complete(Activity::State state)
113 if (state == State::FINISHED)
114 release_dependencies();
115 on_completion(*this);
118 static std::set<Activity*>* get_vetoed_activities() { return vetoed_activities_; }
119 static void set_vetoed_activities(std::set<Activity*>* whereto) { vetoed_activities_ = whereto; }
122 Activity(Activity const&) = delete;
123 Activity& operator=(Activity const&) = delete;
126 /** Starts a previously created activity.
128 * This function is optional: you can call wait() even if you didn't call start()
130 virtual Activity* start() = 0;
131 /** Blocks the current actor until the activity is terminated */
132 Activity* wait() { return wait_for(-1.0); }
133 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
134 * Raises: timeout exception.*/
135 Activity* wait_for(double timeout);
136 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
137 * Raises: timeout exception. */
138 void wait_until(double time_limit);
140 /** Cancel that activity */
142 /** Retrieve the current state of the activity */
143 Activity::State get_state() const { return state_; }
144 /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
145 const char* get_state_str() const;
146 void set_state(Activity::State state) { state_ = state; }
147 /** Tests whether the given activity is terminated yet. */
150 /** Blocks the progression of this activity until it gets resumed */
151 virtual Activity* suspend();
152 /** Unblock the progression of this activity if it was suspended previously */
153 virtual Activity* resume();
154 /** Whether or not the progression of this activity is blocked */
155 bool is_suspended() const { return suspended_; }
157 virtual const char* get_cname() const = 0;
158 virtual const std::string& get_name() const = 0;
160 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
161 virtual double get_remaining() const;
162 /** Set the [remaining] amount of work that this Activity will entail
164 * It is forbidden to change the amount of work once the Activity is started */
165 Activity* set_remaining(double remains);
167 /** Returns the internal implementation of this Activity */
168 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
171 friend void intrusive_ptr_release(Activity* a)
173 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
174 std::atomic_thread_fence(std::memory_order_acquire);
178 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
182 intrusive_ptr_add_ref(this);
185 void unref() { intrusive_ptr_release(this); }
188 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
189 Activity::State state_ = Activity::State::INITED;
191 bool suspended_ = false;
192 std::vector<ActivityPtr> successors_;
193 std::set<ActivityPtr> dependencies_;
194 std::atomic_int_fast32_t refcount_{0};
197 template <class AnyActivity> class Activity_T : public Activity {
198 std::string name_ = "unnamed";
199 std::string tracing_category_ = "";
200 void* user_data_ = nullptr;
203 AnyActivity* add_successor(ActivityPtr a)
205 Activity::add_successor(a);
206 return static_cast<AnyActivity*>(this);
208 AnyActivity* remove_successor(ActivityPtr a)
210 Activity::remove_successor(a);
211 return static_cast<AnyActivity*>(this);
213 AnyActivity* set_name(const std::string& name)
215 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
217 return static_cast<AnyActivity*>(this);
219 const std::string& get_name() const override { return name_; }
220 const char* get_cname() const override { return name_.c_str(); }
222 AnyActivity* set_tracing_category(const std::string& category)
224 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
225 tracing_category_ = category;
226 return static_cast<AnyActivity*>(this);
228 const std::string& get_tracing_category() const { return tracing_category_; }
230 AnyActivity* set_user_data(void* data)
233 return static_cast<AnyActivity*>(this);
236 void* get_user_data() const { return user_data_; }
238 AnyActivity* vetoable_start()
240 Activity::vetoable_start();
241 return static_cast<AnyActivity*>(this);
244 AnyActivity* cancel() { return static_cast<AnyActivity*>(Activity::cancel()); }
245 AnyActivity* wait() { return wait_for(-1.0); }
246 virtual AnyActivity* wait_for(double timeout) { return static_cast<AnyActivity*>(Activity::wait_for(timeout)); }
249 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
250 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
251 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
253 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
254 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
259 } // namespace simgrid
261 #endif /* SIMGRID_S4U_ACTIVITY_HPP */