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_URCU_GENERAL_THREADED_H
32 #define CDSLIB_URCU_GENERAL_THREADED_H
34 #include <cds/urcu/details/gpt.h>
36 namespace cds { namespace urcu {
38 /// User-space general-purpose RCU with special thread for deferred reclamation
39 /** @anchor cds_urcu_general_threaded_gc
41 This is a wrapper around \p general_threaded class.
44 - \p Buffer - lock-free MPSC (muliple producer/single consumer) queue.
45 Default is \p cds::container::VyukovMPSCCycleQueue< retired_ptr >
46 - \p Lock - mutex type, default is \p std::mutex
47 - \p DisposerThread - reclamation thread class, default is \p cds::urcu::dispose_thread
48 See \ref cds::urcu::dispose_thread for class interface.
49 - \p Backoff - back-off schema, default is \p cds::backoff::Default
53 #ifdef CDS_DOXGEN_INVOKED
54 class Buffer = cds::container::VyukovMPSCCycleQueue< epoch_retired_ptr >
55 ,class Lock = std::mutex
56 ,class DisposerThread = dispose_thread<Buffer>
57 ,class Backoff = cds::backoff::Default
65 class gc< general_threaded< Buffer, Lock, DisposerThread, Backoff > >: public details::gc_common
68 typedef general_threaded< Buffer, Lock, DisposerThread, Backoff > rcu_implementation ; ///< Wrapped URCU implementation
70 typedef typename rcu_implementation::rcu_tag rcu_tag ; ///< URCU tag
71 typedef typename rcu_implementation::thread_gc thread_gc ; ///< Thread-side RCU part
72 typedef typename rcu_implementation::scoped_lock scoped_lock ; ///< Access lock class
74 using details::gc_common::atomic_marked_ptr;
77 /// Creates URCU \p %general_threaded singleton.
78 gc( size_t nBufferCapacity = 256 )
80 rcu_implementation::Construct( nBufferCapacity );
83 /// Destroys URCU \p %general_threaded singleton
86 rcu_implementation::Destruct( true );
90 /// Waits to finish a grace period and calls disposing thread
92 After grace period finished the function gives new task to disposing thread.
93 Unlike \ref force_dispose the \p %synchronize function does not wait for
94 task ending. Only a "task ready" message is sent to disposing thread.
96 static void synchronize()
98 rcu_implementation::instance()->synchronize();
101 /// Retires pointer \p p by the disposer \p pFunc
103 If the buffer is full, \ref synchronize function is invoked.
105 template <typename T>
106 static void retire_ptr( T* p, free_retired_ptr_func pFunc )
108 retired_ptr rp( p, pFunc );
112 /// Retires pointer \p p using \p Disposer
114 If the buffer is full, \ref synchronize function is invoked.
116 template <typename Disposer, typename T>
117 static void retire_ptr( T* p )
119 retire_ptr( p, cds::details::static_functor<Disposer, T>::call );
122 /// Retires pointer \p p of type \ref cds_urcu_retired_ptr "retired_ptr"
124 If the buffer is full, \ref synchronize function is invoked.
126 static void retire_ptr( retired_ptr& p )
128 rcu_implementation::instance()->retire_ptr(p);
131 /// Frees chain [ \p itFirst, \p itLast) in one synchronization cycle
132 template <typename ForwardIterator>
133 static void batch_retire( ForwardIterator itFirst, ForwardIterator itLast )
135 rcu_implementation::instance()->batch_retire( itFirst, itLast );
138 /// Retires the pointer chain until \p Func returns \p nullptr retired pointer
139 template <typename Func>
140 static void batch_retire( Func e )
142 rcu_implementation::instance()->batch_retire( e );
145 /// Acquires access lock (so called RCU reader-side lock)
147 For safety reasons, it is better to use \ref scoped_lock class for locking/unlocking
149 static void access_lock()
151 thread_gc::access_lock();
154 /// Releases access lock (so called RCU reader-side lock)
156 For safety reasons, it is better to use \ref scoped_lock class for locking/unlocking
158 static void access_unlock()
160 thread_gc::access_unlock();
163 /// Checks if the thread is inside read-side critical section (i.e. the lock is acquired)
165 Usually, this function is used internally to be convinced
166 that subsequent remove action is not lead to a deadlock.
168 static bool is_locked()
170 return thread_gc::is_locked();
173 /// Returns the threshold of internal buffer
174 static size_t capacity()
176 return rcu_implementation::instance()->capacity();
179 /// Forces retired object removal (synchronous version of \ref synchronize)
181 The function calls \ref synchronize and waits until reclamation thread
182 frees retired objects.
184 static void force_dispose()
186 rcu_implementation::instance()->force_dispose();
192 class gc< general_threaded_stripped >: public gc< general_threaded<>>
196 }} // namespace cds::urcu
198 #endif // #ifndef CDSLIB_URCU_GENERAL_THREADED_H