3 #ifndef CDSLIB_CONTAINER_RWQUEUE_H
4 #define CDSLIB_CONTAINER_RWQUEUE_H
6 #include <mutex> // unique_lock
7 #include <cds/sync/spinlock.h>
8 #include <cds/opt/options.h>
9 #include <cds/details/allocator.h>
11 namespace cds { namespace container {
12 /// RWQueue related definitions
13 /** @ingroup cds_nonintrusive_helper
16 /// RWQueue default type traits
20 typedef cds::sync::spin lock_type;
23 typedef CDS_DEFAULT_ALLOCATOR allocator;
25 /// Item counting feature; by default, disabled. Use \p cds::atomicity::item_counter to enable item counting
26 typedef cds::atomicity::empty_item_counter item_counter;
28 /// Padding for internal critical atomic data. Default is \p opt::cache_line_padding
29 enum { padding = opt::cache_line_padding };
32 /// Metafunction converting option list to \p rwqueue::traits
34 Supported \p Options are:
35 - opt::lock_type - lock policy, default is \p cds::sync::spin. Any type satisfied \p Mutex C++ concept may be used.
36 - opt::allocator - allocator (like \p std::allocator) used for allocating queue nodes. Default is \ref CDS_DEFAULT_ALLOCATOR
37 - opt::item_counter - the type of item counting feature. Default is \p cds::atomicity::empty_item_counter (item counting disabled)
38 To enable item counting use \p cds::atomicity::item_counter.
39 - \p opt::padding - padding for internal critical data. Default is \p opt::cache_line_padding
41 Example: declare mutex-based \p %RWQueue with item counting
43 typedef cds::container::RWQueue< Foo,
44 typename cds::container::rwqueue::make_traits<
45 cds::opt::item_counter< cds::atomicity::item_counter >,
46 cds::opt::lock_type< std::mutex >
51 template <typename... Options>
53 # ifdef CDS_DOXYGEN_INVOKED
54 typedef implementation_defined type; ///< Metafunction result
56 typedef typename cds::opt::make_options<
57 typename cds::opt::find_type_traits< traits, Options... >::type
63 } // namespace rwqueue
65 /// Michael & Scott blocking queue with fine-grained synchronization schema
66 /** @ingroup cds_nonintrusive_queue
67 The queue has two different locks: one for reading and one for writing.
68 Therefore, one writer and one reader can simultaneously access to the queue.
69 The queue does not require any garbage collector.
72 - [1998] Maged Michael, Michael Scott "Simple, fast, and practical non-blocking
73 and blocking concurrent queue algorithms"
75 <b>Template arguments</b>
76 - \p T - value type to be stored in the queue
77 - \p Traits - queue traits, default is \p rwqueue::traits. You can use \p rwqueue::make_traits
78 metafunction to make your traits or just derive your traits from \p %rwqueue::traits:
80 struct myTraits: public cds::container::rwqueue::traits {
81 typedef cds::atomicity::item_counter item_counter;
83 typedef cds::container::RWQueue< Foo, myTraits > myQueue;
85 // Equivalent make_traits example:
86 typedef cds::container::RWQueue< Foo,
87 typename cds::container::rwqueue::make_traits<
88 cds::opt::item_counter< cds::atomicity::item_counter >
93 template <typename T, typename Traits = rwqueue::traits >
97 /// Rebind template arguments
98 template <typename T2, typename Traits2>
100 typedef RWQueue< T2, Traits2 > other ; ///< Rebinding result
104 typedef T value_type; ///< Type of value to be stored in the queue
105 typedef Traits traits; ///< Queue traits
107 typedef typename traits::lock_type lock_type; ///< Locking primitive
108 typedef typename traits::item_counter item_counter; ///< Item counting policy used
115 atomics::atomic< node_type *> m_pNext; ///< Pointer to the next node in the queue
116 value_type m_value; ///< Value stored in the node
118 node_type( value_type const& v )
127 template <typename... Args>
128 node_type( Args&&... args )
130 , m_value( std::forward<Args>(args)...)
136 typedef typename traits::allocator::template rebind<node_type>::other allocator_type; ///< Allocator type used for allocate/deallocate the queue nodes
140 typedef std::unique_lock<lock_type> scoped_lock;
141 typedef cds::details::Allocator< node_type, allocator_type > node_allocator;
144 mutable lock_type lock;
149 typename opt::details::apply_padding< head_type, traits::padding >::padding_type pad_;
152 item_counter m_ItemCounter;
157 static node_type * alloc_node()
159 return node_allocator().New();
162 static node_type * alloc_node( T const& data )
164 return node_allocator().New( data );
167 template <typename... Args>
168 static node_type * alloc_node_move( Args&&... args )
170 return node_allocator().MoveNew( std::forward<Args>( args )... );
173 static void free_node( node_type * pNode )
175 node_allocator().Delete( pNode );
178 bool enqueue_node( node_type * p )
180 assert( p != nullptr );
182 scoped_lock lock( m_Tail.lock );
183 m_Tail.ptr->m_pNext.store( p, atomics::memory_order_release );
190 struct node_disposer {
191 void operator()( node_type * pNode )
196 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
200 /// Makes empty queue
203 node_type * pNode = alloc_node();
208 /// Destructor clears queue
212 assert( m_Head.ptr == m_Tail.ptr );
213 free_node( m_Head.ptr );
216 /// Enqueues \p data. Always return \a true
217 bool enqueue( value_type const& data )
219 scoped_node_ptr p( alloc_node( data ));
220 if ( enqueue_node( p.get() )) {
227 /// Enqueues \p data to the queue using a functor
229 \p Func is a functor called to create node.
230 The functor \p f takes one argument - a reference to a new node of type \ref value_type :
232 cds::container::RWQueue< cds::gc::HP, Foo > myQueue;
234 myQueue.enqueue_with( [&bar]( Foo& dest ) { dest = bar; } );
237 template <typename Func>
238 bool enqueue_with( Func f )
240 scoped_node_ptr p( alloc_node() );
242 if ( enqueue_node( p.get() )) {
249 /// Enqueues data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
250 template <typename... Args>
251 bool emplace( Args&&... args )
253 scoped_node_ptr p( alloc_node_move( std::forward<Args>(args)... ));
254 if ( enqueue_node( p.get() )) {
261 /// Synonym for \p enqueue() function
262 bool push( value_type const& val )
264 return enqueue( val );
267 /// Synonym for \p enqueue_with() function
268 template <typename Func>
269 bool push_with( Func f )
271 return enqueue_with( f );
274 /// Dequeues a value to \p dest.
276 If queue is empty returns \a false, \p dest can be corrupted.
277 If queue is not empty returns \a true, \p dest contains the value dequeued
279 bool dequeue( value_type& dest )
281 return dequeue_with( [&dest]( value_type& src ) { dest = src; } );
284 /// Dequeues a value using a functor
286 \p Func is a functor called to copy dequeued value.
287 The functor takes one argument - a reference to removed node:
289 cds:container::RWQueue< cds::gc::HP, Foo > myQueue;
291 myQueue.dequeue_with( [&bar]( Foo& src ) { bar = std::move( src );});
293 The functor is called only if the queue is not empty.
295 template <typename Func>
296 bool dequeue_with( Func f )
300 scoped_lock lock( m_Head.lock );
302 node_type * pNewHead = pNode->m_pNext.load( atomics::memory_order_acquire );
303 if ( pNewHead == nullptr )
305 f( pNewHead->m_value );
306 m_Head.ptr = pNewHead;
313 /// Synonym for \p dequeue() function
314 bool pop( value_type& dest )
316 return dequeue( dest );
319 /// Synonym for \p dequeue_with() function
320 template <typename Func>
321 bool pop_with( Func f )
323 return dequeue_with( f );
326 /// Checks if queue is empty
329 scoped_lock lock( m_Head.lock );
330 return m_Head.ptr->m_pNext.load( atomics::memory_order_relaxed ) == nullptr;
336 scoped_lock lockR( m_Head.lock );
337 scoped_lock lockW( m_Tail.lock );
338 while ( m_Head.ptr->m_pNext.load( atomics::memory_order_relaxed ) != nullptr ) {
339 node_type * pHead = m_Head.ptr;
340 m_Head.ptr = m_Head.ptr->m_pNext.load( atomics::memory_order_relaxed );
345 /// Returns queue's item count
347 The value returned depends on \p rwqueue::traits::item_counter. For \p atomicity::empty_item_counter,
348 this function always returns 0.
350 @note Even if you use real item counter and it returns 0, this fact is not mean that the queue
351 is empty. To check queue emptyness use \p empty() method.
355 return m_ItemCounter.value();
359 /// The class has no internal statistics. For test consistency only
360 nullptr_t statistics() const
367 }} // namespace cds::container
369 #endif // #ifndef CDSLIB_CONTAINER_RWQUEUE_H