3 #ifndef __CDS_GC_HP_DECL_H
4 #define __CDS_GC_HP_DECL_H
6 #include <cds/gc/hzp/hzp.h>
7 #include <cds/details/marked_ptr.h>
9 namespace cds { namespace gc {
10 /// @defgroup cds_garbage_collector Garbage collectors
12 /// Hazard Pointer garbage collector
13 /** @ingroup cds_garbage_collector
14 @headerfile cds/gc/hp.h
16 This class realizes a wrapper for Hazard Pointer garbage collector internal implementation.
19 - [2002] Maged M.Michael "Safe memory reclamation for dynamic lock-freeobjects using atomic reads and writes"
20 - [2003] Maged M.Michael "Hazard Pointers: Safe memory reclamation for lock-free objects"
21 - [2004] Andrei Alexandrescy, Maged Michael "Lock-free Data Structures with Hazard Pointers"
23 See \ref cds_how_to_use "How to use" section for details of garbage collector applying.
28 /// Native guarded pointer type
29 typedef gc::hzp::hazard_pointer guarded_pointer;
33 @headerfile cds/gc/hp.h
35 template <typename T> using atomic_ref = atomics::atomic<T *>;
37 /// Atomic marked pointer
39 @headerfile cds/gc/hp.h
41 template <typename MarkedPtr> using atomic_marked_ptr = atomics::atomic<MarkedPtr>;
45 @headerfile cds/gc/hp.h
47 template <typename T> using atomic_type = atomics::atomic<T>;
49 /// Thread GC implementation for internal usage
50 typedef hzp::ThreadGC thread_gc_impl;
52 /// Wrapper for hzp::ThreadGC class
54 @headerfile cds/gc/hp.h
55 This class performs automatically attaching/detaching Hazard Pointer GC
56 for the current thread.
58 class thread_gc: public thread_gc_impl
67 The constructor attaches the current thread to the Hazard Pointer GC
68 if it is not yet attached.
69 The \p bPersistent parameter specifies attachment persistence:
70 - \p true - the class destructor will not detach the thread from Hazard Pointer GC.
71 - \p false (default) - the class destructor will detach the thread from Hazard Pointer GC.
74 bool bPersistent = false
75 ) ; //inline in hp_impl.h
79 If the object has been created in persistent mode, the destructor does nothing.
80 Otherwise it detaches the current thread from Hazard Pointer GC.
82 ~thread_gc() ; // inline in hp_impl.h
85 /// Hazard Pointer guard
87 @headerfile cds/gc/hp.h
88 This class is a wrapper for hzp::AutoHPGuard.
90 class Guard: public hzp::AutoHPGuard
93 typedef hzp::AutoHPGuard base_class;
98 Guard() ; // inline in hp_impl.h
101 /// Protects a pointer of type \p atomic<T*>
103 Return the value of \p toGuard
105 The function tries to load \p toGuard and to store it
106 to the HP slot repeatedly until the guard's value equals \p toGuard
108 template <typename T>
109 T protect( atomics::atomic<T> const& toGuard )
111 T pCur = toGuard.load(atomics::memory_order_relaxed);
114 pRet = assign( pCur );
115 pCur = toGuard.load(atomics::memory_order_acquire);
116 } while ( pRet != pCur );
120 /// Protects a converted pointer of type \p atomic<T*>
122 Return the value of \p toGuard
124 The function tries to load \p toGuard and to store result of \p f functor
125 to the HP slot repeatedly until the guard's value equals \p toGuard.
127 The function is useful for intrusive containers when \p toGuard is a node pointer
128 that should be converted to a pointer to the value type before protecting.
129 The parameter \p f of type Func is a functor that makes this conversion:
132 value_type * operator()( T * p );
135 Really, the result of <tt> f( toGuard.load() ) </tt> is assigned to the hazard pointer.
137 template <typename T, class Func>
138 T protect( atomics::atomic<T> const& toGuard, Func f )
140 T pCur = toGuard.load(atomics::memory_order_relaxed);
145 pCur = toGuard.load(atomics::memory_order_acquire);
146 } while ( pRet != pCur );
150 /// Store \p p to the guard
152 The function equals to a simple assignment the value \p p to guard, no loop is performed.
153 Can be used for a pointer that cannot be changed concurrently
155 template <typename T>
158 return base_class::operator =(p);
162 std::nullptr_t assign( std::nullptr_t )
164 return base_class::operator =(nullptr);
168 /// Copy from \p src guard to \p this guard
169 void copy( Guard const& src )
171 assign( src.get_native() );
174 /// Store marked pointer \p p to the guard
176 The function equals to a simple assignment of <tt>p.ptr()</tt>, no loop is performed.
177 Can be used for a marked pointer that cannot be changed concurrently.
179 template <typename T, int BITMASK>
180 T * assign( cds::details::marked_ptr<T, BITMASK> p )
182 return base_class::operator =( p.ptr() );
185 /// Clear value of the guard
191 /// Get the value currently protected
192 template <typename T>
195 return reinterpret_cast<T *>( get_native() );
198 /// Get native hazard pointer stored
199 guarded_pointer get_native() const
201 return base_class::get();
205 /// Array of Hazard Pointer guards
207 @headerfile cds/gc/hp.h
208 This class is a wrapper for hzp::AutoHPArray template.
209 Template parameter \p Count defines the size of HP array.
211 template <size_t Count>
212 class GuardArray: public hzp::AutoHPArray<Count>
215 typedef hzp::AutoHPArray<Count> base_class;
218 /// Rebind array for other size \p Count2
219 template <size_t Count2>
221 typedef GuardArray<Count2> other ; ///< rebinding result
226 GuardArray() ; // inline in hp_impl.h
228 /// Protects a pointer of type \p atomic<T*>
230 Return the value of \p toGuard
232 The function tries to load \p toGuard and to store it
233 to the slot \p nIndex repeatedly until the guard's value equals \p toGuard
235 template <typename T>
236 T protect(size_t nIndex, atomics::atomic<T> const& toGuard )
240 pRet = assign( nIndex, toGuard.load(atomics::memory_order_acquire) );
241 } while ( pRet != toGuard.load(atomics::memory_order_relaxed));
246 /// Protects a pointer of type \p atomic<T*>
248 Return the value of \p toGuard
250 The function tries to load \p toGuard and to store it
251 to the slot \p nIndex repeatedly until the guard's value equals \p toGuard
253 The function is useful for intrusive containers when \p toGuard is a node pointer
254 that should be converted to a pointer to the value type before guarding.
255 The parameter \p f of type Func is a functor that makes this conversion:
258 value_type * operator()( T * p );
261 Really, the result of <tt> f( toGuard.load() ) </tt> is assigned to the hazard pointer.
263 template <typename T, class Func>
264 T protect(size_t nIndex, atomics::atomic<T> const& toGuard, Func f )
268 assign( nIndex, f( pRet = toGuard.load(atomics::memory_order_acquire) ));
269 } while ( pRet != toGuard.load(atomics::memory_order_relaxed));
274 /// Store \p to the slot \p nIndex
276 The function equals to a simple assignment, no loop is performed.
278 template <typename T>
279 T * assign( size_t nIndex, T * p )
281 base_class::set(nIndex, p);
285 /// Store marked pointer \p p to the guard
287 The function equals to a simple assignment of <tt>p.ptr()</tt>, no loop is performed.
288 Can be used for a marked pointer that cannot be changed concurrently.
290 template <typename T, int BITMASK>
291 T * assign( size_t nIndex, cds::details::marked_ptr<T, BITMASK> p )
293 return assign( nIndex, p.ptr() );
296 /// Copy guarded value from \p src guard to slot at index \p nIndex
297 void copy( size_t nIndex, Guard const& src )
299 assign( nIndex, src.get_native() );
302 /// Copy guarded value from slot \p nSrcIndex to slot at index \p nDestIndex
303 void copy( size_t nDestIndex, size_t nSrcIndex )
305 assign( nDestIndex, get_native( nSrcIndex ));
308 /// Clear value of the slot \p nIndex
309 void clear( size_t nIndex)
311 base_class::clear( nIndex );
314 /// Get current value of slot \p nIndex
315 template <typename T>
316 T * get( size_t nIndex) const
318 return reinterpret_cast<T *>( get_native( nIndex ) );
321 /// Get native hazard pointer stored
322 guarded_pointer get_native( size_t nIndex ) const
324 return base_class::operator[](nIndex).get();
327 /// Capacity of the guard array
328 static CDS_CONSTEXPR size_t capacity()
335 /// Initializes hzp::GarbageCollector singleton
337 The constructor initializes GC singleton with passed parameters.
338 If GC instance is not exist then the function creates the instance.
339 Otherwise it does nothing.
341 The Michael's HP reclamation schema depends of three parameters:
342 - \p nHazardPtrCount - hazard pointer count per thread. Usually it is small number (up to 10) depending from
343 the data structure algorithms. By default, if \p nHazardPtrCount = 0, the function
344 uses maximum of the hazard pointer count for CDS library.
345 - \p nMaxThreadCount - max count of thread with using Hazard Pointer GC in your application. Default is 100.
346 - \p nMaxRetiredPtrCount - capacity of array of retired pointers for each thread. Must be greater than
347 <tt> nHazardPtrCount * nMaxThreadCount </tt>. Default is <tt>2 * nHazardPtrCount * nMaxThreadCount </tt>.
350 size_t nHazardPtrCount = 0, ///< Hazard pointer count per thread
351 size_t nMaxThreadCount = 0, ///< Max count of simultaneous working thread in your application
352 size_t nMaxRetiredPtrCount = 0, ///< Capacity of the array of retired objects for the thread
353 hzp::scan_type nScanType = hzp::inplace ///< Scan type (see \ref hzp::scan_type enum)
356 hzp::GarbageCollector::Construct(
364 /// Terminates GC singleton
366 The destructor calls \code hzp::GarbageCollector::Destruct( true ) \endcode
370 hzp::GarbageCollector::Destruct( true );
373 /// Checks if count of hazard pointer is no less than \p nCountNeeded
375 If \p bRaiseException is \p true (that is the default), the function raises an exception gc::too_few_hazard_pointers
376 if \p nCountNeeded is more than the count of hazard pointer per thread.
378 static bool check_available_guards( size_t nCountNeeded, bool bRaiseException = true )
380 if ( hzp::GarbageCollector::instance().getHazardPointerCount() < nCountNeeded ) {
381 if ( bRaiseException )
382 throw cds::gc::too_few_hazard_pointers();
388 /// Returns max Hazard Pointer count
389 size_t max_hazard_count() const
391 return hzp::GarbageCollector::instance().getHazardPointerCount();
394 /// Returns max count of thread
395 size_t max_thread_count() const
397 return hzp::GarbageCollector::instance().getMaxThreadCount();
400 /// Returns capacity of retired pointer array
401 size_t retired_array_capacity() const
403 return hzp::GarbageCollector::instance().getMaxRetiredPtrCount();
406 /// Retire pointer \p p with function \p pFunc
408 The function places pointer \p p to array of pointers ready for removing.
409 (so called retired pointer array). The pointer can be safely removed when no hazard pointer points to it.
410 Deleting the pointer is the function \p pFunc call.
412 template <typename T>
413 static void retire( T * p, void (* pFunc)(T *) ) ; // inline in hp_impl.h
415 /// Retire pointer \p p with functor of type \p Disposer
417 The function places pointer \p p to array of pointers ready for removing.
418 (so called retired pointer array). The pointer can be safely removed when no hazard pointer points to it.
420 Deleting the pointer is an invocation of some object of type \p Disposer; the interface of \p Disposer is:
422 template <typename T>
424 void operator()( T * p ) ; // disposing operator
427 Since the functor call can happen at any time after \p retire call, additional restrictions are imposed to \p Disposer type:
428 - it should be stateless functor
429 - it should be default-constructible
430 - the result of functor call with argument \p p should not depend on where the functor will be called.
433 Operator \p delete functor:
435 template <typename T>
437 void operator ()( T * p ) {
442 // How to call GC::retire method
445 // ... use p in lock-free manner
447 cds::gc::HP::retire<disposer>( p ) ; // place p to retired pointer array of HP GC
450 Functor based on \p std::allocator :
452 template <typename ALLOC = std::allocator<int> >
454 template <typename T>
455 void operator()( T * p ) {
456 typedef typename ALLOC::templare rebind<T>::other alloc_t;
459 a.deallocate( p, 1 );
464 template <class Disposer, typename T>
465 static void retire( T * p ) ; // inline in hp_impl.h
467 /// Get current scan strategy
468 hzp::scan_type getScanType() const
470 return hzp::GarbageCollector::instance().getScanType();
473 /// Set current scan strategy
475 hzp::scan_type nScanType ///< new scan strategy
478 hzp::GarbageCollector::instance().setScanType( nScanType );
481 /// Checks if Hazard Pointer GC is constructed and may be used
484 return hzp::GarbageCollector::isUsed();
488 /// Forced GC cycle call for current thread
490 Usually, this function should not be called directly.
492 static void scan() ; // inline in hp_impl.h
494 /// Synonym for \ref scan()
495 static void force_dispose()
500 }} // namespace cds::gc
502 #endif // #ifndef __CDS_GC_HP_DECL_H