Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: fix "Malformed whitespace in C++" spotted by codefactor.io.
[simgrid.git] / include / simgrid / s4u / Activity.hpp
index 0680700..1757964 100644 (file)
@@ -26,23 +26,44 @@ namespace s4u {
  */
 class XBT_PUBLIC Activity {
   friend Comm;
-  friend XBT_PUBLIC void intrusive_ptr_release(Comm * c);
-  friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
-
   friend Exec;
   friend ExecSeq;
   friend ExecPar;
-  friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
-  friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
-
   friend Io;
-  friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
-  friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
 
 protected:
   Activity()  = default;
   virtual ~Activity() = default;
+
+  void release_dependencies()
+  {
+    while (not successors_.empty()) {
+      ActivityPtr b = successors_.back();
+      XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
+      b->dependencies_.erase(this);
+      if (b->dependencies_.empty()) {
+        b->vetoable_start();
+      }
+      successors_.pop_back();
+    }
+  }
+
+  void add_successor(ActivityPtr a)
+  {
+    successors_.push_back(a);
+    a->dependencies_.insert({this});
+  }
+
 public:
+  void vetoable_start()
+  {
+    state_ = State::STARTING;
+    if (dependencies_.empty()) {
+      XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", get_cname());
+      start();
+    }
+  }
+
 #ifndef DOXYGEN
   Activity(Activity const&) = delete;
   Activity& operator=(Activity const&) = delete;
@@ -71,10 +92,11 @@ public:
   void set_state(Activity::State state) { state_ = state; }
   /** Tests whether the given activity is terminated yet. This is a pure function. */
   virtual bool test() = 0;
+  virtual const char* get_cname()       = 0;
+  virtual const std::string& get_name() = 0;
 
   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
   virtual double get_remaining();
-
   /** Set the [remaining] amount of work that this Activity will entail
    *
    * It is forbidden to change the amount of work once the Activity is started */
@@ -83,68 +105,35 @@ public:
   /** Returns the internal implementation of this Activity */
   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
 
+#ifndef DOXYGEN
+  friend void intrusive_ptr_release(Activity* a)
+  {
+    if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
+      std::atomic_thread_fence(std::memory_order_acquire);
+      delete a;
+    }
+  }
+  friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
+#endif
+
 private:
   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
   Activity::State state_                   = Activity::State::INITED;
   double remains_                          = 0;
+  std::vector<ActivityPtr> successors_;
+  std::set<ActivityPtr> dependencies_;
+  std::atomic_int_fast32_t refcount_{0};
 };
 
 template <class AnyActivity> class Activity_T : public Activity {
-private:
   std::string name_             = "unnamed";
   std::string tracing_category_ = "";
   void* user_data_              = nullptr;
-  std::atomic_int_fast32_t refcount_{0};
-  std::vector<Activity*> successors_;
-  std::set<Activity*> dependencies_;
 
 public:
-#ifndef DOXYGEN
-  friend void intrusive_ptr_release(AnyActivity* a)
-  {
-    if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
-      std::atomic_thread_fence(std::memory_order_acquire);
-      delete a;
-    }
-  }
-  friend void intrusive_ptr_add_ref(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
-#endif
-
-  void add_successor(Activity* a)
+  AnyActivity* add_successor(ActivityPtr a)
   {
-    successors_.push_back(a);
-    static_cast<AnyActivity*>(a)->add_dependency_on(static_cast<Activity*>(this));
-  }
-  void remove_successor() { successors_.pop_back(); }
-  Activity* get_successor() { return successors_.back(); }
-  bool has_successors() { return not successors_.empty(); }
-
-  void add_dependency_on(Activity* a) { dependencies_.insert({a}); }
-  void remove_dependency_on(Activity* a) { dependencies_.erase(a); }
-  bool has_dependencies() { return not dependencies_.empty(); }
-  void release_dependencies()
-  {
-    while (has_successors()) {
-      AnyActivity* b = static_cast<AnyActivity*>(get_successor());
-      XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", static_cast<AnyActivity*>(this)->get_cname(),
-                 b->get_cname());
-      b->remove_dependency_on(static_cast<Activity*>(this));
-      if (not b->has_dependencies()) {
-        b->vetoable_start();
-      }
-      remove_successor();
-    }
-  }
-
-  AnyActivity* vetoable_start()
-  {
-    set_state(State::STARTING);
-    if (has_dependencies())
-      return static_cast<AnyActivity*>(this);
-    set_state(State::STARTED);
-    XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'",
-               static_cast<AnyActivity*>(this)->get_cname());
-    static_cast<AnyActivity*>(this)->start();
+    Activity::add_successor(a);
     return static_cast<AnyActivity*>(this);
   }
 
@@ -172,6 +161,14 @@ public:
   }
 
   void* get_user_data() { return user_data_; }
+#ifndef DOXYGEN
+  /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
+   * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
+   * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
+   */
+  friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
+  friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
+#endif
 };
 
 } // namespace s4u