Logo AND Algorithmique Numérique Distribuée

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