Logo AND Algorithmique Numérique Distribuée

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