Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix the refcounting in this C example
[simgrid.git] / include / simgrid / s4u / Activity.hpp
1 /* Copyright (c) 2006-2020. 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 ExecSeq;
31   friend ExecPar;
32   friend Io;
33
34 protected:
35   Activity()  = default;
36   virtual ~Activity() = default;
37
38   void release_dependencies()
39   {
40     while (not successors_.empty()) {
41       ActivityPtr b = successors_.back();
42       XBT_CDEBUG(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   kernel::activity::ActivityImpl* get_impl() { return pimpl_.get(); }
59   void vetoable_start()
60   {
61     state_ = State::STARTING;
62     if (dependencies_.empty()) {
63       XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", 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 { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
74
75   /** Starts a previously created activity.
76    *
77    * This function is optional: you can call wait() even if you didn't call start()
78    */
79   virtual Activity* start() = 0;
80   /** Blocks until the activity is terminated */
81   virtual Activity* wait() = 0;
82   /** Blocks until the activity is terminated, or until the timeout is elapsed
83    *  Raises: timeout exception.*/
84   virtual Activity* wait_for(double timeout) = 0;
85   /** Blocks until the activity is terminated, or until the time limit is reached
86    * Raises: timeout exception. */
87   void wait_until(double time_limit);
88
89   /** Cancel that activity */
90   virtual Activity* cancel() = 0;
91   /** Retrieve the current state of the activity */
92   Activity::State get_state() { return state_; }
93   void set_state(Activity::State state) { state_ = state; }
94   /** Tests whether the given activity is terminated yet. This is a pure function. */
95   virtual bool test() = 0;
96   virtual const char* get_cname()       = 0;
97   virtual const std::string& get_name() = 0;
98
99   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
100   virtual double get_remaining();
101   /** Set the [remaining] amount of work that this Activity will entail
102    *
103    * It is forbidden to change the amount of work once the Activity is started */
104   Activity* set_remaining(double remains);
105
106   /** Returns the internal implementation of this Activity */
107   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
108
109 #ifndef DOXYGEN
110   friend void intrusive_ptr_release(Activity* a)
111   {
112     if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
113       std::atomic_thread_fence(std::memory_order_acquire);
114       delete a;
115     }
116   }
117   friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
118 #endif
119   Activity* add_ref()
120   {
121     intrusive_ptr_add_ref(this);
122     return this;
123   }
124   Activity* unref()
125   {
126     intrusive_ptr_release(this);
127     return this;
128   }
129
130 private:
131   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
132   Activity::State state_                   = Activity::State::INITED;
133   double remains_                          = 0;
134   std::vector<ActivityPtr> successors_;
135   std::set<ActivityPtr> dependencies_;
136   std::atomic_int_fast32_t refcount_{0};
137 };
138
139 template <class AnyActivity> class Activity_T : public Activity {
140   std::string name_             = "unnamed";
141   std::string tracing_category_ = "";
142   void* user_data_              = nullptr;
143
144 public:
145   AnyActivity* add_successor(ActivityPtr a)
146   {
147     Activity::add_successor(a);
148     return static_cast<AnyActivity*>(this);
149   }
150
151   AnyActivity* set_name(const std::string& name)
152   {
153     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
154     name_ = name;
155     return static_cast<AnyActivity*>(this);
156   }
157   const std::string& get_name() { return name_; }
158   const char* get_cname() { return name_.c_str(); }
159
160   AnyActivity* set_tracing_category(const std::string& category)
161   {
162     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
163     tracing_category_ = category;
164     return static_cast<AnyActivity*>(this);
165   }
166   const std::string& get_tracing_category() { return tracing_category_; }
167
168   AnyActivity* set_user_data(void* data)
169   {
170     user_data_ = data;
171     return static_cast<AnyActivity*>(this);
172   }
173
174   void* get_user_data() { return user_data_; }
175 #ifndef DOXYGEN
176   /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
177    * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
178    * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
179    */
180   friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
181   friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
182 #endif
183 };
184
185 } // namespace s4u
186 } // namespace simgrid
187
188 #endif /* SIMGRID_S4U_ACTIVITY_HPP */