Logo AND Algorithmique Numérique Distribuée

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