You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

420 lines
14 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_concurrent_queue_H
  24. #define __TBB_concurrent_queue_H
  25. #include "internal/_concurrent_queue_impl.h"
  26. namespace tbb {
  27. namespace strict_ppl {
  28. //! A high-performance thread-safe non-blocking concurrent queue.
  29. /** Multiple threads may each push and pop concurrently.
  30. Assignment construction is not allowed.
  31. @ingroup containers */
  32. template<typename T, typename A = cache_aligned_allocator<T> >
  33. class concurrent_queue: public internal::concurrent_queue_base_v3<T> {
  34. template<typename Container, typename Value> friend class internal::concurrent_queue_iterator;
  35. //! Allocator type
  36. typedef typename A::template rebind<char>::other page_allocator_type;
  37. page_allocator_type my_allocator;
  38. //! Allocates a block of size n (bytes)
  39. /*override*/ virtual void *allocate_block( size_t n ) {
  40. void *b = reinterpret_cast<void*>(my_allocator.allocate( n ));
  41. if( !b )
  42. internal::throw_exception(internal::eid_bad_alloc);
  43. return b;
  44. }
  45. //! Deallocates block created by allocate_block.
  46. /*override*/ virtual void deallocate_block( void *b, size_t n ) {
  47. my_allocator.deallocate( reinterpret_cast<char*>(b), n );
  48. }
  49. public:
  50. //! Element type in the queue.
  51. typedef T value_type;
  52. //! Reference type
  53. typedef T& reference;
  54. //! Const reference type
  55. typedef const T& const_reference;
  56. //! Integral type for representing size of the queue.
  57. typedef size_t size_type;
  58. //! Difference type for iterator
  59. typedef ptrdiff_t difference_type;
  60. //! Allocator type
  61. typedef A allocator_type;
  62. //! Construct empty queue
  63. explicit concurrent_queue(const allocator_type& a = allocator_type()) :
  64. my_allocator( a )
  65. {
  66. }
  67. //! [begin,end) constructor
  68. template<typename InputIterator>
  69. concurrent_queue( InputIterator begin, InputIterator end, const allocator_type& a = allocator_type()) :
  70. my_allocator( a )
  71. {
  72. for( ; begin != end; ++begin )
  73. this->internal_push(&*begin);
  74. }
  75. //! Copy constructor
  76. concurrent_queue( const concurrent_queue& src, const allocator_type& a = allocator_type()) :
  77. internal::concurrent_queue_base_v3<T>(), my_allocator( a )
  78. {
  79. this->assign( src );
  80. }
  81. //! Destroy queue
  82. ~concurrent_queue();
  83. //! Enqueue an item at tail of queue.
  84. void push( const T& source ) {
  85. this->internal_push( &source );
  86. }
  87. //! Attempt to dequeue an item from head of queue.
  88. /** Does not wait for item to become available.
  89. Returns true if successful; false otherwise. */
  90. bool try_pop( T& result ) {
  91. return this->internal_try_pop( &result );
  92. }
  93. //! Return the number of items in the queue; thread unsafe
  94. size_type unsafe_size() const {return this->internal_size();}
  95. //! Equivalent to size()==0.
  96. bool empty() const {return this->internal_empty();}
  97. //! Clear the queue. not thread-safe.
  98. void clear() ;
  99. //! Return allocator object
  100. allocator_type get_allocator() const { return this->my_allocator; }
  101. typedef internal::concurrent_queue_iterator<concurrent_queue,T> iterator;
  102. typedef internal::concurrent_queue_iterator<concurrent_queue,const T> const_iterator;
  103. //------------------------------------------------------------------------
  104. // The iterators are intended only for debugging. They are slow and not thread safe.
  105. //------------------------------------------------------------------------
  106. iterator unsafe_begin() {return iterator(*this);}
  107. iterator unsafe_end() {return iterator();}
  108. const_iterator unsafe_begin() const {return const_iterator(*this);}
  109. const_iterator unsafe_end() const {return const_iterator();}
  110. } ;
  111. template<typename T, class A>
  112. concurrent_queue<T,A>::~concurrent_queue() {
  113. clear();
  114. this->internal_finish_clear();
  115. }
  116. template<typename T, class A>
  117. void concurrent_queue<T,A>::clear() {
  118. while( !empty() ) {
  119. T value;
  120. this->internal_try_pop(&value);
  121. }
  122. }
  123. } // namespace strict_ppl
  124. //! A high-performance thread-safe blocking concurrent bounded queue.
  125. /** This is the pre-PPL TBB concurrent queue which supports boundedness and blocking semantics.
  126. Note that method names agree with the PPL-style concurrent queue.
  127. Multiple threads may each push and pop concurrently.
  128. Assignment construction is not allowed.
  129. @ingroup containers */
  130. template<typename T, class A = cache_aligned_allocator<T> >
  131. class concurrent_bounded_queue: public internal::concurrent_queue_base_v3 {
  132. template<typename Container, typename Value> friend class internal::concurrent_queue_iterator;
  133. //! Allocator type
  134. typedef typename A::template rebind<char>::other page_allocator_type;
  135. page_allocator_type my_allocator;
  136. typedef typename concurrent_queue_base_v3::padded_page<T> padded_page;
  137. //! Class used to ensure exception-safety of method "pop"
  138. class destroyer: internal::no_copy {
  139. T& my_value;
  140. public:
  141. destroyer( T& value ) : my_value(value) {}
  142. ~destroyer() {my_value.~T();}
  143. };
  144. T& get_ref( page& p, size_t index ) {
  145. __TBB_ASSERT( index<items_per_page, NULL );
  146. return (&static_cast<padded_page*>(static_cast<void*>(&p))->last)[index];
  147. }
  148. /*override*/ virtual void copy_item( page& dst, size_t index, const void* src ) {
  149. new( &get_ref(dst,index) ) T(*static_cast<const T*>(src));
  150. }
  151. /*override*/ virtual void copy_page_item( page& dst, size_t dindex, const page& src, size_t sindex ) {
  152. new( &get_ref(dst,dindex) ) T( get_ref( const_cast<page&>(src), sindex ) );
  153. }
  154. /*override*/ virtual void assign_and_destroy_item( void* dst, page& src, size_t index ) {
  155. T& from = get_ref(src,index);
  156. destroyer d(from);
  157. *static_cast<T*>(dst) = from;
  158. }
  159. /*override*/ virtual page *allocate_page() {
  160. size_t n = sizeof(padded_page) + (items_per_page-1)*sizeof(T);
  161. page *p = reinterpret_cast<page*>(my_allocator.allocate( n ));
  162. if( !p )
  163. internal::throw_exception(internal::eid_bad_alloc);
  164. return p;
  165. }
  166. /*override*/ virtual void deallocate_page( page *p ) {
  167. size_t n = sizeof(padded_page) + (items_per_page-1)*sizeof(T);
  168. my_allocator.deallocate( reinterpret_cast<char*>(p), n );
  169. }
  170. public:
  171. //! Element type in the queue.
  172. typedef T value_type;
  173. //! Allocator type
  174. typedef A allocator_type;
  175. //! Reference type
  176. typedef T& reference;
  177. //! Const reference type
  178. typedef const T& const_reference;
  179. //! Integral type for representing size of the queue.
  180. /** Note that the size_type is a signed integral type.
  181. This is because the size can be negative if there are pending pops without corresponding pushes. */
  182. typedef std::ptrdiff_t size_type;
  183. //! Difference type for iterator
  184. typedef std::ptrdiff_t difference_type;
  185. //! Construct empty queue
  186. explicit concurrent_bounded_queue(const allocator_type& a = allocator_type()) :
  187. concurrent_queue_base_v3( sizeof(T) ), my_allocator( a )
  188. {
  189. }
  190. //! Copy constructor
  191. concurrent_bounded_queue( const concurrent_bounded_queue& src, const allocator_type& a = allocator_type()) :
  192. concurrent_queue_base_v3( sizeof(T) ), my_allocator( a )
  193. {
  194. assign( src );
  195. }
  196. //! [begin,end) constructor
  197. template<typename InputIterator>
  198. concurrent_bounded_queue( InputIterator begin, InputIterator end, const allocator_type& a = allocator_type()) :
  199. concurrent_queue_base_v3( sizeof(T) ), my_allocator( a )
  200. {
  201. for( ; begin != end; ++begin )
  202. internal_push_if_not_full(&*begin);
  203. }
  204. //! Destroy queue
  205. ~concurrent_bounded_queue();
  206. //! Enqueue an item at tail of queue.
  207. void push( const T& source ) {
  208. internal_push( &source );
  209. }
  210. //! Dequeue item from head of queue.
  211. /** Block until an item becomes available, and then dequeue it. */
  212. void pop( T& destination ) {
  213. internal_pop( &destination );
  214. }
  215. #if TBB_USE_EXCEPTIONS
  216. //! Abort all pending queue operations
  217. void abort() {
  218. internal_abort();
  219. }
  220. #endif
  221. //! Enqueue an item at tail of queue if queue is not already full.
  222. /** Does not wait for queue to become not full.
  223. Returns true if item is pushed; false if queue was already full. */
  224. bool try_push( const T& source ) {
  225. return internal_push_if_not_full( &source );
  226. }
  227. //! Attempt to dequeue an item from head of queue.
  228. /** Does not wait for item to become available.
  229. Returns true if successful; false otherwise. */
  230. bool try_pop( T& destination ) {
  231. return internal_pop_if_present( &destination );
  232. }
  233. //! Return number of pushes minus number of pops.
  234. /** Note that the result can be negative if there are pops waiting for the
  235. corresponding pushes. The result can also exceed capacity() if there
  236. are push operations in flight. */
  237. size_type size() const {return internal_size();}
  238. //! Equivalent to size()<=0.
  239. bool empty() const {return internal_empty();}
  240. //! Maximum number of allowed elements
  241. size_type capacity() const {
  242. return my_capacity;
  243. }
  244. //! Set the capacity
  245. /** Setting the capacity to 0 causes subsequent try_push operations to always fail,
  246. and subsequent push operations to block forever. */
  247. void set_capacity( size_type new_capacity ) {
  248. internal_set_capacity( new_capacity, sizeof(T) );
  249. }
  250. //! return allocator object
  251. allocator_type get_allocator() const { return this->my_allocator; }
  252. //! clear the queue. not thread-safe.
  253. void clear() ;
  254. typedef internal::concurrent_queue_iterator<concurrent_bounded_queue,T> iterator;
  255. typedef internal::concurrent_queue_iterator<concurrent_bounded_queue,const T> const_iterator;
  256. //------------------------------------------------------------------------
  257. // The iterators are intended only for debugging. They are slow and not thread safe.
  258. //------------------------------------------------------------------------
  259. iterator unsafe_begin() {return iterator(*this);}
  260. iterator unsafe_end() {return iterator();}
  261. const_iterator unsafe_begin() const {return const_iterator(*this);}
  262. const_iterator unsafe_end() const {return const_iterator();}
  263. };
  264. template<typename T, class A>
  265. concurrent_bounded_queue<T,A>::~concurrent_bounded_queue() {
  266. clear();
  267. internal_finish_clear();
  268. }
  269. template<typename T, class A>
  270. void concurrent_bounded_queue<T,A>::clear() {
  271. while( !empty() ) {
  272. T value;
  273. internal_pop_if_present(&value);
  274. }
  275. }
  276. namespace deprecated {
  277. //! A high-performance thread-safe blocking concurrent bounded queue.
  278. /** This is the pre-PPL TBB concurrent queue which support boundedness and blocking semantics.
  279. Note that method names agree with the PPL-style concurrent queue.
  280. Multiple threads may each push and pop concurrently.
  281. Assignment construction is not allowed.
  282. @ingroup containers */
  283. template<typename T, class A = cache_aligned_allocator<T> >
  284. class concurrent_queue: public concurrent_bounded_queue<T,A> {
  285. #if !__TBB_TEMPLATE_FRIENDS_BROKEN
  286. template<typename Container, typename Value> friend class internal::concurrent_queue_iterator;
  287. #endif
  288. public:
  289. //! Construct empty queue
  290. explicit concurrent_queue(const A& a = A()) :
  291. concurrent_bounded_queue<T,A>( a )
  292. {
  293. }
  294. //! Copy constructor
  295. concurrent_queue( const concurrent_queue& src, const A& a = A()) :
  296. concurrent_bounded_queue<T,A>( src, a )
  297. {
  298. }
  299. //! [begin,end) constructor
  300. template<typename InputIterator>
  301. concurrent_queue( InputIterator b /*begin*/, InputIterator e /*end*/, const A& a = A()) :
  302. concurrent_bounded_queue<T,A>( b, e, a )
  303. {
  304. }
  305. //! Enqueue an item at tail of queue if queue is not already full.
  306. /** Does not wait for queue to become not full.
  307. Returns true if item is pushed; false if queue was already full. */
  308. bool push_if_not_full( const T& source ) {
  309. return this->try_push( source );
  310. }
  311. //! Attempt to dequeue an item from head of queue.
  312. /** Does not wait for item to become available.
  313. Returns true if successful; false otherwise.
  314. @deprecated Use try_pop()
  315. */
  316. bool pop_if_present( T& destination ) {
  317. return this->try_pop( destination );
  318. }
  319. typedef typename concurrent_bounded_queue<T,A>::iterator iterator;
  320. typedef typename concurrent_bounded_queue<T,A>::const_iterator const_iterator;
  321. //
  322. //------------------------------------------------------------------------
  323. // The iterators are intended only for debugging. They are slow and not thread safe.
  324. //------------------------------------------------------------------------
  325. iterator begin() {return this->unsafe_begin();}
  326. iterator end() {return this->unsafe_end();}
  327. const_iterator begin() const {return this->unsafe_begin();}
  328. const_iterator end() const {return this->unsafe_end();}
  329. };
  330. }
  331. #if TBB_DEPRECATED
  332. using deprecated::concurrent_queue;
  333. #else
  334. using strict_ppl::concurrent_queue;
  335. #endif
  336. } // namespace tbb
  337. #endif /* __TBB_concurrent_queue_H */