3 #ifndef _CDS_URCU_DETAILS_SIG_BUFFERED_H
4 #define _CDS_URCU_DETAILS_SIG_BUFFERED_H
6 #include <cds/urcu/details/sh.h>
7 #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
9 #include <cds/backoff_strategy.h>
10 #include <cds/container/vyukov_mpmc_cycle_queue.h>
12 #include <cds/details/std/mutex.h>
14 namespace cds { namespace urcu {
16 /// User-space signal-handled RCU with deferred (buffered) reclamation
18 @headerfile cds/urcu/signal_buffered.h
20 This URCU implementation contains an internal buffer where retired objects are
21 accumulated. When the buffer becomes full, the RCU \p synchronize function is called
22 that waits until all reader/updater threads end up their read-side critical sections,
23 i.e. until the RCU quiescent state will come. After that the buffer and all retired objects are freed.
24 This synchronization cycle may be called in any thread that calls \p retire_ptr function.
26 The \p Buffer contains items of \ref cds_urcu_retired_ptr "retired_ptr" type and it should support a queue interface with
28 - <tt> bool push( retired_ptr& p ) </tt> - places the retired pointer \p p into queue. If the function
29 returns \p false it means that the buffer is full and RCU synchronization cycle must be processed.
30 - <tt>bool pop( retired_ptr& p ) </tt> - pops queue's head item into \p p parameter; if the queue is empty
31 this function must return \p false
32 - <tt>size_t size()</tt> - returns queue's item count.
34 The buffer is considered as full if \p push returns \p false or the buffer size reaches the RCU threshold.
36 There is a wrapper \ref cds_urcu_signal_buffered_gc "gc<signal_buffered>" for \p %signal_buffered class
37 that provides unified RCU interface. You should use this wrapper class instead \p %signal_buffered
40 - \p Buffer - buffer type. Default is cds::container::VyukovMPMCCycleQueue
41 - \p Lock - mutex type, default is \p std::mutex
42 - \p Backoff - back-off schema, default is cds::backoff::Default
45 class Buffer = cds::container::VyukovMPMCCycleQueue<
47 ,cds::opt::buffer< cds::opt::v::dynamic_buffer< epoch_retired_ptr > >
49 ,class Lock = cds_std::mutex
50 ,class Backoff = cds::backoff::Default
52 class signal_buffered: public details::sh_singleton< signal_buffered_tag >
55 typedef details::sh_singleton< signal_buffered_tag > base_class;
58 typedef signal_buffered_tag rcu_tag ; ///< RCU tag
59 typedef Buffer buffer_type ; ///< Buffer type
60 typedef Lock lock_type ; ///< Lock type
61 typedef Backoff back_off ; ///< Back-off type
63 typedef base_class::thread_gc thread_gc ; ///< Thread-side RCU part
64 typedef typename thread_gc::scoped_lock scoped_lock ; ///< Access lock class
66 static bool const c_bBuffered = true ; ///< This RCU buffers disposed elements
70 typedef details::sh_singleton_instance< rcu_tag > singleton_ptr;
76 CDS_ATOMIC::atomic<uint64_t> m_nCurEpoch;
78 size_t const m_nCapacity;
82 /// Returns singleton instance
83 static signal_buffered * instance()
85 return static_cast<signal_buffered *>( base_class::instance() );
87 /// Checks if the singleton is created and ready to use
90 return singleton_ptr::s_pRCU != nullptr;
95 signal_buffered( size_t nBufferCapacity, int nSignal = SIGUSR1 )
96 : base_class( nSignal )
97 , m_Buffer( nBufferCapacity )
99 , m_nCapacity( nBufferCapacity )
104 clear_buffer( (uint64_t) -1 );
107 void clear_buffer( uint64_t nEpoch )
110 while ( m_Buffer.pop( p )) {
111 if ( p.m_nEpoch <= nEpoch )
120 bool push_buffer( epoch_retired_ptr& ep )
122 bool bPushed = m_Buffer.push( ep );
123 if ( !bPushed || m_Buffer.size() >= capacity() ) {
134 /// Creates singleton object
136 The \p nBufferCapacity parameter defines RCU threshold.
138 The \p nSignal parameter defines a signal number stated for RCU, default is \p SIGUSR1
140 static void Construct( size_t nBufferCapacity = 256, int nSignal = SIGUSR1 )
142 if ( !singleton_ptr::s_pRCU )
143 singleton_ptr::s_pRCU = new signal_buffered( nBufferCapacity, nSignal );
146 /// Destroys singleton object
147 static void Destruct( bool bDetachAll = false )
150 instance()->clear_buffer( (uint64_t) -1 );
152 instance()->m_ThreadList.detach_all();
154 singleton_ptr::s_pRCU = nullptr;
159 /// Retire \p p pointer
161 The method pushes \p p pointer to internal buffer.
162 When the buffer becomes full \ref synchronize function is called
163 to wait for the end of grace period and then to free all pointers from the buffer.
165 virtual void retire_ptr( retired_ptr& p )
168 epoch_retired_ptr ep( p, m_nCurEpoch.load( CDS_ATOMIC::memory_order_relaxed ));
173 /// Retires the pointer chain [\p itFirst, \p itLast)
174 template <typename ForwardIterator>
175 void batch_retire( ForwardIterator itFirst, ForwardIterator itLast )
177 uint64_t nEpoch = m_nCurEpoch.load( CDS_ATOMIC::memory_order_relaxed );
178 while ( itFirst != itLast ) {
179 epoch_retired_ptr ep( *itFirst, nEpoch );
185 /// Wait to finish a grace period and then clear the buffer
188 epoch_retired_ptr ep( retired_ptr(), m_nCurEpoch.load( CDS_ATOMIC::memory_order_relaxed ));
193 bool synchronize( epoch_retired_ptr& ep )
196 CDS_ATOMIC::atomic_thread_fence( CDS_ATOMIC::memory_order_acquire );
198 cds::lock::scoped_lock<lock_type> sl( m_Lock );
199 if ( ep.m_p && m_Buffer.push( ep ) && m_Buffer.size() < capacity())
201 nEpoch = m_nCurEpoch.fetch_add( 1, CDS_ATOMIC::memory_order_relaxed );
204 base_class::force_membar_all_threads( bkOff );
205 base_class::switch_next_epoch();
207 base_class::wait_for_quiescent_state( bkOff );
208 base_class::switch_next_epoch();
210 base_class::wait_for_quiescent_state( bkOff );
211 base_class::force_membar_all_threads( bkOff );
214 clear_buffer( nEpoch );
219 /// Returns the threshold of internal buffer
220 size_t capacity() const
225 /// Returns the signal number stated for RCU
226 int signal_no() const
228 return base_class::signal_no();
232 }} // namespace cds::urcu
234 #endif // #ifdef CDS_URCU_SIGNAL_HANDLING_ENABLED
235 #endif // #ifndef _CDS_URCU_DETAILS_SIG_BUFFERED_H