Removed redundant spaces
[libcds.git] / cds / gc / impl / hp_impl.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #ifndef CDSLIB_GC_IMPL_HP_IMPL_H
32 #define CDSLIB_GC_IMPL_HP_IMPL_H
33
34 #include <cds/threading/model.h>
35 #include <cds/details/static_functor.h>
36
37 //@cond
38 namespace cds { namespace gc {
39
40     namespace hp {
41         static inline ThreadGC& get_thread_gc()
42         {
43             return cds::threading::getGC<HP>();
44         }
45     } // namespace hp
46
47     inline HP::thread_gc::thread_gc(
48         bool    bPersistent
49         )
50         : m_bPersistent( bPersistent )
51     {
52         if ( !threading::Manager::isThreadAttached())
53             threading::Manager::attachThread();
54     }
55
56     inline HP::thread_gc::~thread_gc()
57     {
58         if ( !m_bPersistent )
59             cds::threading::Manager::detachThread();
60     }
61
62     inline /*static*/ cds::gc::hp::details::hp_guard* HP::thread_gc::alloc_guard()
63     {
64         return hp::get_thread_gc().allocGuard();
65     }
66
67     inline /*static*/ void HP::thread_gc::free_guard( cds::gc::hp::details::hp_guard* g )
68     {
69         hp::get_thread_gc().freeGuard( g );
70     }
71
72     inline HP::Guard::Guard()
73         : m_guard( hp::get_thread_gc().allocGuard())
74     {
75         if ( !m_guard )
76             throw too_many_hazard_ptr_exception();
77     }
78
79     template <typename T>
80     inline T * HP::Guard::assign( T * p )
81     {
82         assert( m_guard != nullptr );
83
84         T * pp = ( *m_guard = p );
85         hp::get_thread_gc().sync();
86         return pp;
87     }
88
89     inline void HP::Guard::link()
90     {
91         if ( !m_guard ) {
92             m_guard = hp::get_thread_gc().allocGuard();
93             if ( !m_guard )
94                 throw too_many_hazard_ptr_exception();
95         }
96     }
97
98     inline void HP::Guard::unlink()
99     {
100         if ( m_guard ) {
101             hp::get_thread_gc().freeGuard( m_guard );
102             m_guard = nullptr;
103         }
104     }
105
106     template <size_t Count>
107     inline HP::GuardArray<Count>::GuardArray()
108     {
109         if ( hp::get_thread_gc().allocGuard( m_arr ) != Count )
110             throw too_many_hazard_ptr_exception();
111     }
112
113     template <size_t Count>
114     inline HP::GuardArray<Count>::~GuardArray()
115     {
116         hp::get_thread_gc().freeGuard( m_arr );
117     }
118
119     template <size_t Count>
120     template <typename T>
121     inline T * HP::GuardArray<Count>::assign( size_t nIndex, T* p )
122     {
123         assert( nIndex < capacity());
124
125         m_arr.set(nIndex, p);
126         hp::get_thread_gc().sync();
127         return p;
128     }
129
130     template <typename T>
131     inline void HP::retire( T * p, void (* pFunc)(T *))
132     {
133         cds::threading::getGC<HP>().retirePtr( p, pFunc );
134     }
135
136     template <class Disposer, typename T>
137     inline void HP::retire( T * p )
138     {
139         hp::get_thread_gc().retirePtr( p, cds::details::static_functor<Disposer, T>::call );
140     }
141
142     inline void HP::scan()
143     {
144         hp::get_thread_gc().scan();
145     }
146
147 }} // namespace cds::gc
148 //@endcond
149
150 #endif // #ifndef CDSLIB_GC_IMPL_HP_IMPL_H