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<AnyActivity*> successors_;
99   std::set<AnyActivity*> 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(AnyActivity* a)
114   {
115     successors_.push_back(a);
116     a->add_dependency_on(static_cast<AnyActivity*>(this));
117   }
118   void remove_successor() { successors_.pop_back(); }
119   AnyActivity* get_successor() { return successors_.back(); }
120   bool has_successors() { return not successors_.empty(); }
121
122   void add_dependency_on(AnyActivity* a) { dependencies_.insert({a}); }
123   void remove_dependency_on(AnyActivity* a) { dependencies_.erase(a); }
124   bool has_dependencies() { return not dependencies_.empty(); }
125   void release_dependencies()
126   {
127     while (has_successors()) {
128       AnyActivity* b = get_successor();
129       XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
130       b->remove_dependency_on(static_cast<AnyActivity*>(this));
131       if (not b->has_dependencies()) {
132         b->vetoable_start();
133       }
134       remove_successor();
135     }
136   }
137
138   AnyActivity* vetoable_start()
139   {
140     set_state(State::STARTING);
141     if (has_dependencies())
142       return static_cast<AnyActivity*>(this);
143     set_state(State::STARTED);
144     XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", get_cname());
145     static_cast<AnyActivity*>(this)->start();
146     return static_cast<AnyActivity*>(this);
147   }
148
149   AnyActivity* set_name(const std::string& name)
150   {
151     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
152     name_ = name;
153     return static_cast<AnyActivity*>(this);
154   }
155   const std::string& get_name() { return name_; }
156   const char* get_cname() { return name_.c_str(); }
157
158   AnyActivity* set_tracing_category(const std::string& category)
159   {
160     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
161     tracing_category_ = category;
162     return static_cast<AnyActivity*>(this);
163   }
164   const std::string& get_tracing_category() { return tracing_category_; }
165
166   AnyActivity* set_user_data(void* data)
167   {
168     user_data_ = data;
169     return static_cast<AnyActivity*>(this);
170   }
171
172   void* get_user_data() { return user_data_; }
173 };
174
175 } // namespace s4u
176 } // namespace simgrid
177
178 #endif /* SIMGRID_S4U_ACTIVITY_HPP */