Removed trailing spaces
[libcds.git] / cds / urcu / general_instant.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_URCU_GENERAL_INSTANT_H
32 #define CDSLIB_URCU_GENERAL_INSTANT_H
33
34 #include <cds/urcu/details/gpi.h>
35
36 namespace cds { namespace urcu {
37
38     /// User-space general-purpose RCU with immediate reclamation
39     /** @anchor cds_urcu_general_instant_gc
40
41         This is a wrapper around \p general_instant class.
42
43         Template arguments:
44         - \p Lock - mutex type, default is \p std::mutex
45         - \p Backoff - back-off schema, default is \p cds::backoff::Default
46     */
47     template <
48 #ifdef CDS_DOXGEN_INVOKED
49         class Lock = std::mutex
50        ,class Backoff = cds::backoff::Default
51 #else
52         class Lock
53        ,class Backoff
54 #endif
55     >
56     class gc< general_instant< Lock, Backoff > >: public details::gc_common
57     {
58     public:
59         typedef general_instant< Lock, Backoff >        rcu_implementation   ;   ///< Wrapped URCU implementation
60
61         typedef typename rcu_implementation::rcu_tag     rcu_tag     ;   ///< URCU tag
62         typedef typename rcu_implementation::thread_gc   thread_gc   ;   ///< Thread-side RCU part
63         typedef typename rcu_implementation::scoped_lock scoped_lock ;   ///< Access lock class
64
65         using details::gc_common::atomic_marked_ptr;
66
67     public:
68         /// Creates URCU \p %general_instant singleton
69         gc()
70         {
71             rcu_implementation::Construct();
72         }
73
74         /// Destroys URCU \p %general_instant singleton
75         ~gc()
76         {
77             rcu_implementation::Destruct( true );
78         }
79
80     public:
81         /// Waits to finish a grace period
82         static void synchronize()
83         {
84             rcu_implementation::instance()->synchronize();
85         }
86
87         /// Frees the pointer \p p invoking \p pFunc after end of grace period
88         /**
89             The function calls \ref synchronize to wait for end of grace period
90             and then evaluates disposing expression <tt>pFunc( p )</tt>
91         */
92         template <typename T>
93         static void retire_ptr( T * p, void (* pFunc)(T *) )
94         {
95             retired_ptr rp( reinterpret_cast<void *>( p ), reinterpret_cast<free_retired_ptr_func>( pFunc ) );
96             retire_ptr( rp );
97         }
98
99         /// Frees the pointer \p using \p Disposer after end of grace period
100         /**
101             The function calls \ref synchronize to wait for end of grace period
102             and then evaluates disposing expression <tt>Disposer()( p )</tt>
103         */
104         template <typename Disposer, typename T>
105         static void retire_ptr( T * p )
106         {
107             retire_ptr( p, cds::details::static_functor<Disposer, T>::call );
108         }
109
110         /// Frees the pointer \p after the end of grace period
111         /**
112             The function calls \ref synchronize to wait for end of grace period
113             and then evaluates disposing expression <tt>p.m_funcFree( p.m_p )</tt>
114         */
115         static void retire_ptr( retired_ptr& p )
116         {
117             rcu_implementation::instance()->retire_ptr(p);
118         }
119
120         /// Frees chain [ \p itFirst, \p itLast) in one synchronization cycle
121         template <typename ForwardIterator>
122         static void batch_retire( ForwardIterator itFirst, ForwardIterator itLast )
123         {
124             rcu_implementation::instance()->batch_retire( itFirst, itLast );
125         }
126
127         /// Retires the pointer chain until \p Func returns \p nullptr retired pointer
128         template <typename Func>
129         static void batch_retire( Func e )
130         {
131             rcu_implementation::instance()->batch_retire( e );
132         }
133
134         /// Acquires access lock (so called RCU reader-side lock)
135         /**
136             For safety reasons, it is better to use \ref scoped_lock class for locking/unlocking
137         */
138         static void access_lock()
139         {
140             thread_gc::access_lock();
141         }
142
143         /// Releases access lock (so called RCU reader-side lock)
144         /**
145             For safety reasons, it is better to use \ref scoped_lock class for locking/unlocking
146         */
147         static void access_unlock()
148         {
149             thread_gc::access_unlock();
150         }
151
152         /// Checks if the thread is inside read-side critical section (i.e. the lock is acquired)
153         /**
154             Usually, this function is used internally to be convinced
155             that subsequent remove action is not lead to a deadlock.
156         */
157         static bool is_locked()
158         {
159             return thread_gc::is_locked();
160         }
161
162         /// Forced GC cycle call.
163         /**
164             This method does nothing and is introduced only for uniformity with other
165             garbage collectors.
166         */
167         static void force_dispose()
168         {}
169     };
170
171     //@cond
172     template<>
173     class gc< general_instant_stripped >: public gc< general_instant<>>
174     {};
175     //@endcond
176
177 }} // namespace cds::urcu
178
179 #endif // #ifndef CDSLIB_URCU_GENERAL_INSTANT_H