3 #ifndef _CDS_URCU_DETAILS_GPB_H
4 #define _CDS_URCU_DETAILS_GPB_H
6 #include <cds/urcu/details/gp.h>
7 #include <cds/algo/backoff_strategy.h>
8 #include <cds/container/vyukov_mpmc_cycle_queue.h>
10 #include <cds/details/std/mutex.h>
12 namespace cds { namespace urcu {
14 /// User-space general-purpose RCU with deferred (buffered) reclamation
16 @headerfile cds/urcu/general_buffered.h
18 This URCU implementation contains an internal buffer where retired objects are
19 accumulated. When the buffer becomes full, the RCU \p synchronize function is called
20 that waits until all reader/updater threads end up their read-side critical sections,
21 i.e. until the RCU quiescent state will come. After that the buffer and all retired objects are freed.
22 This synchronization cycle may be called in any thread that calls \p retire_ptr function.
24 The \p Buffer contains items of \ref cds_urcu_retired_ptr "retired_ptr" type and it should support a queue interface with
26 - <tt> bool push( retired_ptr& p ) </tt> - places the retired pointer \p p into queue. If the function
27 returns \p false it means that the buffer is full and RCU synchronization cycle must be processed.
28 - <tt>bool pop( retired_ptr& p ) </tt> - pops queue's head item into \p p parameter; if the queue is empty
29 this function must return \p false
30 - <tt>size_t size()</tt> - returns queue's item count.
32 The buffer is considered as full if \p push returns \p false or the buffer size reaches the RCU threshold.
34 There is a wrapper \ref cds_urcu_general_buffered_gc "gc<general_buffered>" for \p %general_buffered class
35 that provides unified RCU interface. You should use this wrapper class instead \p %general_buffered
38 - \p Buffer - buffer type. Default is cds::container::VyukovMPMCCycleQueue
39 - \p Lock - mutex type, default is \p std::mutex
40 - \p Backoff - back-off schema, default is cds::backoff::Default
43 class Buffer = cds::container::VyukovMPMCCycleQueue<
45 ,cds::opt::buffer< cds::opt::v::dynamic_buffer< epoch_retired_ptr > >
47 ,class Lock = cds_std::mutex
48 ,class Backoff = cds::backoff::Default
50 class general_buffered: public details::gp_singleton< general_buffered_tag >
53 typedef details::gp_singleton< general_buffered_tag > base_class;
56 typedef general_buffered_tag rcu_tag ; ///< RCU tag
57 typedef Buffer buffer_type ; ///< Buffer type
58 typedef Lock lock_type ; ///< Lock type
59 typedef Backoff back_off ; ///< Back-off type
61 typedef base_class::thread_gc thread_gc ; ///< Thread-side RCU part
62 typedef typename thread_gc::scoped_lock scoped_lock ; ///< Access lock class
64 static bool const c_bBuffered = true ; ///< This RCU buffers disposed elements
68 typedef details::gp_singleton_instance< rcu_tag > singleton_ptr;
74 CDS_ATOMIC::atomic<uint64_t> m_nCurEpoch;
76 size_t const m_nCapacity;
80 /// Returns singleton instance
81 static general_buffered * instance()
83 return static_cast<general_buffered *>( base_class::instance() );
85 /// Checks if the singleton is created and ready to use
88 return singleton_ptr::s_pRCU != nullptr;
93 general_buffered( size_t nBufferCapacity )
94 : m_Buffer( nBufferCapacity )
96 , m_nCapacity( nBufferCapacity )
101 clear_buffer( (uint64_t) -1 );
107 base_class::flip_and_wait( bkoff );
110 void clear_buffer( uint64_t nEpoch )
113 while ( m_Buffer.pop( p )) {
114 if ( p.m_nEpoch <= nEpoch )
123 // Return: true - synchronize has been called, false - otherwise
124 bool push_buffer( epoch_retired_ptr& ep )
126 bool bPushed = m_Buffer.push( ep );
127 if ( !bPushed || m_Buffer.size() >= capacity() ) {
138 /// Creates singleton object
140 The \p nBufferCapacity parameter defines RCU threshold.
142 static void Construct( size_t nBufferCapacity = 256 )
144 if ( !singleton_ptr::s_pRCU )
145 singleton_ptr::s_pRCU = new general_buffered( nBufferCapacity );
148 /// Destroys singleton object
149 static void Destruct( bool bDetachAll = false )
152 instance()->clear_buffer( (uint64_t) -1 );
154 instance()->m_ThreadList.detach_all();
156 singleton_ptr::s_pRCU = nullptr;
161 /// Retire \p p pointer
163 The method pushes \p p pointer to internal buffer.
164 When the buffer becomes full \ref synchronize function is called
165 to wait for the end of grace period and then to free all pointers from the buffer.
167 virtual void retire_ptr( retired_ptr& p )
170 epoch_retired_ptr ep( p, m_nCurEpoch.load( CDS_ATOMIC::memory_order_relaxed ));
175 /// Retires the pointer chain [\p itFirst, \p itLast)
176 template <typename ForwardIterator>
177 void batch_retire( ForwardIterator itFirst, ForwardIterator itLast )
179 uint64_t nEpoch = m_nCurEpoch.load( CDS_ATOMIC::memory_order_relaxed );
180 while ( itFirst != itLast ) {
181 epoch_retired_ptr ep( *itFirst, nEpoch );
187 /// Wait to finish a grace period and then clear the buffer
190 epoch_retired_ptr ep( retired_ptr(), m_nCurEpoch.load( CDS_ATOMIC::memory_order_relaxed ));
195 bool synchronize( epoch_retired_ptr& ep )
198 CDS_ATOMIC::atomic_thread_fence( CDS_ATOMIC::memory_order_acquire );
200 cds::lock::scoped_lock<lock_type> sl( m_Lock );
201 if ( ep.m_p && m_Buffer.push( ep ) )
203 nEpoch = m_nCurEpoch.fetch_add( 1, CDS_ATOMIC::memory_order_relaxed );
207 clear_buffer( nEpoch );
208 CDS_ATOMIC::atomic_thread_fence( CDS_ATOMIC::memory_order_release );
213 /// Returns internal buffer capacity
214 size_t capacity() const
220 }} // namespace cds::urcu
222 #endif // #ifndef _CDS_URCU_DETAILS_GPB_H