Commit 92f554a5 authored by Dave Watson's avatar Dave Watson Committed by Facebook Github Bot

Fix virtual struct bug

Summary: virtual classes currently don't work in hazard pointers, and get incorrectly reclaimed.

Reviewed By: magedm

Differential Revision: D4951584

fbshipit-source-id: 8200df6bb8d500af2e89086edf7835d4fb90b6a2
parent ce979260
......@@ -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>
......
......@@ -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 */
......
......@@ -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);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment