Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / include / simgrid / s4u / Activity.hpp
index 0680700..67fe59b 100644 (file)
@@ -26,18 +26,10 @@ 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;
@@ -83,10 +75,21 @@ 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::atomic_int_fast32_t refcount_{0};
 };
 
 template <class AnyActivity> class Activity_T : public Activity {
@@ -94,21 +97,10 @@ 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)
   {
@@ -172,6 +164,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