2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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_IMPL_HP_IMPL_H
32 #define CDSLIB_GC_IMPL_HP_IMPL_H
34 #include <cds/threading/model.h>
35 #include <cds/details/static_functor.h>
38 namespace cds { namespace gc {
41 static inline ThreadGC& get_thread_gc()
43 return cds::threading::getGC<HP>();
47 inline HP::thread_gc::thread_gc(
50 : m_bPersistent( bPersistent )
52 if ( !threading::Manager::isThreadAttached() )
53 threading::Manager::attachThread();
56 inline HP::thread_gc::~thread_gc()
59 cds::threading::Manager::detachThread();
62 inline /*static*/ cds::gc::hp::details::hp_guard* HP::thread_gc::alloc_guard()
64 return hp::get_thread_gc().allocGuard();
67 inline /*static*/ void HP::thread_gc::free_guard( cds::gc::hp::details::hp_guard* g )
69 hp::get_thread_gc().freeGuard( g );
72 inline HP::Guard::Guard()
73 : m_guard( hp::get_thread_gc().allocGuard())
76 throw too_many_hazard_ptr_exception();
80 inline T * HP::Guard::assign( T * p )
82 assert( m_guard != nullptr );
84 T * pp = ( *m_guard = p );
85 hp::get_thread_gc().sync();
89 inline void HP::Guard::link()
92 m_guard = hp::get_thread_gc().allocGuard();
94 throw too_many_hazard_ptr_exception();
98 inline void HP::Guard::unlink()
101 hp::get_thread_gc().freeGuard( m_guard );
106 template <size_t Count>
107 inline HP::GuardArray<Count>::GuardArray()
109 if ( hp::get_thread_gc().allocGuard( m_arr ) != Count )
110 throw too_many_hazard_ptr_exception();
113 template <size_t Count>
114 inline HP::GuardArray<Count>::~GuardArray()
116 hp::get_thread_gc().freeGuard( m_arr );
119 template <size_t Count>
120 template <typename T>
121 inline T * HP::GuardArray<Count>::assign( size_t nIndex, T* p )
123 assert( nIndex < capacity() );
125 m_arr.set(nIndex, p);
126 hp::get_thread_gc().sync();
130 template <typename T>
131 inline void HP::retire( T * p, void (* pFunc)(T *) )
133 cds::threading::getGC<HP>().retirePtr( p, pFunc );
136 template <class Disposer, typename T>
137 inline void HP::retire( T * p )
139 hp::get_thread_gc().retirePtr( p, cds::details::static_functor<Disposer, T>::call );
142 inline void HP::scan()
144 hp::get_thread_gc().scan();
147 }} // namespace cds::gc
150 #endif // #ifndef CDSLIB_GC_IMPL_HP_IMPL_H