2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_GC_DETAILS_HP_COMMON_H
32 #define CDSLIB_GC_DETAILS_HP_COMMON_H
34 #include <cds/algo/atomic.h>
35 #include <cds/gc/details/retired_ptr.h>
37 #ifdef CDS_ENABLE_HPSTAT
38 # define CDS_HPSTAT( expr ) expr
40 # define CDS_HPSTAT( expr )
44 namespace cds { namespace gc { namespace hp { namespace common {
46 /// Hazard pointer type
47 typedef void* hazard_ptr;
50 using cds::gc::details::retired_ptr;
51 using cds::gc::make_retired_ptr;
53 /// Hazard pointer guard
63 T* operator=( T* ptr ) noexcept
69 std::nullptr_t operator=( std::nullptr_t ) noexcept
75 hazard_ptr get() const noexcept
77 return hp_.load( atomics::memory_order_acquire );
80 hazard_ptr get( atomics::memory_order order ) const noexcept
82 return hp_.load( order );
86 T* get_as() const noexcept
88 return reinterpret_cast<T*>( get());
92 void set( T* ptr ) noexcept
94 hp_.store( reinterpret_cast<hazard_ptr>( ptr ), atomics::memory_order_release );
97 void clear( atomics::memory_order order ) noexcept
99 hp_.store( nullptr, order );
102 void clear() noexcept
104 clear( atomics::memory_order_release );
108 atomics::atomic<hazard_ptr> hp_;
111 guard* next_; // free guard list
115 template <size_t Capacity>
119 static size_t const c_nCapacity = Capacity;
126 static constexpr size_t capacity()
131 guard* operator[]( size_t idx ) const noexcept
133 assert( idx < capacity());
137 template <typename T>
138 void set( size_t idx, T* ptr ) noexcept
140 assert( idx < capacity());
141 assert( arr_[idx] != nullptr );
143 arr_[idx]->set( ptr );
146 void clear( size_t idx ) noexcept
148 assert( idx < capacity());
149 assert( arr_[idx] != nullptr );
154 guard* release( size_t idx ) noexcept
156 assert( idx < capacity());
158 guard* g = arr_[idx];
163 void reset( size_t idx, guard* g ) noexcept
165 assert( idx < capacity());
166 assert( arr_[idx] == nullptr );
172 guard* arr_[c_nCapacity];
176 /// Retired pointer disposer
177 typedef void ( *disposer_func )( void* );
179 }}}} // namespace cds::gc::hp::common
182 #endif // #ifndef CDSLIB_GC_DETAILS_HP_COMMON_H