Logo AND Algorithmique Numérique Distribuée

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