Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[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 XBT_PUBLIC void intrusive_ptr_release(Comm * c);
30   friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
31
32   friend Exec;
33   friend ExecSeq;
34   friend ExecPar;
35   friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
36   friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
37
38   friend Io;
39   friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
40   friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
41
42 protected:
43   Activity()  = default;
44   virtual ~Activity() = default;
45 public:
46 #ifndef DOXYGEN
47   Activity(Activity const&) = delete;
48   Activity& operator=(Activity const&) = delete;
49 #endif
50
51   enum class State { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
52
53   /** Starts a previously created activity.
54    *
55    * This function is optional: you can call wait() even if you didn't call start()
56    */
57   virtual Activity* start() = 0;
58   /** Blocks until the activity is terminated */
59   virtual Activity* wait() = 0;
60   /** Blocks until the activity is terminated, or until the timeout is elapsed
61    *  Raises: timeout exception.*/
62   virtual Activity* wait_for(double timeout) = 0;
63   /** Blocks until the activity is terminated, or until the time limit is reached
64    * Raises: timeout exception. */
65   void wait_until(double time_limit);
66
67   /** Cancel that activity */
68   virtual Activity* cancel() = 0;
69   /** Retrieve the current state of the activity */
70   Activity::State get_state() { return state_; }
71   void set_state(Activity::State state) { state_ = state; }
72   /** Tests whether the given activity is terminated yet. This is a pure function. */
73   virtual bool test() = 0;
74
75   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
76   virtual double get_remaining();
77
78   /** Set the [remaining] amount of work that this Activity will entail
79    *
80    * It is forbidden to change the amount of work once the Activity is started */
81   Activity* set_remaining(double remains);
82
83   /** Returns the internal implementation of this Activity */
84   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
85
86 private:
87   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
88   Activity::State state_                   = Activity::State::INITED;
89   double remains_                          = 0;
90 };
91
92 template <class AnyActivity> class Activity_T : public Activity {
93 private:
94   std::string name_             = "unnamed";
95   std::string tracing_category_ = "";
96   void* user_data_              = nullptr;
97   std::atomic_int_fast32_t refcount_{0};
98   std::vector<Activity*> successors_;
99   std::set<Activity*> dependencies_;
100
101 public:
102 #ifndef DOXYGEN
103   friend void intrusive_ptr_release(AnyActivity* a)
104   {
105     if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
106       std::atomic_thread_fence(std::memory_order_acquire);
107       delete a;
108     }
109   }
110   friend void intrusive_ptr_add_ref(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
111 #endif
112
113   void add_successor(Activity* a)
114   {
115     successors_.push_back(a);
116     static_cast<AnyActivity*>(a)->add_dependency_on(static_cast<Activity*>(this));
117   }
118   void remove_successor() { successors_.pop_back(); }
119   Activity* get_successor() { return successors_.back(); }
120   bool has_successors() { return not successors_.empty(); }
121
122   void add_dependency_on(Activity* a) { dependencies_.insert({a}); }
123   void remove_dependency_on(Activity* a) { dependencies_.erase(a); }
124   bool has_dependencies() { return not dependencies_.empty(); }
125   void release_dependencies()
126   {
127     while (has_successors()) {
128       AnyActivity* b = static_cast<AnyActivity*>(get_successor());
129       XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", static_cast<AnyActivity*>(this)->get_cname(),
130                  b->get_cname());
131       b->remove_dependency_on(static_cast<Activity*>(this));
132       if (not b->has_dependencies()) {
133         b->vetoable_start();
134       }
135       remove_successor();
136     }
137   }
138
139   AnyActivity* vetoable_start()
140   {
141     set_state(State::STARTING);
142     if (has_dependencies())
143       return static_cast<AnyActivity*>(this);
144     set_state(State::STARTED);
145     XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'",
146                static_cast<AnyActivity*>(this)->get_cname());
147     static_cast<AnyActivity*>(this)->start();
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 };
176
177 } // namespace s4u
178 } // namespace simgrid
179
180 #endif /* SIMGRID_S4U_ACTIVITY_HPP */