Fix virtual struct bug
authorDave Watson <davejwatson@fb.com>
Wed, 26 Apr 2017 16:56:26 +0000 (09:56 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 26 Apr 2017 17:05:11 +0000 (10:05 -0700)
Summary: virtual classes currently don't work in hazard pointers, and get incorrectly reclaimed.

Reviewed By: magedm

Differential Revision: D4951584

fbshipit-source-id: 8200df6bb8d500af2e89086edf7835d4fb90b6a2

folly/experimental/hazptr/hazptr-impl.h
folly/experimental/hazptr/hazptr.h
folly/experimental/hazptr/test/HazptrTest.cpp

index 1ed1524448a7ec7276fb21b2dd32901f377ed5e2..ecf6865d972cbaa607cb0a2fa209c9148ca1b581 100644 (file)
@@ -108,8 +108,9 @@ inline T* hazptr_owner<T>::get_protected(const A& src) noexcept {
 
 template <typename T>
 inline void hazptr_owner<T>::set(const T* ptr) noexcept {
-  DEBUG_PRINT(this << " " << ptr);
-  hazptr_->set(ptr);
+  auto p = static_cast<hazptr_obj*>(const_cast<T*>(ptr));
+  DEBUG_PRINT(this << " " << ptr << " p:" << p);
+  hazptr_->set(p);
 }
 
 template <typename T>
index 464dc1f3f792e5bbb8e7b27b4bf3b497397098cb..d06cefcb8077956196487441e3ba4bf4edfcd8b1 100644 (file)
@@ -89,7 +89,7 @@ class hazptr_obj {
 
 /** Definition of hazptr_obj_base */
 template <typename T, typename Deleter = std::default_delete<T>>
-class hazptr_obj_base : private hazptr_obj {
+class hazptr_obj_base : public hazptr_obj {
  public:
   /* Retire a removed object and pass the responsibility for
    * reclaiming it to the hazptr library */
index bcabbe7258e4615161d3d51668373049955e23f2..fa4ac1ec2f196f0508efa1babd9c0f6973e7c048 100644 (file)
@@ -238,3 +238,21 @@ TEST_F(HazptrTest, WIDECAS) {
   ret = s.cas(u, v);
   CHECK(ret);
 }
+
+TEST_F(HazptrTest, VirtualTest) {
+  struct Thing : public hazptr_obj_base<Thing> {
+    virtual ~Thing() {
+      DEBUG_PRINT("this: " << this << " &a: " << &a << " a: " << a);
+    }
+    int a;
+  };
+  for (int i = 0; i < 100; i++) {
+    auto bar = new Thing;
+    bar->a = i;
+
+    hazptr_owner<Thing> hptr;
+    hptr.set(bar);
+    bar->retire();
+    EXPECT_EQ(bar->a, i);
+  }
+}