Logo AND Algorithmique Numérique Distribuée

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