Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'local changes'
[simgrid.git] / include / simgrid / s4u / Activity.hpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
8
9 #include <xbt/asserts.h>
10 #include <algorithm>
11 #include <atomic>
12 #include <set>
13 #include <simgrid/forward.h>
14 #include <stdexcept>
15 #include <string>
16 #include <vector>
17 #include <xbt/signal.hpp>
18 #include <xbt/utility.hpp>
19
20 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
21
22 namespace simgrid {
23 namespace s4u {
24
25 /** @brief Activities
26  *
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.
29  */
30 class XBT_PUBLIC Activity {
31   friend Comm;
32   friend Exec;
33   friend Io;
34
35 protected:
36   Activity()  = default;
37   virtual ~Activity() = default;
38
39   virtual bool is_assigned() const = 0;
40
41   void release_dependencies()
42   {
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()) {
48         b->vetoable_start();
49       }
50       successors_.pop_back();
51     }
52   }
53
54   void add_successor(ActivityPtr a)
55   {
56     if(this == 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");
61
62     successors_.push_back(a);
63     a->dependencies_.insert({this});
64   }
65
66   void remove_successor(ActivityPtr a)
67   {
68     if(this == a)
69       throw std::invalid_argument("Cannot ask to remove its from successors");
70
71     auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
72     if (p != successors_.end()){
73       successors_.erase(p);
74       a->dependencies_.erase({this});
75     } else
76       throw std::invalid_argument("Dependency does not exist. Can not be removed.");
77
78   }
79
80 public:
81   void vetoable_start()
82   {
83     state_ = State::STARTING;
84     if (dependencies_.empty() && is_assigned()) {
85       XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
86       start();
87     }
88   }
89
90 #ifndef DOXYGEN
91   Activity(Activity const&) = delete;
92   Activity& operator=(Activity const&) = delete;
93 #endif
94
95   // enum class State { ... }
96   XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, CANCELED, FINISHED);
97
98   /** Starts a previously created activity.
99    *
100    * This function is optional: you can call wait() even if you didn't call start()
101    */
102   virtual Activity* start() = 0;
103   /** Blocks the current actor until the activity is terminated */
104   virtual Activity* wait() = 0;
105   /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
106    *  Raises: timeout exception.*/
107   virtual Activity* wait_for(double timeout) = 0;
108   /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
109    * Raises: timeout exception. */
110   void wait_until(double time_limit);
111
112   /** Cancel that activity */
113   virtual Activity* cancel() = 0;
114   /** Retrieve the current state of the activity */
115   Activity::State get_state() const { return state_; }
116   /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
117   const char* get_state_str() const;
118   void set_state(Activity::State state) { state_ = state; }
119   /** Tests whether the given activity is terminated yet. */
120   virtual bool test();
121
122   /** Blocks the progression of this activity until it gets resumed */
123   virtual Activity* suspend();
124   /** Unblock the progression of this activity if it was suspended previously */
125   virtual Activity* resume();
126   /** Whether or not the progression of this activity is blocked */
127   bool is_suspended() const { return suspended_; }
128
129   virtual const char* get_cname() const       = 0;
130   virtual const std::string& get_name() const = 0;
131
132   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
133   virtual double get_remaining() const;
134   /** Set the [remaining] amount of work that this Activity will entail
135    *
136    * It is forbidden to change the amount of work once the Activity is started */
137   Activity* set_remaining(double remains);
138
139   /** Returns the internal implementation of this Activity */
140   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
141
142 #ifndef DOXYGEN
143   friend void intrusive_ptr_release(Activity* a)
144   {
145     if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
146       std::atomic_thread_fence(std::memory_order_acquire);
147       delete a;
148     }
149   }
150   friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
151 #endif
152   Activity* add_ref()
153   {
154     intrusive_ptr_add_ref(this);
155     return this;
156   }
157   void unref() { intrusive_ptr_release(this); }
158
159 private:
160   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
161   Activity::State state_                   = Activity::State::INITED;
162   double remains_                          = 0;
163   bool suspended_                          = false;
164   std::vector<ActivityPtr> successors_;
165   std::set<ActivityPtr> dependencies_;
166   std::atomic_int_fast32_t refcount_{0};
167 };
168
169 template <class AnyActivity> class Activity_T : public Activity {
170   std::string name_             = "unnamed";
171   std::string tracing_category_ = "";
172   void* user_data_              = nullptr;
173
174 public:
175   AnyActivity* add_successor(ActivityPtr a)
176   {
177     Activity::add_successor(a);
178     return static_cast<AnyActivity*>(this);
179   }
180   AnyActivity* remove_successor(ActivityPtr a)
181   {
182     Activity::remove_successor(a);
183     return static_cast<AnyActivity*>(this);
184   }
185   AnyActivity* set_name(const std::string& name)
186   {
187     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
188     name_ = name;
189     return static_cast<AnyActivity*>(this);
190   }
191   const std::string& get_name() const override { return name_; }
192   const char* get_cname() const override { return name_.c_str(); }
193
194   AnyActivity* set_tracing_category(const std::string& category)
195   {
196     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
197     tracing_category_ = category;
198     return static_cast<AnyActivity*>(this);
199   }
200   const std::string& get_tracing_category() const { return tracing_category_; }
201
202   AnyActivity* set_user_data(void* data)
203   {
204     user_data_ = data;
205     return static_cast<AnyActivity*>(this);
206   }
207
208   void* get_user_data() const { return user_data_; }
209
210   AnyActivity* vetoable_start()
211   {
212     Activity::vetoable_start();
213     return static_cast<AnyActivity*>(this);
214   }
215 #ifndef DOXYGEN
216   /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
217    * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
218    * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
219    */
220   friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
221   friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
222 #endif
223 };
224
225 } // namespace s4u
226 } // namespace simgrid
227
228 #endif /* SIMGRID_S4U_ACTIVITY_HPP */