Logo AND Algorithmique Numérique Distribuée

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