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.

673 lines
24 KiB

  1. /*
  2. Copyright 2005-2013 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_pipeline_H
  24. #define __TBB_pipeline_H
  25. #include "atomic.h"
  26. #include "task.h"
  27. #include "tbb_allocator.h"
  28. #include <cstddef>
  29. //TODO: consider more accurate method to check if need to implement <type_trais> ourself
  30. #if !TBB_IMPLEMENT_CPP0X
  31. #include <type_traits>
  32. #endif
  33. namespace tbb {
  34. class pipeline;
  35. class filter;
  36. //! @cond INTERNAL
  37. namespace internal {
  38. // The argument for PIPELINE_VERSION should be an integer between 2 and 9
  39. #define __TBB_PIPELINE_VERSION(x) ((unsigned char)(x-2)<<1)
  40. typedef unsigned long Token;
  41. typedef long tokendiff_t;
  42. class stage_task;
  43. class input_buffer;
  44. class pipeline_root_task;
  45. class pipeline_cleaner;
  46. } // namespace internal
  47. namespace interface6 {
  48. template<typename T, typename U> class filter_t;
  49. namespace internal {
  50. class pipeline_proxy;
  51. }
  52. }
  53. //! @endcond
  54. //! A stage in a pipeline.
  55. /** @ingroup algorithms */
  56. class filter: internal::no_copy {
  57. private:
  58. //! Value used to mark "not in pipeline"
  59. static filter* not_in_pipeline() {return reinterpret_cast<filter*>(intptr_t(-1));}
  60. protected:
  61. //! The lowest bit 0 is for parallel vs. serial
  62. static const unsigned char filter_is_serial = 0x1;
  63. //! 4th bit distinguishes ordered vs unordered filters.
  64. /** The bit was not set for parallel filters in TBB 2.1 and earlier,
  65. but is_ordered() function always treats parallel filters as out of order. */
  66. static const unsigned char filter_is_out_of_order = 0x1<<4;
  67. //! 5th bit distinguishes thread-bound and regular filters.
  68. static const unsigned char filter_is_bound = 0x1<<5;
  69. //! 6th bit marks input filters emitting small objects
  70. static const unsigned char filter_may_emit_null = 0x1<<6;
  71. //! 7th bit defines exception propagation mode expected by the application.
  72. static const unsigned char exact_exception_propagation =
  73. #if TBB_USE_CAPTURED_EXCEPTION
  74. 0x0;
  75. #else
  76. 0x1<<7;
  77. #endif /* TBB_USE_CAPTURED_EXCEPTION */
  78. static const unsigned char current_version = __TBB_PIPELINE_VERSION(5);
  79. static const unsigned char version_mask = 0x7<<1; // bits 1-3 are for version
  80. public:
  81. enum mode {
  82. //! processes multiple items in parallel and in no particular order
  83. parallel = current_version | filter_is_out_of_order,
  84. //! processes items one at a time; all such filters process items in the same order
  85. serial_in_order = current_version | filter_is_serial,
  86. //! processes items one at a time and in no particular order
  87. serial_out_of_order = current_version | filter_is_serial | filter_is_out_of_order,
  88. //! @deprecated use serial_in_order instead
  89. serial = serial_in_order
  90. };
  91. protected:
  92. filter( bool is_serial_ ) :
  93. next_filter_in_pipeline(not_in_pipeline()),
  94. my_input_buffer(NULL),
  95. my_filter_mode(static_cast<unsigned char>((is_serial_ ? serial : parallel) | exact_exception_propagation)),
  96. prev_filter_in_pipeline(not_in_pipeline()),
  97. my_pipeline(NULL),
  98. next_segment(NULL)
  99. {}
  100. filter( mode filter_mode ) :
  101. next_filter_in_pipeline(not_in_pipeline()),
  102. my_input_buffer(NULL),
  103. my_filter_mode(static_cast<unsigned char>(filter_mode | exact_exception_propagation)),
  104. prev_filter_in_pipeline(not_in_pipeline()),
  105. my_pipeline(NULL),
  106. next_segment(NULL)
  107. {}
  108. // signal end-of-input for concrete_filters
  109. void __TBB_EXPORTED_METHOD set_end_of_input();
  110. public:
  111. //! True if filter is serial.
  112. bool is_serial() const {
  113. return bool( my_filter_mode & filter_is_serial );
  114. }
  115. //! True if filter must receive stream in order.
  116. bool is_ordered() const {
  117. return (my_filter_mode & (filter_is_out_of_order|filter_is_serial))==filter_is_serial;
  118. }
  119. //! True if filter is thread-bound.
  120. bool is_bound() const {
  121. return ( my_filter_mode & filter_is_bound )==filter_is_bound;
  122. }
  123. //! true if an input filter can emit null
  124. bool object_may_be_null() {
  125. return ( my_filter_mode & filter_may_emit_null ) == filter_may_emit_null;
  126. }
  127. //! Operate on an item from the input stream, and return item for output stream.
  128. /** Returns NULL if filter is a sink. */
  129. virtual void* operator()( void* item ) = 0;
  130. //! Destroy filter.
  131. /** If the filter was added to a pipeline, the pipeline must be destroyed first. */
  132. virtual __TBB_EXPORTED_METHOD ~filter();
  133. #if __TBB_TASK_GROUP_CONTEXT
  134. //! Destroys item if pipeline was cancelled.
  135. /** Required to prevent memory leaks.
  136. Note it can be called concurrently even for serial filters.*/
  137. virtual void finalize( void* /*item*/ ) {};
  138. #endif
  139. private:
  140. //! Pointer to next filter in the pipeline.
  141. filter* next_filter_in_pipeline;
  142. //! has the filter not yet processed all the tokens it will ever see?
  143. // (pipeline has not yet reached end_of_input or this filter has not yet
  144. // seen the last token produced by input_filter)
  145. bool has_more_work();
  146. //! Buffer for incoming tokens, or NULL if not required.
  147. /** The buffer is required if the filter is serial or follows a thread-bound one. */
  148. internal::input_buffer* my_input_buffer;
  149. friend class internal::stage_task;
  150. friend class internal::pipeline_root_task;
  151. friend class pipeline;
  152. friend class thread_bound_filter;
  153. //! Storage for filter mode and dynamically checked implementation version.
  154. const unsigned char my_filter_mode;
  155. //! Pointer to previous filter in the pipeline.
  156. filter* prev_filter_in_pipeline;
  157. //! Pointer to the pipeline.
  158. pipeline* my_pipeline;
  159. //! Pointer to the next "segment" of filters, or NULL if not required.
  160. /** In each segment, the first filter is not thread-bound but follows a thread-bound one. */
  161. filter* next_segment;
  162. };
  163. //! A stage in a pipeline served by a user thread.
  164. /** @ingroup algorithms */
  165. class thread_bound_filter: public filter {
  166. public:
  167. enum result_type {
  168. // item was processed
  169. success,
  170. // item is currently not available
  171. item_not_available,
  172. // there are no more items to process
  173. end_of_stream
  174. };
  175. protected:
  176. thread_bound_filter(mode filter_mode):
  177. filter(static_cast<mode>(filter_mode | filter::filter_is_bound))
  178. {}
  179. public:
  180. //! If a data item is available, invoke operator() on that item.
  181. /** This interface is non-blocking.
  182. Returns 'success' if an item was processed.
  183. Returns 'item_not_available' if no item can be processed now
  184. but more may arrive in the future, or if token limit is reached.
  185. Returns 'end_of_stream' if there are no more items to process. */
  186. result_type __TBB_EXPORTED_METHOD try_process_item();
  187. //! Wait until a data item becomes available, and invoke operator() on that item.
  188. /** This interface is blocking.
  189. Returns 'success' if an item was processed.
  190. Returns 'end_of_stream' if there are no more items to process.
  191. Never returns 'item_not_available', as it blocks until another return condition applies. */
  192. result_type __TBB_EXPORTED_METHOD process_item();
  193. private:
  194. //! Internal routine for item processing
  195. result_type internal_process_item(bool is_blocking);
  196. };
  197. //! A processing pipeline that applies filters to items.
  198. /** @ingroup algorithms */
  199. class pipeline {
  200. public:
  201. //! Construct empty pipeline.
  202. __TBB_EXPORTED_METHOD pipeline();
  203. /** Though the current implementation declares the destructor virtual, do not rely on this
  204. detail. The virtualness is deprecated and may disappear in future versions of TBB. */
  205. virtual __TBB_EXPORTED_METHOD ~pipeline();
  206. //! Add filter to end of pipeline.
  207. void __TBB_EXPORTED_METHOD add_filter( filter& filter_ );
  208. //! Run the pipeline to completion.
  209. void __TBB_EXPORTED_METHOD run( size_t max_number_of_live_tokens );
  210. #if __TBB_TASK_GROUP_CONTEXT
  211. //! Run the pipeline to completion with user-supplied context.
  212. void __TBB_EXPORTED_METHOD run( size_t max_number_of_live_tokens, tbb::task_group_context& context );
  213. #endif
  214. //! Remove all filters from the pipeline.
  215. void __TBB_EXPORTED_METHOD clear();
  216. private:
  217. friend class internal::stage_task;
  218. friend class internal::pipeline_root_task;
  219. friend class filter;
  220. friend class thread_bound_filter;
  221. friend class internal::pipeline_cleaner;
  222. friend class tbb::interface6::internal::pipeline_proxy;
  223. //! Pointer to first filter in the pipeline.
  224. filter* filter_list;
  225. //! Pointer to location where address of next filter to be added should be stored.
  226. filter* filter_end;
  227. //! task who's reference count is used to determine when all stages are done.
  228. task* end_counter;
  229. //! Number of idle tokens waiting for input stage.
  230. atomic<internal::Token> input_tokens;
  231. //! Global counter of tokens
  232. atomic<internal::Token> token_counter;
  233. //! False until fetch_input returns NULL.
  234. bool end_of_input;
  235. //! True if the pipeline contains a thread-bound filter; false otherwise.
  236. bool has_thread_bound_filters;
  237. //! Remove filter from pipeline.
  238. void remove_filter( filter& filter_ );
  239. //! Not used, but retained to satisfy old export files.
  240. void __TBB_EXPORTED_METHOD inject_token( task& self );
  241. #if __TBB_TASK_GROUP_CONTEXT
  242. //! Does clean up if pipeline is cancelled or exception occured
  243. void clear_filters();
  244. #endif
  245. };
  246. //------------------------------------------------------------------------
  247. // Support for lambda-friendly parallel_pipeline interface
  248. //------------------------------------------------------------------------
  249. namespace interface6 {
  250. namespace internal {
  251. template<typename T, typename U, typename Body> class concrete_filter;
  252. }
  253. //! input_filter control to signal end-of-input for parallel_pipeline
  254. class flow_control {
  255. bool is_pipeline_stopped;
  256. flow_control() { is_pipeline_stopped = false; }
  257. template<typename T, typename U, typename Body> friend class internal::concrete_filter;
  258. public:
  259. void stop() { is_pipeline_stopped = true; }
  260. };
  261. //! @cond INTERNAL
  262. namespace internal {
  263. template<typename T> struct tbb_large_object {enum { value = sizeof(T) > sizeof(void *) }; };
  264. #if TBB_IMPLEMENT_CPP0X
  265. // cannot use SFINAE in current compilers. Explicitly list the types we wish to be
  266. // placed as-is in the pipeline input_buffers.
  267. template<typename T> struct tbb_trivially_copyable { enum { value = false }; };
  268. template<typename T> struct tbb_trivially_copyable <T*> { enum { value = true }; };
  269. template<> struct tbb_trivially_copyable <short> { enum { value = true }; };
  270. template<> struct tbb_trivially_copyable <unsigned short> { enum { value = true }; };
  271. template<> struct tbb_trivially_copyable <int> { enum { value = !tbb_large_object<int>::value }; };
  272. template<> struct tbb_trivially_copyable <unsigned int> { enum { value = !tbb_large_object<int>::value }; };
  273. template<> struct tbb_trivially_copyable <long> { enum { value = !tbb_large_object<long>::value }; };
  274. template<> struct tbb_trivially_copyable <unsigned long> { enum { value = !tbb_large_object<long>::value }; };
  275. template<> struct tbb_trivially_copyable <float> { enum { value = !tbb_large_object<float>::value }; };
  276. template<> struct tbb_trivially_copyable <double> { enum { value = !tbb_large_object<double>::value }; };
  277. #else
  278. #if __GNUC__==4 && __GNUC_MINOR__>=4 && __GXX_EXPERIMENTAL_CXX0X__
  279. template<typename T> struct tbb_trivially_copyable { enum { value = std::has_trivial_copy_constructor<T>::value }; };
  280. #else
  281. template<typename T> struct tbb_trivially_copyable { enum { value = std::is_trivially_copyable<T>::value }; };
  282. #endif //
  283. #endif // TBB_IMPLEMENT_CPP0X
  284. template<typename T> struct is_large_object {enum { value = tbb_large_object<T>::value || !tbb_trivially_copyable<T>::value }; };
  285. template<typename T, bool> class token_helper;
  286. // large object helper (uses tbb_allocator)
  287. template<typename T>
  288. class token_helper<T, true> {
  289. public:
  290. typedef typename tbb::tbb_allocator<T> allocator;
  291. typedef T* pointer;
  292. typedef T value_type;
  293. static pointer create_token(const value_type & source) {
  294. pointer output_t = allocator().allocate(1);
  295. return new (output_t) T(source);
  296. }
  297. static value_type & token(pointer & t) { return *t;}
  298. static void * cast_to_void_ptr(pointer ref) { return (void *) ref; }
  299. static pointer cast_from_void_ptr(void * ref) { return (pointer)ref; }
  300. static void destroy_token(pointer token) {
  301. allocator().destroy(token);
  302. allocator().deallocate(token,1);
  303. }
  304. };
  305. // pointer specialization
  306. template<typename T>
  307. class token_helper<T*, false > {
  308. public:
  309. typedef T* pointer;
  310. typedef T* value_type;
  311. static pointer create_token(const value_type & source) { return source; }
  312. static value_type & token(pointer & t) { return t;}
  313. static void * cast_to_void_ptr(pointer ref) { return (void *)ref; }
  314. static pointer cast_from_void_ptr(void * ref) { return (pointer)ref; }
  315. static void destroy_token( pointer /*token*/) {}
  316. };
  317. // small object specialization (converts void* to the correct type, passes objects directly.)
  318. template<typename T>
  319. class token_helper<T, false> {
  320. typedef union {
  321. T actual_value;
  322. void * void_overlay;
  323. } type_to_void_ptr_map;
  324. public:
  325. typedef T pointer; // not really a pointer in this case.
  326. typedef T value_type;
  327. static pointer create_token(const value_type & source) {
  328. return source; }
  329. static value_type & token(pointer & t) { return t;}
  330. static void * cast_to_void_ptr(pointer ref) {
  331. type_to_void_ptr_map mymap;
  332. mymap.void_overlay = NULL;
  333. mymap.actual_value = ref;
  334. return mymap.void_overlay;
  335. }
  336. static pointer cast_from_void_ptr(void * ref) {
  337. type_to_void_ptr_map mymap;
  338. mymap.void_overlay = ref;
  339. return mymap.actual_value;
  340. }
  341. static void destroy_token( pointer /*token*/) {}
  342. };
  343. template<typename T, typename U, typename Body>
  344. class concrete_filter: public tbb::filter {
  345. const Body& my_body;
  346. typedef token_helper<T,is_large_object<T>::value > t_helper;
  347. typedef typename t_helper::pointer t_pointer;
  348. typedef token_helper<U,is_large_object<U>::value > u_helper;
  349. typedef typename u_helper::pointer u_pointer;
  350. /*override*/ void* operator()(void* input) {
  351. t_pointer temp_input = t_helper::cast_from_void_ptr(input);
  352. u_pointer output_u = u_helper::create_token(my_body(t_helper::token(temp_input)));
  353. t_helper::destroy_token(temp_input);
  354. return u_helper::cast_to_void_ptr(output_u);
  355. }
  356. /*override*/ void finalize(void * input) {
  357. t_pointer temp_input = t_helper::cast_from_void_ptr(input);
  358. t_helper::destroy_token(temp_input);
  359. }
  360. public:
  361. concrete_filter(tbb::filter::mode filter_mode, const Body& body) : filter(filter_mode), my_body(body) {}
  362. };
  363. // input
  364. template<typename U, typename Body>
  365. class concrete_filter<void,U,Body>: public filter {
  366. const Body& my_body;
  367. typedef token_helper<U, is_large_object<U>::value > u_helper;
  368. typedef typename u_helper::pointer u_pointer;
  369. /*override*/void* operator()(void*) {
  370. flow_control control;
  371. u_pointer output_u = u_helper::create_token(my_body(control));
  372. if(control.is_pipeline_stopped) {
  373. u_helper::destroy_token(output_u);
  374. set_end_of_input();
  375. return NULL;
  376. }
  377. return u_helper::cast_to_void_ptr(output_u);
  378. }
  379. public:
  380. concrete_filter(tbb::filter::mode filter_mode, const Body& body) :
  381. filter(static_cast<tbb::filter::mode>(filter_mode | filter_may_emit_null)),
  382. my_body(body)
  383. {}
  384. };
  385. template<typename T, typename Body>
  386. class concrete_filter<T,void,Body>: public filter {
  387. const Body& my_body;
  388. typedef token_helper<T, is_large_object<T>::value > t_helper;
  389. typedef typename t_helper::pointer t_pointer;
  390. /*override*/ void* operator()(void* input) {
  391. t_pointer temp_input = t_helper::cast_from_void_ptr(input);
  392. my_body(t_helper::token(temp_input));
  393. t_helper::destroy_token(temp_input);
  394. return NULL;
  395. }
  396. /*override*/ void finalize(void* input) {
  397. t_pointer temp_input = t_helper::cast_from_void_ptr(input);
  398. t_helper::destroy_token(temp_input);
  399. }
  400. public:
  401. concrete_filter(tbb::filter::mode filter_mode, const Body& body) : filter(filter_mode), my_body(body) {}
  402. };
  403. template<typename Body>
  404. class concrete_filter<void,void,Body>: public filter {
  405. const Body& my_body;
  406. /** Override privately because it is always called virtually */
  407. /*override*/ void* operator()(void*) {
  408. flow_control control;
  409. my_body(control);
  410. void* output = control.is_pipeline_stopped ? NULL : (void*)(intptr_t)-1;
  411. return output;
  412. }
  413. public:
  414. concrete_filter(filter::mode filter_mode, const Body& body) : filter(filter_mode), my_body(body) {}
  415. };
  416. //! The class that represents an object of the pipeline for parallel_pipeline().
  417. /** It primarily serves as RAII class that deletes heap-allocated filter instances. */
  418. class pipeline_proxy {
  419. tbb::pipeline my_pipe;
  420. public:
  421. pipeline_proxy( const filter_t<void,void>& filter_chain );
  422. ~pipeline_proxy() {
  423. while( filter* f = my_pipe.filter_list )
  424. delete f; // filter destructor removes it from the pipeline
  425. }
  426. tbb::pipeline* operator->() { return &my_pipe; }
  427. };
  428. //! Abstract base class that represents a node in a parse tree underlying a filter_t.
  429. /** These nodes are always heap-allocated and can be shared by filter_t objects. */
  430. class filter_node: tbb::internal::no_copy {
  431. /** Count must be atomic because it is hidden state for user, but might be shared by threads. */
  432. tbb::atomic<intptr_t> ref_count;
  433. protected:
  434. filter_node() {
  435. ref_count = 0;
  436. #ifdef __TBB_TEST_FILTER_NODE_COUNT
  437. ++(__TBB_TEST_FILTER_NODE_COUNT);
  438. #endif
  439. }
  440. public:
  441. //! Add concrete_filter to pipeline
  442. virtual void add_to( pipeline& ) = 0;
  443. //! Increment reference count
  444. void add_ref() {++ref_count;}
  445. //! Decrement reference count and delete if it becomes zero.
  446. void remove_ref() {
  447. __TBB_ASSERT(ref_count>0,"ref_count underflow");
  448. if( --ref_count==0 )
  449. delete this;
  450. }
  451. virtual ~filter_node() {
  452. #ifdef __TBB_TEST_FILTER_NODE_COUNT
  453. --(__TBB_TEST_FILTER_NODE_COUNT);
  454. #endif
  455. }
  456. };
  457. //! Node in parse tree representing result of make_filter.
  458. template<typename T, typename U, typename Body>
  459. class filter_node_leaf: public filter_node {
  460. const tbb::filter::mode mode;
  461. const Body body;
  462. /*override*/void add_to( pipeline& p ) {
  463. concrete_filter<T,U,Body>* f = new concrete_filter<T,U,Body>(mode,body);
  464. p.add_filter( *f );
  465. }
  466. public:
  467. filter_node_leaf( tbb::filter::mode m, const Body& b ) : mode(m), body(b) {}
  468. };
  469. //! Node in parse tree representing join of two filters.
  470. class filter_node_join: public filter_node {
  471. friend class filter_node; // to suppress GCC 3.2 warnings
  472. filter_node& left;
  473. filter_node& right;
  474. /*override*/~filter_node_join() {
  475. left.remove_ref();
  476. right.remove_ref();
  477. }
  478. /*override*/void add_to( pipeline& p ) {
  479. left.add_to(p);
  480. right.add_to(p);
  481. }
  482. public:
  483. filter_node_join( filter_node& x, filter_node& y ) : left(x), right(y) {
  484. left.add_ref();
  485. right.add_ref();
  486. }
  487. };
  488. } // namespace internal
  489. //! @endcond
  490. //! Create a filter to participate in parallel_pipeline
  491. template<typename T, typename U, typename Body>
  492. filter_t<T,U> make_filter(tbb::filter::mode mode, const Body& body) {
  493. return new internal::filter_node_leaf<T,U,Body>(mode, body);
  494. }
  495. template<typename T, typename V, typename U>
  496. filter_t<T,U> operator& (const filter_t<T,V>& left, const filter_t<V,U>& right) {
  497. __TBB_ASSERT(left.root,"cannot use default-constructed filter_t as left argument of '&'");
  498. __TBB_ASSERT(right.root,"cannot use default-constructed filter_t as right argument of '&'");
  499. return new internal::filter_node_join(*left.root,*right.root);
  500. }
  501. //! Class representing a chain of type-safe pipeline filters
  502. template<typename T, typename U>
  503. class filter_t {
  504. typedef internal::filter_node filter_node;
  505. filter_node* root;
  506. filter_t( filter_node* root_ ) : root(root_) {
  507. root->add_ref();
  508. }
  509. friend class internal::pipeline_proxy;
  510. template<typename T_, typename U_, typename Body>
  511. friend filter_t<T_,U_> make_filter(tbb::filter::mode, const Body& );
  512. template<typename T_, typename V_, typename U_>
  513. friend filter_t<T_,U_> operator& (const filter_t<T_,V_>& , const filter_t<V_,U_>& );
  514. public:
  515. filter_t() : root(NULL) {}
  516. filter_t( const filter_t<T,U>& rhs ) : root(rhs.root) {
  517. if( root ) root->add_ref();
  518. }
  519. template<typename Body>
  520. filter_t( tbb::filter::mode mode, const Body& body ) :
  521. root( new internal::filter_node_leaf<T,U,Body>(mode, body) ) {
  522. root->add_ref();
  523. }
  524. void operator=( const filter_t<T,U>& rhs ) {
  525. // Order of operations below carefully chosen so that reference counts remain correct
  526. // in unlikely event that remove_ref throws exception.
  527. filter_node* old = root;
  528. root = rhs.root;
  529. if( root ) root->add_ref();
  530. if( old ) old->remove_ref();
  531. }
  532. ~filter_t() {
  533. if( root ) root->remove_ref();
  534. }
  535. void clear() {
  536. // Like operator= with filter_t() on right side.
  537. if( root ) {
  538. filter_node* old = root;
  539. root = NULL;
  540. old->remove_ref();
  541. }
  542. }
  543. };
  544. inline internal::pipeline_proxy::pipeline_proxy( const filter_t<void,void>& filter_chain ) : my_pipe() {
  545. __TBB_ASSERT( filter_chain.root, "cannot apply parallel_pipeline to default-constructed filter_t" );
  546. filter_chain.root->add_to(my_pipe);
  547. }
  548. inline void parallel_pipeline(size_t max_number_of_live_tokens, const filter_t<void,void>& filter_chain
  549. #if __TBB_TASK_GROUP_CONTEXT
  550. , tbb::task_group_context& context
  551. #endif
  552. ) {
  553. internal::pipeline_proxy pipe(filter_chain);
  554. // tbb::pipeline::run() is called via the proxy
  555. pipe->run(max_number_of_live_tokens
  556. #if __TBB_TASK_GROUP_CONTEXT
  557. , context
  558. #endif
  559. );
  560. }
  561. #if __TBB_TASK_GROUP_CONTEXT
  562. inline void parallel_pipeline(size_t max_number_of_live_tokens, const filter_t<void,void>& filter_chain) {
  563. tbb::task_group_context context;
  564. parallel_pipeline(max_number_of_live_tokens, filter_chain, context);
  565. }
  566. #endif // __TBB_TASK_GROUP_CONTEXT
  567. } // interface6
  568. using interface6::flow_control;
  569. using interface6::filter_t;
  570. using interface6::make_filter;
  571. using interface6::parallel_pipeline;
  572. } // tbb
  573. #endif /* __TBB_pipeline_H */