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 its from successors");
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.");
92 /*! Signal fired each time that the activity fails to start because of a veto (e.g., unsolved dependency or no
93 * resource assigned) */
94 static xbt::signal<void(Activity&)> on_veto;
98 state_ = State::STARTING;
99 if (dependencies_solved() && is_assigned()) {
100 XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
108 Activity(Activity const&) = delete;
109 Activity& operator=(Activity const&) = delete;
112 /** Starts a previously created activity.
114 * This function is optional: you can call wait() even if you didn't call start()
116 virtual Activity* start() = 0;
117 /** Blocks the current actor until the activity is terminated */
118 Activity* wait() { return wait_for(-1.0); }
119 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
120 * Raises: timeout exception.*/
121 Activity* wait_for(double timeout);
122 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
123 * Raises: timeout exception. */
124 void wait_until(double time_limit);
126 /** Cancel that activity */
128 /** Retrieve the current state of the activity */
129 Activity::State get_state() const { return state_; }
130 /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
131 const char* get_state_str() const;
132 void set_state(Activity::State state) { state_ = state; }
133 /** Tests whether the given activity is terminated yet. */
136 /** Blocks the progression of this activity until it gets resumed */
137 virtual Activity* suspend();
138 /** Unblock the progression of this activity if it was suspended previously */
139 virtual Activity* resume();
140 /** Whether or not the progression of this activity is blocked */
141 bool is_suspended() const { return suspended_; }
143 virtual const char* get_cname() const = 0;
144 virtual const std::string& get_name() const = 0;
146 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
147 virtual double get_remaining() const;
148 /** Set the [remaining] amount of work that this Activity will entail
150 * It is forbidden to change the amount of work once the Activity is started */
151 Activity* set_remaining(double remains);
153 /** Returns the internal implementation of this Activity */
154 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
157 friend void intrusive_ptr_release(Activity* a)
159 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
160 std::atomic_thread_fence(std::memory_order_acquire);
164 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
168 intrusive_ptr_add_ref(this);
171 void unref() { intrusive_ptr_release(this); }
174 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
175 Activity::State state_ = Activity::State::INITED;
177 bool suspended_ = false;
178 std::vector<ActivityPtr> successors_;
179 std::set<ActivityPtr> dependencies_;
180 std::atomic_int_fast32_t refcount_{0};
183 template <class AnyActivity> class Activity_T : public Activity {
184 std::string name_ = "unnamed";
185 std::string tracing_category_ = "";
186 void* user_data_ = nullptr;
189 AnyActivity* add_successor(ActivityPtr a)
191 Activity::add_successor(a);
192 return static_cast<AnyActivity*>(this);
194 AnyActivity* remove_successor(ActivityPtr a)
196 Activity::remove_successor(a);
197 return static_cast<AnyActivity*>(this);
199 AnyActivity* set_name(const std::string& name)
201 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
203 return static_cast<AnyActivity*>(this);
205 const std::string& get_name() const override { return name_; }
206 const char* get_cname() const override { return name_.c_str(); }
208 AnyActivity* set_tracing_category(const std::string& category)
210 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
211 tracing_category_ = category;
212 return static_cast<AnyActivity*>(this);
214 const std::string& get_tracing_category() const { return tracing_category_; }
216 AnyActivity* set_user_data(void* data)
219 return static_cast<AnyActivity*>(this);
222 void* get_user_data() const { return user_data_; }
224 AnyActivity* vetoable_start()
226 Activity::vetoable_start();
227 return static_cast<AnyActivity*>(this);
230 AnyActivity* cancel() { return static_cast<AnyActivity*>(Activity::cancel()); }
231 AnyActivity* wait() { return wait_for(-1.0); }
232 virtual AnyActivity* wait_for(double timeout) { return static_cast<AnyActivity*>(Activity::wait_for(timeout)); }
235 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
236 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
237 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
239 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
240 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
245 } // namespace simgrid
247 #endif /* SIMGRID_S4U_ACTIVITY_HPP */