From 19ee8eb06cd8075d263d19bd85d2c28ea3741e45 Mon Sep 17 00:00:00 2001
From: khizmax <libcds.dev@gmail.com>
Date: Tue, 17 Jan 2017 22:05:07 +0300
Subject: [PATCH] Fixed HP/DHP thread data initialization

---
 cds/gc/dhp_smr.h |  9 ++++++---
 cds/gc/hp_smr.h  |  3 +++
 src/hp.cpp       | 14 ++++++++------
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/cds/gc/dhp_smr.h b/cds/gc/dhp_smr.h
index d8c3e9f6..7d0b5d7d 100644
--- a/cds/gc/dhp_smr.h
+++ b/cds/gc/dhp_smr.h
@@ -73,8 +73,8 @@ namespace cds { namespace gc {
         public:
             static hp_allocator& instance();
 
-            CDS_EXPORT_API guard_block*    alloc();
-            void            free( guard_block* block )
+            CDS_EXPORT_API guard_block* alloc();
+            void free( guard_block* block )
             {
                 free_list_.put( block );
             }
@@ -98,7 +98,10 @@ namespace cds { namespace gc {
                 , extended_list_( nullptr )
                 , array_( arr )
                 , initial_capacity_( nSize )
-            {}
+            {
+                // Initialize guards
+                new( arr ) guard[nSize];
+            }
 
             thread_hp_storage() = delete;
             thread_hp_storage( thread_hp_storage const& ) = delete;
diff --git a/cds/gc/hp_smr.h b/cds/gc/hp_smr.h
index 2fb7c307..55111780 100644
--- a/cds/gc/hp_smr.h
+++ b/cds/gc/hp_smr.h
@@ -74,6 +74,9 @@ namespace cds { namespace gc {
                 , free_guard_count_(0)
 #       endif
             {
+                // Initialize guards
+                new( arr ) guard[nSize];
+
                 for ( guard* pEnd = arr + nSize - 1; arr < pEnd; ++arr )
                     arr->next_ = arr + 1;
                 arr->next_ = nullptr;
diff --git a/src/hp.cpp b/src/hp.cpp
index 897e119a..b678cfc8 100644
--- a/src/hp.cpp
+++ b/src/hp.cpp
@@ -341,7 +341,7 @@ namespace cds { namespace gc { namespace hp {
         {
             retired_ptr dummy_retired;
             while ( pNode ) {
-                if ( !pNode->m_bFree.load( atomics::memory_order_relaxed )) {
+                if ( pNode->m_idOwner.load( atomics::memory_order_relaxed ) != cds::OS::c_NullThreadId ) {
                     thread_hp_storage& hpstg = pNode->hazards_;
                     for ( size_t i = 0; i < hazard_ptr_count_; ++i ) {
                         pRec->sync();
@@ -397,11 +397,13 @@ namespace cds { namespace gc { namespace hp {
         thread_record* pNode = thread_list_.load( atomics::memory_order_acquire );
 
         while ( pNode ) {
-            for ( size_t i = 0; i < get_hazard_ptr_count(); ++i ) {
-                pRec->sync();
-                void * hptr = pNode->hazards_[i].get();
-                if ( hptr )
-                    plist.push_back( hptr );
+            if ( pNode->m_idOwner.load( std::memory_order_relaxed ) != cds::OS::c_NullThreadId ) {
+                for ( size_t i = 0; i < get_hazard_ptr_count(); ++i ) {
+                    pRec->sync();
+                    void * hptr = pNode->hazards_[i].get();
+                    if ( hptr )
+                        plist.push_back( hptr );
+                }
             }
             pNode = pNode->m_pNextNode.load( atomics::memory_order_relaxed );
         }
-- 
2.34.1