Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
working version
[simgrid.git] / include / simgrid / s4u / Activity.hpp
1 /* Copyright (c) 2006-2019. 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 "xbt/log.h"
11 #include <atomic>
12 #include <set>
13 #include <simgrid/forward.h>
14 #include <string>
15 #include <vector>
16 #include <xbt/signal.hpp>
17
18 namespace simgrid {
19 namespace s4u {
20
21 /** @brief Activities
22  *
23  * This class is the ancestor of every activities that an actor can undertake.
24  * That is, activities are all the things that do take time to the actor in the simulated world.
25  */
26 class XBT_PUBLIC Activity {
27   friend Comm;
28   friend XBT_PUBLIC void intrusive_ptr_release(Comm * c);
29   friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
30
31   friend Exec;
32   friend ExecSeq;
33   friend ExecPar;
34   friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
35   friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
36
37   friend Io;
38   friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
39   friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
40
41 protected:
42   Activity()  = default;
43   virtual ~Activity() = default;
44 public:
45 #ifndef DOXYGEN
46   Activity(Activity const&) = delete;
47   Activity& operator=(Activity const&) = delete;
48 #endif
49
50   enum class State { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
51
52   /** Starts a previously created activity.
53    *
54    * This function is optional: you can call wait() even if you didn't call start()
55    */
56   virtual Activity* start() = 0;
57   /** Blocks until the activity is terminated */
58   virtual Activity* wait() = 0;
59   /** Blocks until the activity is terminated, or until the timeout is elapsed
60    *  Raises: timeout exception.*/
61   virtual Activity* wait_for(double timeout) = 0;
62   /** Blocks until the activity is terminated, or until the time limit is reached
63    * Raises: timeout exception. */
64   void wait_until(double time_limit);
65
66   /** Cancel that activity */
67   virtual Activity* cancel() = 0;
68   /** Retrieve the current state of the activity */
69   Activity::State get_state() { return state_; }
70   void set_state(Activity::State state) { state_ = state; }
71   /** Tests whether the given activity is terminated yet. This is a pure function. */
72   virtual bool test() = 0;
73
74   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
75   virtual double get_remaining();
76
77   /** Set the [remaining] amount of work that this Activity will entail
78    *
79    * It is forbidden to change the amount of work once the Activity is started */
80   Activity* set_remaining(double remains);
81
82   /** Returns the internal implementation of this Activity */
83   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
84
85 private:
86   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
87   Activity::State state_                   = Activity::State::INITED;
88   double remains_                          = 0;
89 };
90
91 // template <class AnyActivity> class DependencyGuard {
92 // public:
93 //  static bool activity_start_vetoer(AnyActivity* a) { return not a->has_dependencies(); }
94 //  static void on_activity_done(AnyActivity* a);
95 ////  {
96 ////    while (a->has_successors()) {
97 ////      AnyActivity* b = a->get_successor();
98 ////      b->remove_dependency_on(a);
99 ////      if (not b->has_dependencies()) {
100 ////        XBT_INFO("Activity is done and a successor can start");
101 ////        b->vetoable_start();
102 ////      }
103 ////      a->remove_successor();
104 ////    }
105 ////  }
106 //};
107
108 template <class AnyActivity> class Activity_T : public Activity {
109 private:
110   std::string name_             = "";
111   std::string tracing_category_ = "";
112   void* user_data_              = nullptr;
113   std::atomic_int_fast32_t refcount_{0};
114   std::vector<AnyActivity*> successors_;
115   std::set<AnyActivity*> dependencies_;
116
117 public:
118 #ifndef DOXYGEN
119   friend void intrusive_ptr_release(AnyActivity* 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(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
127 #endif
128
129   void add_successor(AnyActivity* a)
130   {
131     successors_.push_back(a);
132     a->add_dependency_on(static_cast<AnyActivity*>(this));
133   }
134   void remove_successor() { successors_.pop_back(); }
135   AnyActivity* get_successor() { return successors_.back(); }
136   bool has_successors() { return not successors_.empty(); }
137
138   void add_dependency_on(AnyActivity* a) { dependencies_.insert({a}); }
139   void remove_dependency_on(AnyActivity* a) { dependencies_.erase(a); }
140   bool has_dependencies() { return not dependencies_.empty(); }
141   void release_dependencies()
142   {
143     while (has_successors()) {
144       AnyActivity* b = get_successor();
145       b->remove_dependency_on(static_cast<AnyActivity*>(this));
146       if (not b->has_dependencies()) {
147         // XBT_INFO("Activity is done and a successor can start");
148         b->vetoable_start();
149       }
150       remove_successor();
151     }
152   }
153
154   AnyActivity* vetoable_start()
155   {
156     set_state(State::STARTING);
157     if (has_dependencies())
158       return static_cast<AnyActivity*>(this);
159     //    XBT_INFO("No veto, Activity can start");
160     set_state(State::STARTED);
161     static_cast<AnyActivity*>(this)->start();
162     return static_cast<AnyActivity*>(this);
163   }
164
165   AnyActivity* set_name(const std::string& name)
166   {
167     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
168     name_ = name;
169     return static_cast<AnyActivity*>(this);
170   }
171   const std::string& get_name() { return name_; }
172   const char* get_cname() { return name_.c_str(); }
173
174   AnyActivity* set_tracing_category(const std::string& category)
175   {
176     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
177     tracing_category_ = category;
178     return static_cast<AnyActivity*>(this);
179   }
180   const std::string& get_tracing_category() { return tracing_category_; }
181
182   AnyActivity* set_user_data(void* data)
183   {
184     user_data_ = data;
185     return static_cast<AnyActivity*>(this);
186   }
187
188   void* get_user_data() { return user_data_; }
189 };
190
191 } // namespace s4u
192 } // namespace simgrid
193
194 #endif /* SIMGRID_S4U_ACTIVITY_HPP */