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 {
37 virtual ~Activity() = default;
39 virtual bool is_assigned() const = 0;
41 void release_dependencies()
43 while (not successors_.empty()) {
44 ActivityPtr b = successors_.back();
45 XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
46 b->dependencies_.erase(this);
47 if (b->dependencies_.empty()) {
50 successors_.pop_back();
54 void add_successor(ActivityPtr a)
57 throw std::invalid_argument("Cannot be its own successor");
58 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
59 if (p != successors_.end())
60 throw std::invalid_argument("Dependency already exists");
62 successors_.push_back(a);
63 a->dependencies_.insert({this});
66 void remove_successor(ActivityPtr a)
69 throw std::invalid_argument("Cannot ask to remove its from successors");
71 auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
72 if (p != successors_.end()){
74 a->dependencies_.erase({this});
76 throw std::invalid_argument("Dependency does not exist. Can not be removed.");
82 state_ = State::STARTING;
83 if (dependencies_.empty() && is_assigned()) {
84 XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
90 Activity(Activity const&) = delete;
91 Activity& operator=(Activity const&) = delete;
94 // enum class State { ... }
95 XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, CANCELED, FINISHED);
97 /** Starts a previously created activity.
99 * This function is optional: you can call wait() even if you didn't call start()
101 virtual Activity* start() = 0;
102 /** Blocks the current actor until the activity is terminated */
103 virtual Activity* wait() = 0;
104 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
105 * Raises: timeout exception.*/
106 virtual Activity* wait_for(double timeout) = 0;
107 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
108 * Raises: timeout exception. */
109 void wait_until(double time_limit);
111 /** Cancel that activity */
112 virtual Activity* cancel() = 0;
113 /** Retrieve the current state of the activity */
114 Activity::State get_state() const { return state_; }
115 /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
116 const char* get_state_str() const;
117 void set_state(Activity::State state) { state_ = state; }
118 /** Tests whether the given activity is terminated yet. */
121 /** Blocks the progression of this activity until it gets resumed */
122 virtual Activity* suspend();
123 /** Unblock the progression of this activity if it was suspended previously */
124 virtual Activity* resume();
125 /** Whether or not the progression of this activity is blocked */
126 bool is_suspended() const { return suspended_; }
128 virtual const char* get_cname() const = 0;
129 virtual const std::string& get_name() const = 0;
131 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
132 virtual double get_remaining() const;
133 /** Set the [remaining] amount of work that this Activity will entail
135 * It is forbidden to change the amount of work once the Activity is started */
136 Activity* set_remaining(double remains);
138 /** Returns the internal implementation of this Activity */
139 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
142 friend void intrusive_ptr_release(Activity* a)
144 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
145 std::atomic_thread_fence(std::memory_order_acquire);
149 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
153 intrusive_ptr_add_ref(this);
156 void unref() { intrusive_ptr_release(this); }
159 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
160 Activity::State state_ = Activity::State::INITED;
162 bool suspended_ = false;
163 std::vector<ActivityPtr> successors_;
164 std::set<ActivityPtr> dependencies_;
165 std::atomic_int_fast32_t refcount_{0};
168 template <class AnyActivity> class Activity_T : public Activity {
169 std::string name_ = "unnamed";
170 std::string tracing_category_ = "";
171 void* user_data_ = nullptr;
174 AnyActivity* add_successor(ActivityPtr a)
176 Activity::add_successor(a);
177 return static_cast<AnyActivity*>(this);
179 AnyActivity* remove_successor(ActivityPtr a)
181 Activity::remove_successor(a);
182 return static_cast<AnyActivity*>(this);
184 AnyActivity* set_name(const std::string& name)
186 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
188 return static_cast<AnyActivity*>(this);
190 const std::string& get_name() const override { return name_; }
191 const char* get_cname() const override { return name_.c_str(); }
193 AnyActivity* set_tracing_category(const std::string& category)
195 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
196 tracing_category_ = category;
197 return static_cast<AnyActivity*>(this);
199 const std::string& get_tracing_category() const { return tracing_category_; }
201 AnyActivity* set_user_data(void* data)
204 return static_cast<AnyActivity*>(this);
207 void* get_user_data() const { return user_data_; }
209 AnyActivity* vetoable_start()
211 Activity::vetoable_start();
212 return static_cast<AnyActivity*>(this);
215 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
216 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
217 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
219 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
220 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
225 } // namespace simgrid
227 #endif /* SIMGRID_S4U_ACTIVITY_HPP */