X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=cds%2Fcontainer%2Fvyukov_mpmc_cycle_queue.h;h=4a06618a25c38a4f52a71263792e1e0fbafa56c9;hb=71da1f106e0d9451556720b8315f7db0f24383b7;hp=5f57d115b465c627a9de5413184507acdd5c4f6f;hpb=1cf811d992476e3d62cef948ba4c3cb1292016b0;p=libcds.git diff --git a/cds/container/vyukov_mpmc_cycle_queue.h b/cds/container/vyukov_mpmc_cycle_queue.h index 5f57d115..4a06618a 100644 --- a/cds/container/vyukov_mpmc_cycle_queue.h +++ b/cds/container/vyukov_mpmc_cycle_queue.h @@ -1,4 +1,32 @@ -//$$CDS-header$$ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ #ifndef CDSLIB_CONTAINER_VYUKOV_MPMC_CYCLE_QUEUE_H #define CDSLIB_CONTAINER_VYUKOV_MPMC_CYCLE_QUEUE_H @@ -21,17 +49,21 @@ namespace cds { namespace container { /// Buffer type for internal array /* The type of element for the buffer is not important: the queue rebinds - buffer for required type via \p rebind metafunction. + the buffer for required type via \p rebind metafunction. For \p VyukovMPMCCycleQueue queue the buffer size should have power-of-2 size. + + You should use only uninitialized buffer for the queue - + \p cds::opt::v::uninitialized_dynamic_buffer (the default), + \p cds::opt::v::uninitialized_static_buffer. */ - typedef cds::opt::v::dynamic_buffer< void * > buffer; + typedef cds::opt::v::uninitialized_dynamic_buffer< void * > buffer; /// A functor to clean item dequeued. /** - The functor calls the destructor for queue item. + The functor calls the destructor for queue item. After an item is dequeued, \p value_cleaner cleans the cell that the item has been occupied. - If \p T is a complex type, \p value_cleaner may be the useful feature. + If \p T is a complex type, \p value_cleaner may be useful feature. Default value is \ref opt::v::destruct_cleaner */ @@ -43,7 +75,7 @@ namespace cds { namespace container { /// C++ memory ordering model /** Can be \p opt::v::relaxed_ordering (relaxed memory model, the default) - or \p opt::v::sequential_consistent (sequentially consisnent memory model). + or \p opt::v::sequential_consistent (sequentially consistent memory model). */ typedef opt::v::relaxed_ordering memory_model; @@ -55,7 +87,7 @@ namespace cds { namespace container { /// Single-consumer version /** - For single-consumer version of algorithm some additional functions + For single-consumer version of algorithm some additional functions (\p front(), \p pop_front()) is available. Default is \p false @@ -66,8 +98,8 @@ namespace cds { namespace container { /// Metafunction converting option list to \p vyukov_queue::traits /** Supported \p Options are: - - \p opt::buffer - the buffer type for internal cyclic array. Possible types are: - \p opt::v::dynamic_buffer (the default), \p opt::v::static_buffer. The type of + - \p opt::buffer - an uninitialized buffer type for internal cyclic array. Possible types are: + \p opt::v::uninitialized_dynamic_buffer (the default), \p opt::v::uninitialized_static_buffer. The type of element in the buffer is not important: it will be changed via \p rebind metafunction. - \p opt::value_cleaner - a functor to clean item dequeued. The functor calls the destructor for queue item. @@ -85,7 +117,7 @@ namespace cds { namespace container { \code typedef cds::container::VyukovMPMCCycleQueue< Foo, typename cds::container::vyukov_queue::make_traits< - cds::opt::buffer< cds::opt::v::static_buffer< void *, 1024 >, + cds::opt::buffer< cds::opt::v::uninitialized_static_buffer< void *, 1024 >, cds::opt::item_counte< cds::atomicity::item_counter > >::type > myQueue; @@ -124,17 +156,17 @@ namespace cds { namespace container { Template parameters - \p T - type stored in queue. - - \p Traits - queue traits, default is \p vykov_queue::traits. You can use \p vykov_queue::make_traits - metafunction to make your traits or just derive your traits from \p %vykov_queue::traits: + - \p Traits - queue traits, default is \p vyukov_queue::traits. You can use \p vyukov_queue::make_traits + metafunction to make your traits or just derive your traits from \p %vyukov_queue::traits: \code - struct myTraits: public cds::container::vykov_queue::traits { + struct myTraits: public cds::container::vyukov_queue::traits { typedef cds::atomicity::item_counter item_counter; }; typedef cds::container::VyukovMPMCCycleQueue< Foo, myTraits > myQueue; // Equivalent make_traits example: typedef cds::container::VyukovMPMCCycleQueue< cds::gc::HP, Foo, - typename cds::container::vykov_queue::make_traits< + typename cds::container::vyukov_queue::make_traits< cds::opt::item_counter< cds::atomicity::item_counter > >::type > myQueue; @@ -150,7 +182,7 @@ namespace cds { namespace container { typedef T value_type; ///< Value type to be stored in the queue typedef Traits traits; ///< Queue traits typedef typename traits::item_counter item_counter; ///< Item counter type - typedef typename traits::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option + typedef typename traits::memory_model memory_model; ///< Memory ordering. See \p cds::opt::memory_model option typedef typename traits::value_cleaner value_cleaner; ///< Value cleaner, see \p vyukov_queue::traits::value_cleaner typedef typename traits::back_off back_off; ///< back-off strategy @@ -193,7 +225,7 @@ namespace cds { namespace container { public: /// Constructs the queue of capacity \p nCapacity /** - For \p cds::opt::v::static_buffer the \p nCapacity parameter is ignored. + For \p cds::opt::v::uninitialized_static_buffer the \p nCapacity parameter is ignored. The buffer capacity must be the power of two. */ @@ -250,7 +282,7 @@ namespace cds { namespace container { } else if (dif < 0) { // Queue full? - if ( pos - m_posDequeue.load( memory_model::memory_order_relaxed ) == capacity() ) + if ( pos - m_posDequeue.load( memory_model::memory_order_relaxed ) == capacity()) return false; // queue full bkoff(); pos = m_posEnqueue.load( memory_model::memory_order_relaxed ); @@ -277,12 +309,24 @@ namespace cds { namespace container { return enqueue_with( [&val]( value_type& dest ){ new ( &dest ) value_type( val ); }); } - /// Synonym for \p enqueue() + /// Enqueues \p val value into the queue, move semantics + bool enqueue( value_type&& val ) + { + return enqueue_with( [&val]( value_type& dest ) { new (&dest) value_type( std::move( val ));}); + } + + /// Synonym for \p enqueue( valuetype const& ) bool push( value_type const& data ) { return enqueue( data ); } + /// Synonym for \p enqueue( value_type&& ) + bool push( value_type&& data ) + { + return enqueue( std::move( data )); + } + /// Synonym for \p enqueue_with() template bool push_with( Func f ) @@ -296,9 +340,10 @@ namespace cds { namespace container { { #if (CDS_COMPILER == CDS_COMPILER_GCC) && (CDS_COMPILER_VERSION < 40900) //work around unsupported feature in g++ 4.8 for forwarding parameter packs to lambda. - return enqueue_with ( std::bind([]( value_type& dest,Args ... args ){ new ( &dest ) value_type( std::forward(args)... );}, std::placeholders::_1 ,args...)); + value_type val( std::forward(args)... ); + return enqueue_with( [&val]( value_type& dest ){ new ( &dest ) value_type( std::move( val )); }); #else - return enqueue_with( [&args ...]( value_type& dest ){ new ( &dest ) value_type( std::forward(args)... ); }); + return enqueue_with( [&args ...]( value_type& dest ){ new ( &dest ) value_type( std::forward( args )... ); }); #endif } @@ -351,13 +396,13 @@ namespace cds { namespace container { /// Dequeues a value from the queue /** - If queue is not empty, the function returns \p true, \p dest contains copy of + If queue is not empty, the function returns \p true, \p dest contains a copy of dequeued value. The assignment operator for type \ref value_type is invoked. If queue is empty, the function returns \p false, \p dest is unchanged. */ - bool dequeue(value_type & dest ) + bool dequeue(value_type& dest ) { - return dequeue_with( [&dest]( value_type& src ){ dest = src; } ); + return dequeue_with( [&dest]( value_type& src ){ dest = std::move( src );}); } /// Synonym for \p dequeue() @@ -438,7 +483,7 @@ namespace cds { namespace container { void clear() { value_type v; - while ( pop(v) ); + while ( pop(v)); } /// Returns queue's item count @@ -459,7 +504,8 @@ namespace cds { namespace container { }; //@cond - namespace vyukov_queue { + namespace vyukov_queue { + template struct single_consumer_traits : public Traits { @@ -468,7 +514,7 @@ namespace cds { namespace container { } // namespace vyukov_queue //@endcond - /// Vyukov's queue multiple producer - single consumer version + /// Vyukov's queue multiple producer - single consumer version template using VyukovMPSCCycleQueue = VyukovMPMCCycleQueue< T, vyukov_queue::single_consumer_traits >;