Removed trailing spaces
[libcds.git] / cds / container / moir_queue.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #ifndef CDSLIB_CONTAINER_MOIR_QUEUE_H
32 #define CDSLIB_CONTAINER_MOIR_QUEUE_H
33
34 #include <memory>
35 #include <cds/container/msqueue.h>
36 #include <cds/intrusive/moir_queue.h>
37
38 namespace cds { namespace container {
39
40     //@cond
41     namespace details {
42         template <typename GC, typename T, typename Traits>
43         struct make_moir_queue: public cds::container::details::make_msqueue< GC, T, Traits >
44         {
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;
47         };
48     }
49     //@endcond
50
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.
54
55         Template arguments:
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:
60             \code
61             struct myTraits: public cds::container::msqueue::traits {
62                 typedef cds::intrusive::msqueue::stat<> stat;
63                 typedef cds::atomicity::item_counter    item_counter;
64             };
65             typedef cds::container::MoirQueue< cds::gc::HP, Foo, myTraits > myQueue;
66
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 >
72                 >::type
73             > myQueue;
74             \endcode
75     */
76     template <typename GC, typename T, typename Traits = cds::container::msqueue::traits >
77     class MoirQueue:
78 #ifdef CDS_DOXYGEN_INVOKED
79         private intrusive::MoirQueue< GC, intrusive::msqueue::node< T >, Traits >
80 #else
81         private details::make_moir_queue< GC, T, Traits >::type
82 #endif
83     {
84         //@cond
85         typedef details::make_moir_queue< GC, T, Traits > maker;
86         typedef typename maker::type base_class;
87         //@endcond
88
89     public:
90         /// Rebind template arguments
91         template <typename GC2, typename T2, typename Traits2>
92         struct rebind {
93             typedef MoirQueue< GC2, T2, Traits2 > other   ;   ///< Rebinding result
94         };
95
96         static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm
97
98     public:
99         typedef T value_type ; ///< Value type stored in the queue
100         typedef typename base_class::gc                 gc;             ///< Garbage collector
101         typedef typename base_class::back_off           back_off;       ///< Back-off strategy
102         typedef typename maker::allocator_type          allocator_type; ///< Allocator type used for allocate/deallocate the nodes
103         typedef typename base_class::item_counter       item_counter;   ///< Item counting policy used
104         typedef typename base_class::stat               stat;           ///< Internal statistics policy used
105         typedef typename base_class::memory_model       memory_model;   ///< Memory ordering. See cds::opt::memory_model option
106
107     protected:
108         //@cond
109         typedef typename maker::node_type  node_type;   ///< queue node type (derived from intrusive::msqueue::node)
110
111         typedef typename maker::cxx_allocator     cxx_allocator;
112         typedef typename maker::node_deallocator  node_deallocator;   // deallocate node
113         typedef typename base_class::node_traits  node_traits;
114         //@endcond
115
116     protected:
117         ///@cond
118         static node_type * alloc_node()
119         {
120             return cxx_allocator().New();
121         }
122         static node_type * alloc_node( const value_type& val )
123         {
124             return cxx_allocator().New( val );
125         }
126         template <typename... Args>
127         static node_type * alloc_node_move( Args&&... args )
128         {
129             return cxx_allocator().MoveNew( std::forward<Args>( args )... );
130         }
131         static void free_node( node_type * p )
132         {
133             node_deallocator()( p );
134         }
135
136         struct node_disposer {
137             void operator()( node_type * pNode )
138             {
139                 free_node( pNode );
140             }
141         };
142         typedef std::unique_ptr< node_type, node_disposer >     scoped_node_ptr;
143         //@endcond
144
145     public:
146         /// Initializes empty queue
147         MoirQueue()
148         {}
149
150         /// Destructor clears the queue
151         ~MoirQueue()
152         {}
153
154         /// Enqueues \p val value into the queue.
155         /**
156             The function makes queue node in dynamic memory calling copy constructor for \p val
157             and then it calls \p intrusive::MoirQueue::enqueue.
158             Returns \p true if success, \p false otherwise.
159         */
160         bool enqueue( value_type const& val )
161         {
162             scoped_node_ptr p( alloc_node(val) );
163             if ( base_class::enqueue( *p )) {
164                 p.release();
165                 return true;
166             }
167             return false;
168         }
169
170         /// Enqueues \p val value into the queue, move semantics
171         bool enqueue( value_type&& val )
172         {
173             scoped_node_ptr p( alloc_node_move( std::move( val )));
174             if ( base_class::enqueue( *p ) ) {
175                 p.release();
176                 return true;
177             }
178             return false;
179         }
180
181         /// Enqueues \p data to queue using a functor
182         /**
183             \p Func is a functor calling to create a new node.
184             The functor should initialize creating node
185             and it takes one argument - a reference to a new node of type \ref value_type :
186             \code
187             cds:container::MoirQueue< cds::gc::HP, Foo > myQueue;
188             Bar bar;
189             myQueue.enqueue_with( [&bar]( Foo& dest ) { dest = bar; } );
190             \endcode
191         */
192         template <typename Func>
193         bool enqueue_with( Func f )
194         {
195             scoped_node_ptr p( alloc_node() );
196             f( p->m_value );
197             if ( base_class::enqueue( *p )) {
198                 p.release();
199                 return true;
200             }
201             return false;
202         }
203
204         /// Enqueues data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
205         template <typename... Args>
206         bool emplace( Args&&... args )
207         {
208             scoped_node_ptr p( alloc_node_move( std::forward<Args>( args )... ) );
209             if ( base_class::enqueue( *p ) ) {
210                 p.release();
211                 return true;
212             }
213             return false;
214         }
215
216         /// Synonym for \p enqueue() function
217         bool push( value_type const& val )
218         {
219             return enqueue( val );
220         }
221
222         /// Synonym for \p enqueue() function, move semantics
223         bool push( value_type&& val )
224         {
225             return enqueue( std::move( val ));
226         }
227
228         /// Synonym for \p enqueue_with() function
229         template <typename Func>
230         bool push_with( Func f )
231         {
232             return enqueue_with( f );
233         }
234
235         /// Dequeues a value from the queue
236         /**
237             If queue is not empty, the function returns \p true, \p dest contains copy of
238             dequeued value. The assignment operator for type \ref value_type is invoked.
239             If queue is empty, the function returns \p false, \p dest is unchanged.
240         */
241         bool dequeue( value_type& dest )
242         {
243             return dequeue_with( [&dest]( value_type& src ) { dest = std::move( src ); });
244         }
245
246         /// Dequeues a value using a functor
247         /**
248             \p Func is a functor called to copy dequeued value.
249             The functor takes one argument - a reference to removed node:
250             \code
251             cds:container::MoirQueue< cds::gc::HP, Foo > myQueue;
252             Bar bar;
253             myQueue.dequeue_with( [&bar]( Foo& src ) { bar = std::move( src );});
254             \endcode
255             The functor is called only if the queue is not empty.
256         */
257         template <typename Func>
258         bool dequeue_with( Func f )
259         {
260             typename base_class::dequeue_result res;
261             if ( base_class::do_dequeue( res )) {
262                 f( node_traits::to_value_ptr( *res.pNext )->m_value );
263                 base_class::dispose_result( res );
264                 return true;
265             }
266             return false;
267         }
268
269         /// Synonym for \p dequeue() function
270         bool pop( value_type& dest )
271         {
272             return dequeue( dest );
273         }
274
275         /// Synonym for \p dequeue_with() function
276         template <typename Func>
277         bool pop_with( Func f )
278         {
279             return dequeue_with( f );
280         }
281
282         /// Clear the queue
283         /**
284             The function repeatedly calls \ref dequeue until it returns \p nullptr.
285             The disposer defined in template \p Traits is called for each item
286             that can be safely disposed.
287         */
288         void clear()
289         {
290             base_class::clear();
291         }
292
293         /// Checks if the queue is empty
294         bool empty() const
295         {
296             return base_class::empty();
297         }
298
299         /// Returns queue's item count (see \ref intrusive::MSQueue::size for explanation)
300         size_t size() const
301         {
302             return base_class::size();
303         }
304
305         /// Returns refernce to internal statistics
306         const stat& statistics() const
307         {
308             return base_class::statistics();
309         }
310
311     };
312
313 }}  // namespace cds::container
314
315 #endif  // #ifndef CDSLIB_CONTAINER_MOIR_QUEUE_H
316
317