Logo AND Algorithmique Numérique Distribuée

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