Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cannot do anything with dependencies
[simgrid.git] / include / simgrid / s4u / Activity.hpp
1 /* Copyright (c) 2006-2021. 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 <algorithm>
11 #include <atomic>
12 #include <set>
13 #include <simgrid/forward.h>
14 #include <string>
15 #include <vector>
16 #include <xbt/signal.hpp>
17 #include <xbt/utility.hpp>
18
19 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
20
21 namespace simgrid {
22 namespace s4u {
23
24 /** @brief Activities
25  *
26  * This class is the ancestor of every activities that an actor can undertake.
27  * That is, activities are all the things that do take time to the actor in the simulated world.
28  */
29 class XBT_PUBLIC Activity {
30   friend Comm;
31   friend Exec;
32   friend Io;
33
34 protected:
35   Activity()  = default;
36   virtual ~Activity() = default;
37
38   virtual bool is_assigned() const = 0;
39
40   void release_dependencies()
41   {
42     while (not successors_.empty()) {
43       ActivityPtr b = successors_.back();
44       XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
45       b->dependencies_.erase(this);
46       if (b->dependencies_.empty()) {
47         b->vetoable_start();
48       }
49       successors_.pop_back();
50     }
51   }
52
53   void add_successor(ActivityPtr a)
54   {
55     if(this == a)
56       throw std::invalid_argument("Cannot be its own successor");
57     auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
58     if (p != successors_.end())
59       throw std::invalid_argument("Dependency already exists");
60
61     successors_.push_back(a);
62     a->dependencies_.insert({this});
63   }
64
65   void remove_successor(ActivityPtr a)
66   {
67     if(this == a)
68       throw std::invalid_argument("Cannot ask to remove its from successors");
69
70     auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
71     if (p != successors_.end()){
72       successors_.erase(p);
73       a->dependencies_.erase({this});
74     } else
75       throw std::invalid_argument("Dependency does not exist. Can not be removed.");
76
77   }
78
79 public:
80   void vetoable_start()
81   {
82     state_ = State::STARTING;
83     if (dependencies_.empty() && is_assigned()) {
84       XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
85       start();
86     }
87   }
88
89 #ifndef DOXYGEN
90   Activity(Activity const&) = delete;
91   Activity& operator=(Activity const&) = delete;
92 #endif
93
94   // enum class State { ... }
95   XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, CANCELED, FINISHED);
96
97   /** Starts a previously created activity.
98    *
99    * This function is optional: you can call wait() even if you didn't call start()
100    */
101   virtual Activity* start() = 0;
102   /** Blocks the current actor until the activity is terminated */
103   virtual Activity* wait() = 0;
104   /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
105    *  Raises: timeout exception.*/
106   virtual Activity* wait_for(double timeout) = 0;
107   /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
108    * Raises: timeout exception. */
109   void wait_until(double time_limit);
110
111   /** Cancel that activity */
112   virtual Activity* cancel() = 0;
113   /** Retrieve the current state of the activity */
114   Activity::State get_state() const { return state_; }
115   /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
116   const char* get_state_str() const;
117   void set_state(Activity::State state) { state_ = state; }
118   /** Tests whether the given activity is terminated yet. */
119   virtual bool test();
120
121   /** Blocks the progression of this activity until it gets resumed */
122   virtual Activity* suspend();
123   /** Unblock the progression of this activity if it was suspended previously */
124   virtual Activity* resume();
125   /** Whether or not the progression of this activity is blocked */
126   bool is_suspended() const { return suspended_; }
127
128   virtual const char* get_cname() const       = 0;
129   virtual const std::string& get_name() const = 0;
130
131   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
132   virtual double get_remaining() const;
133   /** Set the [remaining] amount of work that this Activity will entail
134    *
135    * It is forbidden to change the amount of work once the Activity is started */
136   Activity* set_remaining(double remains);
137
138   /** Returns the internal implementation of this Activity */
139   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
140
141 #ifndef DOXYGEN
142   friend void intrusive_ptr_release(Activity* a)
143   {
144     if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
145       std::atomic_thread_fence(std::memory_order_acquire);
146       delete a;
147     }
148   }
149   friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
150 #endif
151   Activity* add_ref()
152   {
153     intrusive_ptr_add_ref(this);
154     return this;
155   }
156   void unref() { intrusive_ptr_release(this); }
157
158 private:
159   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
160   Activity::State state_                   = Activity::State::INITED;
161   double remains_                          = 0;
162   bool suspended_                          = false;
163   std::vector<ActivityPtr> successors_;
164   std::set<ActivityPtr> dependencies_;
165   std::atomic_int_fast32_t refcount_{0};
166 };
167
168 template <class AnyActivity> class Activity_T : public Activity {
169   std::string name_             = "unnamed";
170   std::string tracing_category_ = "";
171   void* user_data_              = nullptr;
172
173 public:
174   AnyActivity* add_successor(ActivityPtr a)
175   {
176     Activity::add_successor(a);
177     return static_cast<AnyActivity*>(this);
178   }
179   AnyActivity* remove_successor(ActivityPtr a)
180   {
181     Activity::remove_successor(a);
182     return static_cast<AnyActivity*>(this);
183   }
184   AnyActivity* set_name(const std::string& name)
185   {
186     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
187     name_ = name;
188     return static_cast<AnyActivity*>(this);
189   }
190   const std::string& get_name() const override { return name_; }
191   const char* get_cname() const override { return name_.c_str(); }
192
193   AnyActivity* set_tracing_category(const std::string& category)
194   {
195     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
196     tracing_category_ = category;
197     return static_cast<AnyActivity*>(this);
198   }
199   const std::string& get_tracing_category() const { return tracing_category_; }
200
201   AnyActivity* set_user_data(void* data)
202   {
203     user_data_ = data;
204     return static_cast<AnyActivity*>(this);
205   }
206
207   void* get_user_data() const { return user_data_; }
208
209   AnyActivity* vetoable_start()
210   {
211     Activity::vetoable_start();
212     return static_cast<AnyActivity*>(this);
213   }
214 #ifndef DOXYGEN
215   /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
216    * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
217    * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
218    */
219   friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
220   friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
221 #endif
222 };
223
224 } // namespace s4u
225 } // namespace simgrid
226
227 #endif /* SIMGRID_S4U_ACTIVITY_HPP */