2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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_CONTAINER_MOIR_QUEUE_H
32 #define CDSLIB_CONTAINER_MOIR_QUEUE_H
35 #include <cds/container/msqueue.h>
36 #include <cds/intrusive/moir_queue.h>
38 namespace cds { namespace container {
42 template <typename GC, typename T, typename Traits>
43 struct make_moir_queue: public cds::container::details::make_msqueue< GC, T, Traits >
45 typedef cds::container::details::make_msqueue< GC, T, Traits > base_class;
46 typedef cds::intrusive::MoirQueue< GC, typename base_class::node_type, typename base_class::intrusive_traits > type;
51 /// A variation of Michael & Scott's lock-free queue
52 /** @ingroup cds_nonintrusive_queue
53 It is non-intrusive version of \p cds::intrusive::MoirQueue.
56 - \p GC - garbage collector type: \p gc::HP, \p gc::DHP
57 - \p T - a type stored in the queue.
58 - \p Traits - queue traits, default is \p msqueue::traits. You can use \p msqueue::make_traits
59 metafunction to make your traits or just derive your traits from \p %msqueue::traits:
61 struct myTraits: public cds::container::msqueue::traits {
62 typedef cds::intrusive::msqueue::stat<> stat;
63 typedef cds::atomicity::item_counter item_counter;
65 typedef cds::container::MoirQueue< cds::gc::HP, Foo, myTraits > myQueue;
67 // Equivalent make_traits example:
68 typedef cds::container::MoirQueue< cds::gc::HP, Foo,
69 typename cds::container::msqueue::make_traits<
70 cds::opt::stat< cds::container::msqueue::stat<> >,
71 cds::opt::item_counter< cds::atomicity::item_counter >
76 template <typename GC, typename T, typename Traits = cds::container::msqueue::traits >
78 #ifdef CDS_DOXYGEN_INVOKED
79 private intrusive::MoirQueue< GC, intrusive::msqueue::node< T >, Traits >
81 private details::make_moir_queue< GC, T, Traits >::type
85 typedef details::make_moir_queue< GC, T, Traits > maker;
86 typedef typename maker::type base_class;
90 /// Rebind template arguments
91 template <typename GC2, typename T2, typename Traits2>
93 typedef MoirQueue< GC2, T2, Traits2 > other ; ///< Rebinding result
97 typedef T value_type ; ///< Value type stored in the queue
98 typedef typename base_class::gc gc; ///< Garbage collector
99 typedef typename base_class::back_off back_off; ///< Back-off strategy
100 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
101 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
102 typedef typename base_class::stat stat; ///< Internal statistics policy used
103 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
107 typedef typename maker::node_type node_type; ///< queue node type (derived from intrusive::msqueue::node)
109 typedef typename maker::cxx_allocator cxx_allocator;
110 typedef typename maker::node_deallocator node_deallocator; // deallocate node
111 typedef typename base_class::node_traits node_traits;
116 static node_type * alloc_node()
118 return cxx_allocator().New();
120 static node_type * alloc_node( const value_type& val )
122 return cxx_allocator().New( val );
124 template <typename... Args>
125 static node_type * alloc_node_move( Args&&... args )
127 return cxx_allocator().MoveNew( std::forward<Args>( args )... );
129 static void free_node( node_type * p )
131 node_deallocator()( p );
134 struct node_disposer {
135 void operator()( node_type * pNode )
140 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
144 /// Initializes empty queue
148 /// Destructor clears the queue
152 /// Enqueues \p val value into the queue.
154 The function makes queue node in dynamic memory calling copy constructor for \p val
155 and then it calls intrusive::MoirQueue::enqueue.
156 Returns \p true if success, \p false otherwise.
158 bool enqueue( value_type const& val )
160 scoped_node_ptr p( alloc_node(val) );
161 if ( base_class::enqueue( *p )) {
168 /// Enqueues \p data to queue using a functor
170 \p Func is a functor calling to create a new node.
171 The functor should initialize creating node
172 and it takes one argument - a reference to a new node of type \ref value_type :
174 cds:container::MoirQueue< cds::gc::HP, Foo > myQueue;
176 myQueue.enqueue_with( [&bar]( Foo& dest ) { dest = bar; } );
179 template <typename Func>
180 bool enqueue_with( Func f )
182 scoped_node_ptr p( alloc_node() );
184 if ( base_class::enqueue( *p )) {
191 /// Enqueues data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
192 template <typename... Args>
193 bool emplace( Args&&... args )
195 scoped_node_ptr p( alloc_node_move( std::forward<Args>( args )... ) );
196 if ( base_class::enqueue( *p ) ) {
203 /// Synonym for \p enqueue() function
204 bool push( value_type const& val )
206 return enqueue( val );
209 /// Synonym for \p enqueue_with() function
210 template <typename Func>
211 bool push_with( Func f )
213 return enqueue_with( f );
216 /// Dequeues a value from the queue
218 If queue is not empty, the function returns \p true, \p dest contains copy of
219 dequeued value. The assignment operator for type \ref value_type is invoked.
220 If queue is empty, the function returns \p false, \p dest is unchanged.
222 bool dequeue( value_type& dest )
224 return dequeue_with( [&dest]( value_type& src ) { dest = src; } );
227 /// Dequeues a value using a functor
229 \p Func is a functor called to copy dequeued value.
230 The functor takes one argument - a reference to removed node:
232 cds:container::MoirQueue< cds::gc::HP, Foo > myQueue;
234 myQueue.dequeue_with( [&bar]( Foo& src ) { bar = std::move( src );});
236 The functor is called only if the queue is not empty.
238 template <typename Func>
239 bool dequeue_with( Func f )
241 typename base_class::dequeue_result res;
242 if ( base_class::do_dequeue( res )) {
243 f( node_traits::to_value_ptr( *res.pNext )->m_value );
244 base_class::dispose_result( res );
250 /// Synonym for \p dequeue() function
251 bool pop( value_type& dest )
253 return dequeue( dest );
256 /// Synonym for \p dequeue_with() function
257 template <typename Func>
258 bool pop_with( Func f )
260 return dequeue_with( f );
265 The function repeatedly calls \ref dequeue until it returns \p nullptr.
266 The disposer defined in template \p Traits is called for each item
267 that can be safely disposed.
274 /// Checks if the queue is empty
277 return base_class::empty();
280 /// Returns queue's item count (see \ref intrusive::MSQueue::size for explanation)
283 return base_class::size();
286 /// Returns refernce to internal statistics
287 const stat& statistics() const
289 return base_class::statistics();
294 }} // namespace cds::container
296 #endif // #ifndef CDSLIB_CONTAINER_MOIR_QUEUE_H