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.

2065 lines
72 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_flow_graph_H
  24. #define __TBB_flow_graph_H
  25. #include "tbb_stddef.h"
  26. #include "atomic.h"
  27. #include "spin_mutex.h"
  28. #include "null_mutex.h"
  29. #include "spin_rw_mutex.h"
  30. #include "null_rw_mutex.h"
  31. #include "task.h"
  32. #include "concurrent_vector.h"
  33. #include "internal/_aggregator_impl.h"
  34. // use the VC10 or gcc version of tuple if it is available.
  35. #if __TBB_CPP11_TUPLE_PRESENT
  36. #include <tuple>
  37. namespace tbb {
  38. namespace flow {
  39. using std::tuple;
  40. using std::tuple_size;
  41. using std::tuple_element;
  42. using std::get;
  43. }
  44. }
  45. #else
  46. #include "compat/tuple"
  47. #endif
  48. #include<list>
  49. #include<queue>
  50. /** @file
  51. \brief The graph related classes and functions
  52. There are some applications that best express dependencies as messages
  53. passed between nodes in a graph. These messages may contain data or
  54. simply act as signals that a predecessors has completed. The graph
  55. class and its associated node classes can be used to express such
  56. applcations.
  57. */
  58. namespace tbb {
  59. namespace flow {
  60. //! An enumeration the provides the two most common concurrency levels: unlimited and serial
  61. enum concurrency { unlimited = 0, serial = 1 };
  62. namespace interface6 {
  63. namespace internal {
  64. template<typename T, typename M> class successor_cache;
  65. template<typename T, typename M> class broadcast_cache;
  66. template<typename T, typename M> class round_robin_cache;
  67. }
  68. //! An empty class used for messages that mean "I'm done"
  69. class continue_msg {};
  70. template< typename T > class sender;
  71. template< typename T > class receiver;
  72. class continue_receiver;
  73. //! Pure virtual template class that defines a sender of messages of type T
  74. template< typename T >
  75. class sender {
  76. public:
  77. //! The output type of this sender
  78. typedef T output_type;
  79. //! The successor type for this node
  80. typedef receiver<T> successor_type;
  81. virtual ~sender() {}
  82. //! Add a new successor to this node
  83. virtual bool register_successor( successor_type &r ) = 0;
  84. //! Removes a successor from this node
  85. virtual bool remove_successor( successor_type &r ) = 0;
  86. //! Request an item from the sender
  87. virtual bool try_get( T & ) { return false; }
  88. //! Reserves an item in the sender
  89. virtual bool try_reserve( T & ) { return false; }
  90. //! Releases the reserved item
  91. virtual bool try_release( ) { return false; }
  92. //! Consumes the reserved item
  93. virtual bool try_consume( ) { return false; }
  94. };
  95. template< typename T > class limiter_node; // needed for resetting decrementer
  96. template< typename R, typename B > class run_and_put_task;
  97. static tbb::task * const SUCCESSFULLY_ENQUEUED = (task *)-1;
  98. // enqueue left task if necessary. Returns the non-enqueued task if there is one.
  99. static inline tbb::task *combine_tasks( tbb::task * left, tbb::task * right) {
  100. // if no RHS task, don't change left.
  101. if(right == NULL) return left;
  102. // right != NULL
  103. if(left == NULL) return right;
  104. if(left == SUCCESSFULLY_ENQUEUED) return right;
  105. // left contains a task
  106. if(right != SUCCESSFULLY_ENQUEUED) {
  107. // both are valid tasks
  108. tbb::task::enqueue(*left);
  109. return right;
  110. }
  111. return left;
  112. }
  113. //! Pure virtual template class that defines a receiver of messages of type T
  114. template< typename T >
  115. class receiver {
  116. public:
  117. //! The input type of this receiver
  118. typedef T input_type;
  119. //! The predecessor type for this node
  120. typedef sender<T> predecessor_type;
  121. //! Destructor
  122. virtual ~receiver() {}
  123. //! Put an item to the receiver
  124. bool try_put( const T& t ) {
  125. task *res = try_put_task(t);
  126. if(!res) return false;
  127. if (res != SUCCESSFULLY_ENQUEUED) task::enqueue(*res);
  128. return true;
  129. }
  130. //! put item to successor; return task to run the successor if possible.
  131. protected:
  132. template< typename R, typename B > friend class run_and_put_task;
  133. template<typename X, typename Y> friend class internal::broadcast_cache;
  134. template<typename X, typename Y> friend class internal::round_robin_cache;
  135. virtual task *try_put_task(const T& t) = 0;
  136. public:
  137. //! Add a predecessor to the node
  138. virtual bool register_predecessor( predecessor_type & ) { return false; }
  139. //! Remove a predecessor from the node
  140. virtual bool remove_predecessor( predecessor_type & ) { return false; }
  141. protected:
  142. //! put receiver back in initial state
  143. template<typename U> friend class limiter_node;
  144. virtual void reset_receiver() = 0;
  145. template<typename TT, typename M>
  146. friend class internal::successor_cache;
  147. virtual bool is_continue_receiver() { return false; }
  148. };
  149. //! Base class for receivers of completion messages
  150. /** These receivers automatically reset, but cannot be explicitly waited on */
  151. class continue_receiver : public receiver< continue_msg > {
  152. public:
  153. //! The input type
  154. typedef continue_msg input_type;
  155. //! The predecessor type for this node
  156. typedef sender< continue_msg > predecessor_type;
  157. //! Constructor
  158. continue_receiver( int number_of_predecessors = 0 ) {
  159. my_predecessor_count = my_initial_predecessor_count = number_of_predecessors;
  160. my_current_count = 0;
  161. }
  162. //! Copy constructor
  163. continue_receiver( const continue_receiver& src ) : receiver<continue_msg>() {
  164. my_predecessor_count = my_initial_predecessor_count = src.my_initial_predecessor_count;
  165. my_current_count = 0;
  166. }
  167. //! Destructor
  168. virtual ~continue_receiver() { }
  169. //! Increments the trigger threshold
  170. /* override */ bool register_predecessor( predecessor_type & ) {
  171. spin_mutex::scoped_lock l(my_mutex);
  172. ++my_predecessor_count;
  173. return true;
  174. }
  175. //! Decrements the trigger threshold
  176. /** Does not check to see if the removal of the predecessor now makes the current count
  177. exceed the new threshold. So removing a predecessor while the graph is active can cause
  178. unexpected results. */
  179. /* override */ bool remove_predecessor( predecessor_type & ) {
  180. spin_mutex::scoped_lock l(my_mutex);
  181. --my_predecessor_count;
  182. return true;
  183. }
  184. protected:
  185. template< typename R, typename B > friend class run_and_put_task;
  186. template<typename X, typename Y> friend class internal::broadcast_cache;
  187. template<typename X, typename Y> friend class internal::round_robin_cache;
  188. // execute body is supposed to be too small to create a task for.
  189. /* override */ task *try_put_task( const input_type & ) {
  190. {
  191. spin_mutex::scoped_lock l(my_mutex);
  192. if ( ++my_current_count < my_predecessor_count )
  193. return SUCCESSFULLY_ENQUEUED;
  194. else
  195. my_current_count = 0;
  196. }
  197. task * res = execute();
  198. if(!res) return SUCCESSFULLY_ENQUEUED;
  199. return res;
  200. }
  201. spin_mutex my_mutex;
  202. int my_predecessor_count;
  203. int my_current_count;
  204. int my_initial_predecessor_count;
  205. // the friend declaration in the base class did not eliminate the "protected class"
  206. // error in gcc 4.1.2
  207. template<typename U> friend class limiter_node;
  208. /*override*/void reset_receiver() {
  209. my_current_count = 0;
  210. }
  211. //! Does whatever should happen when the threshold is reached
  212. /** This should be very fast or else spawn a task. This is
  213. called while the sender is blocked in the try_put(). */
  214. virtual task * execute() = 0;
  215. template<typename TT, typename M>
  216. friend class internal::successor_cache;
  217. /*override*/ bool is_continue_receiver() { return true; }
  218. };
  219. #include "internal/_flow_graph_impl.h"
  220. using namespace internal::graph_policy_namespace;
  221. class graph;
  222. class graph_node;
  223. template <typename GraphContainerType, typename GraphNodeType>
  224. class graph_iterator {
  225. friend class graph;
  226. friend class graph_node;
  227. public:
  228. typedef size_t size_type;
  229. typedef GraphNodeType value_type;
  230. typedef GraphNodeType* pointer;
  231. typedef GraphNodeType& reference;
  232. typedef const GraphNodeType& const_reference;
  233. typedef std::forward_iterator_tag iterator_category;
  234. //! Default constructor
  235. graph_iterator() : my_graph(NULL), current_node(NULL) {}
  236. //! Copy constructor
  237. graph_iterator(const graph_iterator& other) :
  238. my_graph(other.my_graph), current_node(other.current_node)
  239. {}
  240. //! Assignment
  241. graph_iterator& operator=(const graph_iterator& other) {
  242. if (this != &other) {
  243. my_graph = other.my_graph;
  244. current_node = other.current_node;
  245. }
  246. return *this;
  247. }
  248. //! Dereference
  249. reference operator*() const;
  250. //! Dereference
  251. pointer operator->() const;
  252. //! Equality
  253. bool operator==(const graph_iterator& other) const {
  254. return ((my_graph == other.my_graph) && (current_node == other.current_node));
  255. }
  256. //! Inequality
  257. bool operator!=(const graph_iterator& other) const { return !(operator==(other)); }
  258. //! Pre-increment
  259. graph_iterator& operator++() {
  260. internal_forward();
  261. return *this;
  262. }
  263. //! Post-increment
  264. graph_iterator operator++(int) {
  265. graph_iterator result = *this;
  266. operator++();
  267. return result;
  268. }
  269. private:
  270. // the graph over which we are iterating
  271. GraphContainerType *my_graph;
  272. // pointer into my_graph's my_nodes list
  273. pointer current_node;
  274. //! Private initializing constructor for begin() and end() iterators
  275. graph_iterator(GraphContainerType *g, bool begin);
  276. void internal_forward();
  277. };
  278. //! The graph class
  279. /** This class serves as a handle to the graph */
  280. class graph : tbb::internal::no_copy {
  281. friend class graph_node;
  282. template< typename Body >
  283. class run_task : public task {
  284. public:
  285. run_task( Body& body ) : my_body(body) {}
  286. task *execute() {
  287. my_body();
  288. return NULL;
  289. }
  290. private:
  291. Body my_body;
  292. };
  293. template< typename Receiver, typename Body >
  294. class run_and_put_task : public task {
  295. public:
  296. run_and_put_task( Receiver &r, Body& body ) : my_receiver(r), my_body(body) {}
  297. task *execute() {
  298. task *res = my_receiver.try_put_task( my_body() );
  299. if(res == SUCCESSFULLY_ENQUEUED) res = NULL;
  300. return res;
  301. }
  302. private:
  303. Receiver &my_receiver;
  304. Body my_body;
  305. };
  306. public:
  307. //! Constructs a graph with isolated task_group_context
  308. explicit graph() : my_nodes(NULL), my_nodes_last(NULL)
  309. {
  310. own_context = true;
  311. cancelled = false;
  312. caught_exception = false;
  313. my_context = new task_group_context();
  314. my_root_task = ( new ( task::allocate_root(*my_context) ) empty_task );
  315. my_root_task->set_ref_count(1);
  316. }
  317. //! Constructs a graph with use_this_context as context
  318. explicit graph(task_group_context& use_this_context) :
  319. my_context(&use_this_context), my_nodes(NULL), my_nodes_last(NULL)
  320. {
  321. own_context = false;
  322. my_root_task = ( new ( task::allocate_root(*my_context) ) empty_task );
  323. my_root_task->set_ref_count(1);
  324. }
  325. //! Destroys the graph.
  326. /** Calls wait_for_all, then destroys the root task and context. */
  327. ~graph() {
  328. wait_for_all();
  329. my_root_task->set_ref_count(0);
  330. task::destroy( *my_root_task );
  331. if (own_context) delete my_context;
  332. }
  333. //! Used to register that an external entity may still interact with the graph.
  334. /** The graph will not return from wait_for_all until a matching number of decrement_wait_count calls
  335. is made. */
  336. void increment_wait_count() {
  337. if (my_root_task)
  338. my_root_task->increment_ref_count();
  339. }
  340. //! Deregisters an external entity that may have interacted with the graph.
  341. /** The graph will not return from wait_for_all until all the number of decrement_wait_count calls
  342. matches the number of increment_wait_count calls. */
  343. void decrement_wait_count() {
  344. if (my_root_task)
  345. my_root_task->decrement_ref_count();
  346. }
  347. //! Spawns a task that runs a body and puts its output to a specific receiver
  348. /** The task is spawned as a child of the graph. This is useful for running tasks
  349. that need to block a wait_for_all() on the graph. For example a one-off source. */
  350. template< typename Receiver, typename Body >
  351. void run( Receiver &r, Body body ) {
  352. task::enqueue( * new ( task::allocate_additional_child_of( *my_root_task ) )
  353. run_and_put_task< Receiver, Body >( r, body ) );
  354. }
  355. //! Spawns a task that runs a function object
  356. /** The task is spawned as a child of the graph. This is useful for running tasks
  357. that need to block a wait_for_all() on the graph. For example a one-off source. */
  358. template< typename Body >
  359. void run( Body body ) {
  360. task::enqueue( * new ( task::allocate_additional_child_of( *my_root_task ) )
  361. run_task< Body >( body ) );
  362. }
  363. //! Wait until graph is idle and decrement_wait_count calls equals increment_wait_count calls.
  364. /** The waiting thread will go off and steal work while it is block in the wait_for_all. */
  365. void wait_for_all() {
  366. cancelled = false;
  367. caught_exception = false;
  368. if (my_root_task) {
  369. #if TBB_USE_EXCEPTIONS
  370. try {
  371. #endif
  372. my_root_task->wait_for_all();
  373. cancelled = my_context->is_group_execution_cancelled();
  374. #if TBB_USE_EXCEPTIONS
  375. }
  376. catch(...) {
  377. my_root_task->set_ref_count(1);
  378. my_context->reset();
  379. caught_exception = true;
  380. cancelled = true;
  381. throw;
  382. }
  383. #endif
  384. my_context->reset(); // consistent with behavior in catch()
  385. my_root_task->set_ref_count(1);
  386. }
  387. }
  388. //! Returns the root task of the graph
  389. task * root_task() {
  390. return my_root_task;
  391. }
  392. // ITERATORS
  393. template<typename C, typename N>
  394. friend class graph_iterator;
  395. // Graph iterator typedefs
  396. typedef graph_iterator<graph,graph_node> iterator;
  397. typedef graph_iterator<const graph,const graph_node> const_iterator;
  398. // Graph iterator constructors
  399. //! start iterator
  400. iterator begin() { return iterator(this, true); }
  401. //! end iterator
  402. iterator end() { return iterator(this, false); }
  403. //! start const iterator
  404. const_iterator begin() const { return const_iterator(this, true); }
  405. //! end const iterator
  406. const_iterator end() const { return const_iterator(this, false); }
  407. //! start const iterator
  408. const_iterator cbegin() const { return const_iterator(this, true); }
  409. //! end const iterator
  410. const_iterator cend() const { return const_iterator(this, false); }
  411. //! return status of graph execution
  412. bool is_cancelled() { return cancelled; }
  413. bool exception_thrown() { return caught_exception; }
  414. // un-thread-safe state reset.
  415. void reset();
  416. private:
  417. task *my_root_task;
  418. task_group_context *my_context;
  419. bool own_context;
  420. bool cancelled;
  421. bool caught_exception;
  422. graph_node *my_nodes, *my_nodes_last;
  423. spin_mutex nodelist_mutex;
  424. void register_node(graph_node *n);
  425. void remove_node(graph_node *n);
  426. };
  427. template <typename C, typename N>
  428. graph_iterator<C,N>::graph_iterator(C *g, bool begin) : my_graph(g), current_node(NULL)
  429. {
  430. if (begin) current_node = my_graph->my_nodes;
  431. //else it is an end iterator by default
  432. }
  433. template <typename C, typename N>
  434. typename graph_iterator<C,N>::reference graph_iterator<C,N>::operator*() const {
  435. __TBB_ASSERT(current_node, "graph_iterator at end");
  436. return *operator->();
  437. }
  438. template <typename C, typename N>
  439. typename graph_iterator<C,N>::pointer graph_iterator<C,N>::operator->() const {
  440. return current_node;
  441. }
  442. template <typename C, typename N>
  443. void graph_iterator<C,N>::internal_forward() {
  444. if (current_node) current_node = current_node->next;
  445. }
  446. //! The base of all graph nodes.
  447. class graph_node : tbb::internal::no_assign {
  448. friend class graph;
  449. template<typename C, typename N>
  450. friend class graph_iterator;
  451. protected:
  452. graph& my_graph;
  453. graph_node *next, *prev;
  454. public:
  455. graph_node(graph& g) : my_graph(g) {
  456. my_graph.register_node(this);
  457. }
  458. virtual ~graph_node() {
  459. my_graph.remove_node(this);
  460. }
  461. protected:
  462. virtual void reset() = 0;
  463. };
  464. inline void graph::register_node(graph_node *n) {
  465. n->next = NULL;
  466. {
  467. spin_mutex::scoped_lock lock(nodelist_mutex);
  468. n->prev = my_nodes_last;
  469. if (my_nodes_last) my_nodes_last->next = n;
  470. my_nodes_last = n;
  471. if (!my_nodes) my_nodes = n;
  472. }
  473. }
  474. inline void graph::remove_node(graph_node *n) {
  475. {
  476. spin_mutex::scoped_lock lock(nodelist_mutex);
  477. __TBB_ASSERT(my_nodes && my_nodes_last, "graph::remove_node: Error: no registered nodes");
  478. if (n->prev) n->prev->next = n->next;
  479. if (n->next) n->next->prev = n->prev;
  480. if (my_nodes_last == n) my_nodes_last = n->prev;
  481. if (my_nodes == n) my_nodes = n->next;
  482. }
  483. n->prev = n->next = NULL;
  484. }
  485. inline void graph::reset() {
  486. // reset context
  487. if(my_context) my_context->reset();
  488. cancelled = false;
  489. caught_exception = false;
  490. // reset all the nodes comprising the graph
  491. for(iterator ii = begin(); ii != end(); ++ii) {
  492. graph_node *my_p = &(*ii);
  493. my_p->reset();
  494. }
  495. }
  496. #include "internal/_flow_graph_node_impl.h"
  497. //! An executable node that acts as a source, i.e. it has no predecessors
  498. template < typename Output >
  499. class source_node : public graph_node, public sender< Output > {
  500. protected:
  501. using graph_node::my_graph;
  502. public:
  503. //! The type of the output message, which is complete
  504. typedef Output output_type;
  505. //! The type of successors of this node
  506. typedef receiver< Output > successor_type;
  507. //! Constructor for a node with a successor
  508. template< typename Body >
  509. source_node( graph &g, Body body, bool is_active = true )
  510. : graph_node(g), my_root_task(g.root_task()), my_active(is_active), init_my_active(is_active),
  511. my_body( new internal::source_body_leaf< output_type, Body>(body) ),
  512. my_reserved(false), my_has_cached_item(false)
  513. {
  514. my_successors.set_owner(this);
  515. }
  516. //! Copy constructor
  517. source_node( const source_node& src ) :
  518. graph_node(src.my_graph), sender<Output>(),
  519. my_root_task( src.my_root_task), my_active(src.init_my_active),
  520. init_my_active(src.init_my_active), my_body( src.my_body->clone() ),
  521. my_reserved(false), my_has_cached_item(false)
  522. {
  523. my_successors.set_owner(this);
  524. }
  525. //! The destructor
  526. ~source_node() { delete my_body; }
  527. //! Add a new successor to this node
  528. /* override */ bool register_successor( receiver<output_type> &r ) {
  529. spin_mutex::scoped_lock lock(my_mutex);
  530. my_successors.register_successor(r);
  531. if ( my_active )
  532. spawn_put();
  533. return true;
  534. }
  535. //! Removes a successor from this node
  536. /* override */ bool remove_successor( receiver<output_type> &r ) {
  537. spin_mutex::scoped_lock lock(my_mutex);
  538. my_successors.remove_successor(r);
  539. return true;
  540. }
  541. //! Request an item from the node
  542. /*override */ bool try_get( output_type &v ) {
  543. spin_mutex::scoped_lock lock(my_mutex);
  544. if ( my_reserved )
  545. return false;
  546. if ( my_has_cached_item ) {
  547. v = my_cached_item;
  548. my_has_cached_item = false;
  549. return true;
  550. }
  551. // we've been asked to provide an item, but we have none. enqueue a task to
  552. // provide one.
  553. spawn_put();
  554. return false;
  555. }
  556. //! Reserves an item.
  557. /* override */ bool try_reserve( output_type &v ) {
  558. spin_mutex::scoped_lock lock(my_mutex);
  559. if ( my_reserved ) {
  560. return false;
  561. }
  562. if ( my_has_cached_item ) {
  563. v = my_cached_item;
  564. my_reserved = true;
  565. return true;
  566. } else {
  567. return false;
  568. }
  569. }
  570. //! Release a reserved item.
  571. /** true = item has been released and so remains in sender, dest must request or reserve future items */
  572. /* override */ bool try_release( ) {
  573. spin_mutex::scoped_lock lock(my_mutex);
  574. __TBB_ASSERT( my_reserved && my_has_cached_item, "releasing non-existent reservation" );
  575. my_reserved = false;
  576. if(!my_successors.empty())
  577. spawn_put();
  578. return true;
  579. }
  580. //! Consumes a reserved item
  581. /* override */ bool try_consume( ) {
  582. spin_mutex::scoped_lock lock(my_mutex);
  583. __TBB_ASSERT( my_reserved && my_has_cached_item, "consuming non-existent reservation" );
  584. my_reserved = false;
  585. my_has_cached_item = false;
  586. if ( !my_successors.empty() ) {
  587. spawn_put();
  588. }
  589. return true;
  590. }
  591. //! Activates a node that was created in the inactive state
  592. void activate() {
  593. spin_mutex::scoped_lock lock(my_mutex);
  594. my_active = true;
  595. if ( !my_successors.empty() )
  596. spawn_put();
  597. }
  598. template<typename Body>
  599. Body copy_function_object() {
  600. internal::source_body<output_type> &body_ref = *this->my_body;
  601. return dynamic_cast< internal::source_body_leaf<output_type, Body> & >(body_ref).get_body();
  602. }
  603. protected:
  604. //! resets the node to its initial state
  605. void reset() {
  606. my_active = init_my_active;
  607. my_reserved =false;
  608. if(my_has_cached_item) {
  609. my_has_cached_item = false;
  610. }
  611. }
  612. private:
  613. task *my_root_task;
  614. spin_mutex my_mutex;
  615. bool my_active;
  616. bool init_my_active;
  617. internal::source_body<output_type> *my_body;
  618. internal::broadcast_cache< output_type > my_successors;
  619. bool my_reserved;
  620. bool my_has_cached_item;
  621. output_type my_cached_item;
  622. // used by apply_body, can invoke body of node.
  623. bool try_reserve_apply_body(output_type &v) {
  624. spin_mutex::scoped_lock lock(my_mutex);
  625. if ( my_reserved ) {
  626. return false;
  627. }
  628. if ( !my_has_cached_item && (*my_body)(my_cached_item) )
  629. my_has_cached_item = true;
  630. if ( my_has_cached_item ) {
  631. v = my_cached_item;
  632. my_reserved = true;
  633. return true;
  634. } else {
  635. return false;
  636. }
  637. }
  638. //! Spawns a task that applies the body
  639. /* override */ void spawn_put( ) {
  640. task::enqueue( * new ( task::allocate_additional_child_of( *my_root_task ) )
  641. internal:: source_task_bypass < source_node< output_type > >( *this ) );
  642. }
  643. friend class internal::source_task_bypass< source_node< output_type > >;
  644. //! Applies the body. Returning SUCCESSFULLY_ENQUEUED okay; forward_task_bypass will handle it.
  645. /* override */ task * apply_body_bypass( ) {
  646. output_type v;
  647. if ( !try_reserve_apply_body(v) )
  648. return NULL;
  649. task *last_task = my_successors.try_put_task(v);
  650. if ( last_task )
  651. try_consume();
  652. else
  653. try_release();
  654. return last_task;
  655. }
  656. }; // source_node
  657. //! Implements a function node that supports Input -> Output
  658. template < typename Input, typename Output = continue_msg, graph_buffer_policy = queueing, typename Allocator=cache_aligned_allocator<Input> >
  659. class function_node : public graph_node, public internal::function_input<Input,Output,Allocator>, public internal::function_output<Output> {
  660. protected:
  661. using graph_node::my_graph;
  662. public:
  663. typedef Input input_type;
  664. typedef Output output_type;
  665. typedef sender< input_type > predecessor_type;
  666. typedef receiver< output_type > successor_type;
  667. typedef internal::function_input<input_type,output_type,Allocator> fInput_type;
  668. typedef internal::function_output<output_type> fOutput_type;
  669. //! Constructor
  670. template< typename Body >
  671. function_node( graph &g, size_t concurrency, Body body ) :
  672. graph_node(g), internal::function_input<input_type,output_type,Allocator>(g, concurrency, body)
  673. {}
  674. //! Copy constructor
  675. function_node( const function_node& src ) :
  676. graph_node(src.my_graph), internal::function_input<input_type,output_type,Allocator>( src ),
  677. fOutput_type()
  678. {}
  679. protected:
  680. template< typename R, typename B > friend class run_and_put_task;
  681. template<typename X, typename Y> friend class internal::broadcast_cache;
  682. template<typename X, typename Y> friend class internal::round_robin_cache;
  683. using fInput_type::try_put_task;
  684. // override of graph_node's reset.
  685. /*override*/void reset() {fInput_type::reset_function_input(); }
  686. /* override */ internal::broadcast_cache<output_type> &successors () { return fOutput_type::my_successors; }
  687. };
  688. //! Implements a function node that supports Input -> Output
  689. template < typename Input, typename Output, typename Allocator >
  690. class function_node<Input,Output,queueing,Allocator> : public graph_node, public internal::function_input<Input,Output,Allocator>, public internal::function_output<Output> {
  691. protected:
  692. using graph_node::my_graph;
  693. public:
  694. typedef Input input_type;
  695. typedef Output output_type;
  696. typedef sender< input_type > predecessor_type;
  697. typedef receiver< output_type > successor_type;
  698. typedef internal::function_input<input_type,output_type,Allocator> fInput_type;
  699. typedef internal::function_input_queue<input_type, Allocator> queue_type;
  700. typedef internal::function_output<output_type> fOutput_type;
  701. //! Constructor
  702. template< typename Body >
  703. function_node( graph &g, size_t concurrency, Body body ) :
  704. graph_node(g), fInput_type( g, concurrency, body, new queue_type() )
  705. {}
  706. //! Copy constructor
  707. function_node( const function_node& src ) :
  708. graph_node(src.my_graph), fInput_type( src, new queue_type() ), fOutput_type()
  709. {}
  710. protected:
  711. template< typename R, typename B > friend class run_and_put_task;
  712. template<typename X, typename Y> friend class internal::broadcast_cache;
  713. template<typename X, typename Y> friend class internal::round_robin_cache;
  714. using fInput_type::try_put_task;
  715. /*override*/void reset() { fInput_type::reset_function_input(); }
  716. /* override */ internal::broadcast_cache<output_type> &successors () { return fOutput_type::my_successors; }
  717. };
  718. #include "tbb/internal/_flow_graph_types_impl.h"
  719. //! implements a function node that supports Input -> (set of outputs)
  720. // Output is a tuple of output types.
  721. template < typename Input, typename Output, graph_buffer_policy = queueing, typename Allocator=cache_aligned_allocator<Input> >
  722. class multifunction_node :
  723. public graph_node,
  724. public internal::multifunction_input
  725. <
  726. Input,
  727. typename internal::wrap_tuple_elements<
  728. tbb::flow::tuple_size<Output>::value, // #elements in tuple
  729. internal::multifunction_output, // wrap this around each element
  730. Output // the tuple providing the types
  731. >::type,
  732. Allocator
  733. > {
  734. protected:
  735. using graph_node::my_graph;
  736. private:
  737. static const int N = tbb::flow::tuple_size<Output>::value;
  738. public:
  739. typedef Input input_type;
  740. typedef typename internal::wrap_tuple_elements<N,internal::multifunction_output, Output>::type output_ports_type;
  741. private:
  742. typedef typename internal::multifunction_input<input_type, output_ports_type, Allocator> base_type;
  743. typedef typename internal::function_input_queue<input_type,Allocator> queue_type;
  744. public:
  745. template<typename Body>
  746. multifunction_node( graph &g, size_t concurrency, Body body ) :
  747. graph_node(g), base_type(g,concurrency, body)
  748. {}
  749. multifunction_node( const multifunction_node &other) :
  750. graph_node(other.my_graph), base_type(other)
  751. {}
  752. // all the guts are in multifunction_input...
  753. protected:
  754. /*override*/void reset() { base_type::reset(); }
  755. }; // multifunction_node
  756. template < typename Input, typename Output, typename Allocator >
  757. class multifunction_node<Input,Output,queueing,Allocator> : public graph_node, public internal::multifunction_input<Input,
  758. typename internal::wrap_tuple_elements<tbb::flow::tuple_size<Output>::value, internal::multifunction_output, Output>::type, Allocator> {
  759. protected:
  760. using graph_node::my_graph;
  761. static const int N = tbb::flow::tuple_size<Output>::value;
  762. public:
  763. typedef Input input_type;
  764. typedef typename internal::wrap_tuple_elements<N, internal::multifunction_output, Output>::type output_ports_type;
  765. private:
  766. typedef typename internal::multifunction_input<input_type, output_ports_type, Allocator> base_type;
  767. typedef typename internal::function_input_queue<input_type,Allocator> queue_type;
  768. public:
  769. template<typename Body>
  770. multifunction_node( graph &g, size_t concurrency, Body body) :
  771. graph_node(g), base_type(g,concurrency, body, new queue_type())
  772. {}
  773. multifunction_node( const multifunction_node &other) :
  774. graph_node(other.my_graph), base_type(other, new queue_type())
  775. {}
  776. // all the guts are in multifunction_input...
  777. protected:
  778. /*override*/void reset() { base_type::reset(); }
  779. }; // multifunction_node
  780. //! split_node: accepts a tuple as input, forwards each element of the tuple to its
  781. // successors. The node has unlimited concurrency, so though it is marked as
  782. // "rejecting" it does not reject inputs.
  783. template<typename TupleType, typename Allocator=cache_aligned_allocator<TupleType> >
  784. class split_node : public multifunction_node<TupleType, TupleType, rejecting, Allocator> {
  785. static const int N = tbb::flow::tuple_size<TupleType>::value;
  786. typedef multifunction_node<TupleType,TupleType,rejecting,Allocator> base_type;
  787. public:
  788. typedef typename base_type::output_ports_type output_ports_type;
  789. private:
  790. struct splitting_body {
  791. void operator()(const TupleType& t, output_ports_type &p) {
  792. internal::emit_element<N>::emit_this(t, p);
  793. }
  794. };
  795. public:
  796. typedef TupleType input_type;
  797. typedef Allocator allocator_type;
  798. split_node(graph &g) : base_type(g, unlimited, splitting_body()) {}
  799. split_node( const split_node & other) : base_type(other) {}
  800. };
  801. //! Implements an executable node that supports continue_msg -> Output
  802. template <typename Output>
  803. class continue_node : public graph_node, public internal::continue_input<Output>, public internal::function_output<Output> {
  804. protected:
  805. using graph_node::my_graph;
  806. public:
  807. typedef continue_msg input_type;
  808. typedef Output output_type;
  809. typedef sender< input_type > predecessor_type;
  810. typedef receiver< output_type > successor_type;
  811. typedef internal::continue_input<Output> fInput_type;
  812. typedef internal::function_output<output_type> fOutput_type;
  813. //! Constructor for executable node with continue_msg -> Output
  814. template <typename Body >
  815. continue_node( graph &g, Body body ) :
  816. graph_node(g), internal::continue_input<output_type>( g, body )
  817. {}
  818. //! Constructor for executable node with continue_msg -> Output
  819. template <typename Body >
  820. continue_node( graph &g, int number_of_predecessors, Body body ) :
  821. graph_node(g), internal::continue_input<output_type>( g, number_of_predecessors, body )
  822. {}
  823. //! Copy constructor
  824. continue_node( const continue_node& src ) :
  825. graph_node(src.my_graph), internal::continue_input<output_type>(src),
  826. internal::function_output<Output>()
  827. {}
  828. protected:
  829. template< typename R, typename B > friend class run_and_put_task;
  830. template<typename X, typename Y> friend class internal::broadcast_cache;
  831. template<typename X, typename Y> friend class internal::round_robin_cache;
  832. using fInput_type::try_put_task;
  833. /*override*/void reset() { internal::continue_input<Output>::reset_receiver(); }
  834. /* override */ internal::broadcast_cache<output_type> &successors () { return fOutput_type::my_successors; }
  835. };
  836. template< typename T >
  837. class overwrite_node : public graph_node, public receiver<T>, public sender<T> {
  838. protected:
  839. using graph_node::my_graph;
  840. public:
  841. typedef T input_type;
  842. typedef T output_type;
  843. typedef sender< input_type > predecessor_type;
  844. typedef receiver< output_type > successor_type;
  845. overwrite_node(graph &g) : graph_node(g), my_buffer_is_valid(false) {
  846. my_successors.set_owner( this );
  847. }
  848. // Copy constructor; doesn't take anything from src; default won't work
  849. overwrite_node( const overwrite_node& src ) :
  850. graph_node(src.my_graph), receiver<T>(), sender<T>(), my_buffer_is_valid(false)
  851. {
  852. my_successors.set_owner( this );
  853. }
  854. ~overwrite_node() {}
  855. /* override */ bool register_successor( successor_type &s ) {
  856. spin_mutex::scoped_lock l( my_mutex );
  857. if ( my_buffer_is_valid ) {
  858. // We have a valid value that must be forwarded immediately.
  859. if ( s.try_put( my_buffer ) || !s.register_predecessor( *this ) ) {
  860. // We add the successor: it accepted our put or it rejected it but won't let us become a predecessor
  861. my_successors.register_successor( s );
  862. return true;
  863. } else {
  864. // We don't add the successor: it rejected our put and we became its predecessor instead
  865. return false;
  866. }
  867. } else {
  868. // No valid value yet, just add as successor
  869. my_successors.register_successor( s );
  870. return true;
  871. }
  872. }
  873. /* override */ bool remove_successor( successor_type &s ) {
  874. spin_mutex::scoped_lock l( my_mutex );
  875. my_successors.remove_successor(s);
  876. return true;
  877. }
  878. /* override */ bool try_get( T &v ) {
  879. spin_mutex::scoped_lock l( my_mutex );
  880. if ( my_buffer_is_valid ) {
  881. v = my_buffer;
  882. return true;
  883. } else {
  884. return false;
  885. }
  886. }
  887. bool is_valid() {
  888. spin_mutex::scoped_lock l( my_mutex );
  889. return my_buffer_is_valid;
  890. }
  891. void clear() {
  892. spin_mutex::scoped_lock l( my_mutex );
  893. my_buffer_is_valid = false;
  894. }
  895. protected:
  896. template< typename R, typename B > friend class run_and_put_task;
  897. template<typename X, typename Y> friend class internal::broadcast_cache;
  898. template<typename X, typename Y> friend class internal::round_robin_cache;
  899. /* override */ task * try_put_task( const T &v ) {
  900. spin_mutex::scoped_lock l( my_mutex );
  901. my_buffer = v;
  902. my_buffer_is_valid = true;
  903. task * rtask = my_successors.try_put_task(v);
  904. if(!rtask) rtask = SUCCESSFULLY_ENQUEUED;
  905. return rtask;
  906. }
  907. /*override*/void reset() { my_buffer_is_valid = false; }
  908. spin_mutex my_mutex;
  909. internal::broadcast_cache< T, null_rw_mutex > my_successors;
  910. T my_buffer;
  911. bool my_buffer_is_valid;
  912. /*override*/void reset_receiver() {}
  913. };
  914. template< typename T >
  915. class write_once_node : public overwrite_node<T> {
  916. public:
  917. typedef T input_type;
  918. typedef T output_type;
  919. typedef sender< input_type > predecessor_type;
  920. typedef receiver< output_type > successor_type;
  921. //! Constructor
  922. write_once_node(graph& g) : overwrite_node<T>(g) {}
  923. //! Copy constructor: call base class copy constructor
  924. write_once_node( const write_once_node& src ) : overwrite_node<T>(src) {}
  925. protected:
  926. template< typename R, typename B > friend class run_and_put_task;
  927. template<typename X, typename Y> friend class internal::broadcast_cache;
  928. template<typename X, typename Y> friend class internal::round_robin_cache;
  929. /* override */ task *try_put_task( const T &v ) {
  930. spin_mutex::scoped_lock l( this->my_mutex );
  931. if ( this->my_buffer_is_valid ) {
  932. return NULL;
  933. } else {
  934. this->my_buffer = v;
  935. this->my_buffer_is_valid = true;
  936. task *res = this->my_successors.try_put_task(v);
  937. if(!res) res = SUCCESSFULLY_ENQUEUED;
  938. return res;
  939. }
  940. }
  941. };
  942. //! Forwards messages of type T to all successors
  943. template <typename T>
  944. class broadcast_node : public graph_node, public receiver<T>, public sender<T> {
  945. protected:
  946. using graph_node::my_graph;
  947. private:
  948. internal::broadcast_cache<T> my_successors;
  949. public:
  950. typedef T input_type;
  951. typedef T output_type;
  952. typedef sender< input_type > predecessor_type;
  953. typedef receiver< output_type > successor_type;
  954. broadcast_node(graph& g) : graph_node(g) {
  955. my_successors.set_owner( this );
  956. }
  957. // Copy constructor
  958. broadcast_node( const broadcast_node& src ) :
  959. graph_node(src.my_graph), receiver<T>(), sender<T>()
  960. {
  961. my_successors.set_owner( this );
  962. }
  963. //! Adds a successor
  964. virtual bool register_successor( receiver<T> &r ) {
  965. my_successors.register_successor( r );
  966. return true;
  967. }
  968. //! Removes s as a successor
  969. virtual bool remove_successor( receiver<T> &r ) {
  970. my_successors.remove_successor( r );
  971. return true;
  972. }
  973. protected:
  974. template< typename R, typename B > friend class run_and_put_task;
  975. template<typename X, typename Y> friend class internal::broadcast_cache;
  976. template<typename X, typename Y> friend class internal::round_robin_cache;
  977. //! build a task to run the successor if possible. Default is old behavior.
  978. /*override*/ task *try_put_task(const T& t) {
  979. task *new_task = my_successors.try_put_task(t);
  980. if(!new_task) new_task = SUCCESSFULLY_ENQUEUED;
  981. return new_task;
  982. }
  983. /*override*/void reset() {}
  984. /*override*/void reset_receiver() {}
  985. }; // broadcast_node
  986. #include "internal/_flow_graph_item_buffer_impl.h"
  987. //! Forwards messages in arbitrary order
  988. template <typename T, typename A=cache_aligned_allocator<T> >
  989. class buffer_node : public graph_node, public reservable_item_buffer<T, A>, public receiver<T>, public sender<T> {
  990. protected:
  991. using graph_node::my_graph;
  992. public:
  993. typedef T input_type;
  994. typedef T output_type;
  995. typedef sender< input_type > predecessor_type;
  996. typedef receiver< output_type > successor_type;
  997. typedef buffer_node<T, A> my_class;
  998. protected:
  999. typedef size_t size_type;
  1000. internal::round_robin_cache< T, null_rw_mutex > my_successors;
  1001. task *my_parent;
  1002. friend class internal::forward_task_bypass< buffer_node< T, A > >;
  1003. enum op_type {reg_succ, rem_succ, req_item, res_item, rel_res, con_res, put_item, try_fwd_task };
  1004. enum op_stat {WAIT=0, SUCCEEDED, FAILED};
  1005. // implements the aggregator_operation concept
  1006. class buffer_operation : public internal::aggregated_operation< buffer_operation > {
  1007. public:
  1008. char type;
  1009. T *elem;
  1010. task * ltask;
  1011. successor_type *r;
  1012. buffer_operation(const T& e, op_type t) : type(char(t)), elem(const_cast<T*>(&e)) , ltask(NULL) , r(NULL) {}
  1013. buffer_operation(op_type t) : type(char(t)) , ltask(NULL) , r(NULL) {}
  1014. };
  1015. bool forwarder_busy;
  1016. typedef internal::aggregating_functor<my_class, buffer_operation> my_handler;
  1017. friend class internal::aggregating_functor<my_class, buffer_operation>;
  1018. internal::aggregator< my_handler, buffer_operation> my_aggregator;
  1019. virtual void handle_operations(buffer_operation *op_list) {
  1020. buffer_operation *tmp = NULL;
  1021. bool try_forwarding=false;
  1022. while (op_list) {
  1023. tmp = op_list;
  1024. op_list = op_list->next;
  1025. switch (tmp->type) {
  1026. case reg_succ: internal_reg_succ(tmp); try_forwarding = true; break;
  1027. case rem_succ: internal_rem_succ(tmp); break;
  1028. case req_item: internal_pop(tmp); break;
  1029. case res_item: internal_reserve(tmp); break;
  1030. case rel_res: internal_release(tmp); try_forwarding = true; break;
  1031. case con_res: internal_consume(tmp); try_forwarding = true; break;
  1032. case put_item: internal_push(tmp); try_forwarding = true; break;
  1033. case try_fwd_task: internal_forward_task(tmp); break;
  1034. }
  1035. }
  1036. if (try_forwarding && !forwarder_busy) {
  1037. forwarder_busy = true;
  1038. task *new_task = new(task::allocate_additional_child_of(*my_parent)) internal::
  1039. forward_task_bypass
  1040. < buffer_node<input_type, A> >(*this);
  1041. // tmp should point to the last item handled by the aggregator. This is the operation
  1042. // the handling thread enqueued. So modifying that record will be okay.
  1043. tbb::task *z = tmp->ltask;
  1044. tmp->ltask = combine_tasks(z, new_task); // in case the op generated a task
  1045. }
  1046. }
  1047. inline task *grab_forwarding_task( buffer_operation &op_data) {
  1048. return op_data.ltask;
  1049. }
  1050. inline bool enqueue_forwarding_task(buffer_operation &op_data) {
  1051. task *ft = grab_forwarding_task(op_data);
  1052. if(ft) {
  1053. task::enqueue(*ft);
  1054. return true;
  1055. }
  1056. return false;
  1057. }
  1058. //! This is executed by an enqueued task, the "forwarder"
  1059. virtual task *forward_task() {
  1060. buffer_operation op_data(try_fwd_task);
  1061. task *last_task = NULL;
  1062. do {
  1063. op_data.status = WAIT;
  1064. op_data.ltask = NULL;
  1065. my_aggregator.execute(&op_data);
  1066. tbb::task *xtask = op_data.ltask;
  1067. last_task = combine_tasks(last_task, xtask);
  1068. } while (op_data.status == SUCCEEDED);
  1069. return last_task;
  1070. }
  1071. //! Register successor
  1072. virtual void internal_reg_succ(buffer_operation *op) {
  1073. my_successors.register_successor(*(op->r));
  1074. __TBB_store_with_release(op->status, SUCCEEDED);
  1075. }
  1076. //! Remove successor
  1077. virtual void internal_rem_succ(buffer_operation *op) {
  1078. my_successors.remove_successor(*(op->r));
  1079. __TBB_store_with_release(op->status, SUCCEEDED);
  1080. }
  1081. //! Tries to forward valid items to successors
  1082. virtual void internal_forward_task(buffer_operation *op) {
  1083. if (this->my_reserved || !this->item_valid(this->my_tail-1)) {
  1084. __TBB_store_with_release(op->status, FAILED);
  1085. this->forwarder_busy = false;
  1086. return;
  1087. }
  1088. T i_copy;
  1089. task * last_task = NULL;
  1090. size_type counter = my_successors.size();
  1091. // Try forwarding, giving each successor a chance
  1092. while (counter>0 && !this->buffer_empty() && this->item_valid(this->my_tail-1)) {
  1093. this->fetch_back(i_copy);
  1094. task *new_task = my_successors.try_put_task(i_copy);
  1095. last_task = combine_tasks(last_task, new_task);
  1096. if(new_task) {
  1097. this->invalidate_back();
  1098. --(this->my_tail);
  1099. }
  1100. --counter;
  1101. }
  1102. op->ltask = last_task; // return task
  1103. if (last_task && !counter) {
  1104. __TBB_store_with_release(op->status, SUCCEEDED);
  1105. }
  1106. else {
  1107. __TBB_store_with_release(op->status, FAILED);
  1108. forwarder_busy = false;
  1109. }
  1110. }
  1111. virtual void internal_push(buffer_operation *op) {
  1112. this->push_back(*(op->elem));
  1113. __TBB_store_with_release(op->status, SUCCEEDED);
  1114. }
  1115. virtual void internal_pop(buffer_operation *op) {
  1116. if(this->pop_back(*(op->elem))) {
  1117. __TBB_store_with_release(op->status, SUCCEEDED);
  1118. }
  1119. else {
  1120. __TBB_store_with_release(op->status, FAILED);
  1121. }
  1122. }
  1123. virtual void internal_reserve(buffer_operation *op) {
  1124. if(this->reserve_front(*(op->elem))) {
  1125. __TBB_store_with_release(op->status, SUCCEEDED);
  1126. }
  1127. else {
  1128. __TBB_store_with_release(op->status, FAILED);
  1129. }
  1130. }
  1131. virtual void internal_consume(buffer_operation *op) {
  1132. this->consume_front();
  1133. __TBB_store_with_release(op->status, SUCCEEDED);
  1134. }
  1135. virtual void internal_release(buffer_operation *op) {
  1136. this->release_front();
  1137. __TBB_store_with_release(op->status, SUCCEEDED);
  1138. }
  1139. public:
  1140. //! Constructor
  1141. buffer_node( graph &g ) : graph_node(g), reservable_item_buffer<T>(),
  1142. my_parent( g.root_task() ), forwarder_busy(false) {
  1143. my_successors.set_owner(this);
  1144. my_aggregator.initialize_handler(my_handler(this));
  1145. }
  1146. //! Copy constructor
  1147. buffer_node( const buffer_node& src ) : graph_node(src.my_graph),
  1148. reservable_item_buffer<T>(), receiver<T>(), sender<T>(),
  1149. my_parent( src.my_parent ) {
  1150. forwarder_busy = false;
  1151. my_successors.set_owner(this);
  1152. my_aggregator.initialize_handler(my_handler(this));
  1153. }
  1154. virtual ~buffer_node() {}
  1155. //
  1156. // message sender implementation
  1157. //
  1158. //! Adds a new successor.
  1159. /** Adds successor r to the list of successors; may forward tasks. */
  1160. /* override */ bool register_successor( receiver<output_type> &r ) {
  1161. buffer_operation op_data(reg_succ);
  1162. op_data.r = &r;
  1163. my_aggregator.execute(&op_data);
  1164. (void)enqueue_forwarding_task(op_data);
  1165. return true;
  1166. }
  1167. //! Removes a successor.
  1168. /** Removes successor r from the list of successors.
  1169. It also calls r.remove_predecessor(*this) to remove this node as a predecessor. */
  1170. /* override */ bool remove_successor( receiver<output_type> &r ) {
  1171. r.remove_predecessor(*this);
  1172. buffer_operation op_data(rem_succ);
  1173. op_data.r = &r;
  1174. my_aggregator.execute(&op_data);
  1175. // even though this operation does not cause a forward, if we are the handler, and
  1176. // a forward is scheduled, we may be the first to reach this point after the aggregator,
  1177. // and so should check for the task.
  1178. (void)enqueue_forwarding_task(op_data);
  1179. return true;
  1180. }
  1181. //! Request an item from the buffer_node
  1182. /** true = v contains the returned item<BR>
  1183. false = no item has been returned */
  1184. /* override */ bool try_get( T &v ) {
  1185. buffer_operation op_data(req_item);
  1186. op_data.elem = &v;
  1187. my_aggregator.execute(&op_data);
  1188. (void)enqueue_forwarding_task(op_data);
  1189. return (op_data.status==SUCCEEDED);
  1190. }
  1191. //! Reserves an item.
  1192. /** false = no item can be reserved<BR>
  1193. true = an item is reserved */
  1194. /* override */ bool try_reserve( T &v ) {
  1195. buffer_operation op_data(res_item);
  1196. op_data.elem = &v;
  1197. my_aggregator.execute(&op_data);
  1198. (void)enqueue_forwarding_task(op_data);
  1199. return (op_data.status==SUCCEEDED);
  1200. }
  1201. //! Release a reserved item.
  1202. /** true = item has been released and so remains in sender */
  1203. /* override */ bool try_release() {
  1204. buffer_operation op_data(rel_res);
  1205. my_aggregator.execute(&op_data);
  1206. (void)enqueue_forwarding_task(op_data);
  1207. return true;
  1208. }
  1209. //! Consumes a reserved item.
  1210. /** true = item is removed from sender and reservation removed */
  1211. /* override */ bool try_consume() {
  1212. buffer_operation op_data(con_res);
  1213. my_aggregator.execute(&op_data);
  1214. (void)enqueue_forwarding_task(op_data);
  1215. return true;
  1216. }
  1217. protected:
  1218. template< typename R, typename B > friend class run_and_put_task;
  1219. template<typename X, typename Y> friend class internal::broadcast_cache;
  1220. template<typename X, typename Y> friend class internal::round_robin_cache;
  1221. //! receive an item, return a task *if possible
  1222. /* override */ task *try_put_task(const T &t) {
  1223. buffer_operation op_data(t, put_item);
  1224. my_aggregator.execute(&op_data);
  1225. task *ft = grab_forwarding_task(op_data);
  1226. if(!ft) {
  1227. ft = SUCCESSFULLY_ENQUEUED;
  1228. }
  1229. return ft;
  1230. }
  1231. /*override*/void reset() {
  1232. reservable_item_buffer<T, A>::reset();
  1233. forwarder_busy = false;
  1234. }
  1235. /*override*/void reset_receiver() {
  1236. // nothing to do; no predecesor_cache
  1237. }
  1238. }; // buffer_node
  1239. //! Forwards messages in FIFO order
  1240. template <typename T, typename A=cache_aligned_allocator<T> >
  1241. class queue_node : public buffer_node<T, A> {
  1242. protected:
  1243. typedef typename buffer_node<T, A>::size_type size_type;
  1244. typedef typename buffer_node<T, A>::buffer_operation queue_operation;
  1245. enum op_stat {WAIT=0, SUCCEEDED, FAILED};
  1246. /* override */ void internal_forward_task(queue_operation *op) {
  1247. if (this->my_reserved || !this->item_valid(this->my_head)) {
  1248. __TBB_store_with_release(op->status, FAILED);
  1249. this->forwarder_busy = false;
  1250. return;
  1251. }
  1252. T i_copy;
  1253. task *last_task = NULL;
  1254. size_type counter = this->my_successors.size();
  1255. // Keep trying to send items while there is at least one accepting successor
  1256. while (counter>0 && this->item_valid(this->my_head)) {
  1257. this->fetch_front(i_copy);
  1258. task *new_task = this->my_successors.try_put_task(i_copy);
  1259. if(new_task) {
  1260. this->invalidate_front();
  1261. ++(this->my_head);
  1262. last_task = combine_tasks(last_task, new_task);
  1263. }
  1264. --counter;
  1265. }
  1266. op->ltask = last_task;
  1267. if (last_task && !counter)
  1268. __TBB_store_with_release(op->status, SUCCEEDED);
  1269. else {
  1270. __TBB_store_with_release(op->status, FAILED);
  1271. this->forwarder_busy = false;
  1272. }
  1273. }
  1274. /* override */ void internal_pop(queue_operation *op) {
  1275. if ( this->my_reserved || !this->item_valid(this->my_head)){
  1276. __TBB_store_with_release(op->status, FAILED);
  1277. }
  1278. else {
  1279. this->pop_front(*(op->elem));
  1280. __TBB_store_with_release(op->status, SUCCEEDED);
  1281. }
  1282. }
  1283. /* override */ void internal_reserve(queue_operation *op) {
  1284. if (this->my_reserved || !this->item_valid(this->my_head)) {
  1285. __TBB_store_with_release(op->status, FAILED);
  1286. }
  1287. else {
  1288. this->my_reserved = true;
  1289. this->fetch_front(*(op->elem));
  1290. this->invalidate_front();
  1291. __TBB_store_with_release(op->status, SUCCEEDED);
  1292. }
  1293. }
  1294. /* override */ void internal_consume(queue_operation *op) {
  1295. this->consume_front();
  1296. __TBB_store_with_release(op->status, SUCCEEDED);
  1297. }
  1298. public:
  1299. typedef T input_type;
  1300. typedef T output_type;
  1301. typedef sender< input_type > predecessor_type;
  1302. typedef receiver< output_type > successor_type;
  1303. //! Constructor
  1304. queue_node( graph &g ) : buffer_node<T, A>(g) {}
  1305. //! Copy constructor
  1306. queue_node( const queue_node& src) : buffer_node<T, A>(src) {}
  1307. };
  1308. //! Forwards messages in sequence order
  1309. template< typename T, typename A=cache_aligned_allocator<T> >
  1310. class sequencer_node : public queue_node<T, A> {
  1311. internal::function_body< T, size_t > *my_sequencer;
  1312. public:
  1313. typedef T input_type;
  1314. typedef T output_type;
  1315. typedef sender< input_type > predecessor_type;
  1316. typedef receiver< output_type > successor_type;
  1317. //! Constructor
  1318. template< typename Sequencer >
  1319. sequencer_node( graph &g, const Sequencer& s ) : queue_node<T, A>(g),
  1320. my_sequencer(new internal::function_body_leaf< T, size_t, Sequencer>(s) ) {}
  1321. //! Copy constructor
  1322. sequencer_node( const sequencer_node& src ) : queue_node<T, A>(src),
  1323. my_sequencer( src.my_sequencer->clone() ) {}
  1324. //! Destructor
  1325. ~sequencer_node() { delete my_sequencer; }
  1326. protected:
  1327. typedef typename buffer_node<T, A>::size_type size_type;
  1328. typedef typename buffer_node<T, A>::buffer_operation sequencer_operation;
  1329. enum op_stat {WAIT=0, SUCCEEDED, FAILED};
  1330. private:
  1331. /* override */ void internal_push(sequencer_operation *op) {
  1332. size_type tag = (*my_sequencer)(*(op->elem));
  1333. this->my_tail = (tag+1 > this->my_tail) ? tag+1 : this->my_tail;
  1334. if(this->size() > this->capacity())
  1335. this->grow_my_array(this->size()); // tail already has 1 added to it
  1336. this->item(tag) = std::make_pair( *(op->elem), true );
  1337. __TBB_store_with_release(op->status, SUCCEEDED);
  1338. }
  1339. };
  1340. //! Forwards messages in priority order
  1341. template< typename T, typename Compare = std::less<T>, typename A=cache_aligned_allocator<T> >
  1342. class priority_queue_node : public buffer_node<T, A> {
  1343. public:
  1344. typedef T input_type;
  1345. typedef T output_type;
  1346. typedef buffer_node<T,A> base_type;
  1347. typedef sender< input_type > predecessor_type;
  1348. typedef receiver< output_type > successor_type;
  1349. //! Constructor
  1350. priority_queue_node( graph &g ) : buffer_node<T, A>(g), mark(0) {}
  1351. //! Copy constructor
  1352. priority_queue_node( const priority_queue_node &src ) : buffer_node<T, A>(src), mark(0) {}
  1353. protected:
  1354. /*override*/void reset() {
  1355. mark = 0;
  1356. base_type::reset();
  1357. }
  1358. typedef typename buffer_node<T, A>::size_type size_type;
  1359. typedef typename buffer_node<T, A>::item_type item_type;
  1360. typedef typename buffer_node<T, A>::buffer_operation prio_operation;
  1361. enum op_stat {WAIT=0, SUCCEEDED, FAILED};
  1362. /* override */ void handle_operations(prio_operation *op_list) {
  1363. prio_operation *tmp = op_list /*, *pop_list*/ ;
  1364. bool try_forwarding=false;
  1365. while (op_list) {
  1366. tmp = op_list;
  1367. op_list = op_list->next;
  1368. switch (tmp->type) {
  1369. case buffer_node<T, A>::reg_succ: this->internal_reg_succ(tmp); try_forwarding = true; break;
  1370. case buffer_node<T, A>::rem_succ: this->internal_rem_succ(tmp); break;
  1371. case buffer_node<T, A>::put_item: internal_push(tmp); try_forwarding = true; break;
  1372. case buffer_node<T, A>::try_fwd_task: internal_forward_task(tmp); break;
  1373. case buffer_node<T, A>::rel_res: internal_release(tmp); try_forwarding = true; break;
  1374. case buffer_node<T, A>::con_res: internal_consume(tmp); try_forwarding = true; break;
  1375. case buffer_node<T, A>::req_item: internal_pop(tmp); break;
  1376. case buffer_node<T, A>::res_item: internal_reserve(tmp); break;
  1377. }
  1378. }
  1379. // process pops! for now, no special pop processing
  1380. if (mark<this->my_tail) heapify();
  1381. if (try_forwarding && !this->forwarder_busy) {
  1382. this->forwarder_busy = true;
  1383. task *new_task = new(task::allocate_additional_child_of(*(this->my_parent))) internal::
  1384. forward_task_bypass
  1385. < buffer_node<input_type, A> >(*this);
  1386. // tmp should point to the last item handled by the aggregator. This is the operation
  1387. // the handling thread enqueued. So modifying that record will be okay.
  1388. tbb::task *tmp1 = tmp->ltask;
  1389. tmp->ltask = combine_tasks(tmp1, new_task);
  1390. }
  1391. }
  1392. //! Tries to forward valid items to successors
  1393. /* override */ void internal_forward_task(prio_operation *op) {
  1394. T i_copy;
  1395. task * last_task = NULL; // flagged when a successor accepts
  1396. size_type counter = this->my_successors.size();
  1397. if (this->my_reserved || this->my_tail == 0) {
  1398. __TBB_store_with_release(op->status, FAILED);
  1399. this->forwarder_busy = false;
  1400. return;
  1401. }
  1402. // Keep trying to send while there exists an accepting successor
  1403. while (counter>0 && this->my_tail > 0) {
  1404. i_copy = this->my_array[0].first;
  1405. task * new_task = this->my_successors.try_put_task(i_copy);
  1406. last_task = combine_tasks(last_task, new_task);
  1407. if ( new_task ) {
  1408. if (mark == this->my_tail) --mark;
  1409. --(this->my_tail);
  1410. this->my_array[0].first=this->my_array[this->my_tail].first;
  1411. if (this->my_tail > 1) // don't reheap for heap of size 1
  1412. reheap();
  1413. }
  1414. --counter;
  1415. }
  1416. op->ltask = last_task;
  1417. if (last_task && !counter)
  1418. __TBB_store_with_release(op->status, SUCCEEDED);
  1419. else {
  1420. __TBB_store_with_release(op->status, FAILED);
  1421. this->forwarder_busy = false;
  1422. }
  1423. }
  1424. /* override */ void internal_push(prio_operation *op) {
  1425. if ( this->my_tail >= this->my_array_size )
  1426. this->grow_my_array( this->my_tail + 1 );
  1427. this->my_array[this->my_tail] = std::make_pair( *(op->elem), true );
  1428. ++(this->my_tail);
  1429. __TBB_store_with_release(op->status, SUCCEEDED);
  1430. }
  1431. /* override */ void internal_pop(prio_operation *op) {
  1432. if ( this->my_reserved == true || this->my_tail == 0 ) {
  1433. __TBB_store_with_release(op->status, FAILED);
  1434. }
  1435. else {
  1436. if (mark<this->my_tail &&
  1437. compare(this->my_array[0].first,
  1438. this->my_array[this->my_tail-1].first)) {
  1439. // there are newly pushed elems; last one higher than top
  1440. // copy the data
  1441. *(op->elem) = this->my_array[this->my_tail-1].first;
  1442. --(this->my_tail);
  1443. __TBB_store_with_release(op->status, SUCCEEDED);
  1444. }
  1445. else { // extract and push the last element down heap
  1446. *(op->elem) = this->my_array[0].first; // copy the data
  1447. if (mark == this->my_tail) --mark;
  1448. --(this->my_tail);
  1449. __TBB_store_with_release(op->status, SUCCEEDED);
  1450. this->my_array[0].first=this->my_array[this->my_tail].first;
  1451. if (this->my_tail > 1) // don't reheap for heap of size 1
  1452. reheap();
  1453. }
  1454. }
  1455. }
  1456. /* override */ void internal_reserve(prio_operation *op) {
  1457. if (this->my_reserved == true || this->my_tail == 0) {
  1458. __TBB_store_with_release(op->status, FAILED);
  1459. }
  1460. else {
  1461. this->my_reserved = true;
  1462. *(op->elem) = reserved_item = this->my_array[0].first;
  1463. if (mark == this->my_tail) --mark;
  1464. --(this->my_tail);
  1465. __TBB_store_with_release(op->status, SUCCEEDED);
  1466. this->my_array[0].first = this->my_array[this->my_tail].first;
  1467. if (this->my_tail > 1) // don't reheap for heap of size 1
  1468. reheap();
  1469. }
  1470. }
  1471. /* override */ void internal_consume(prio_operation *op) {
  1472. this->my_reserved = false;
  1473. __TBB_store_with_release(op->status, SUCCEEDED);
  1474. }
  1475. /* override */ void internal_release(prio_operation *op) {
  1476. if (this->my_tail >= this->my_array_size)
  1477. this->grow_my_array( this->my_tail + 1 );
  1478. this->my_array[this->my_tail] = std::make_pair(reserved_item, true);
  1479. ++(this->my_tail);
  1480. this->my_reserved = false;
  1481. __TBB_store_with_release(op->status, SUCCEEDED);
  1482. heapify();
  1483. }
  1484. private:
  1485. Compare compare;
  1486. size_type mark;
  1487. input_type reserved_item;
  1488. void heapify() {
  1489. if (!mark) mark = 1;
  1490. for (; mark<this->my_tail; ++mark) { // for each unheaped element
  1491. size_type cur_pos = mark;
  1492. input_type to_place = this->my_array[mark].first;
  1493. do { // push to_place up the heap
  1494. size_type parent = (cur_pos-1)>>1;
  1495. if (!compare(this->my_array[parent].first, to_place))
  1496. break;
  1497. this->my_array[cur_pos].first = this->my_array[parent].first;
  1498. cur_pos = parent;
  1499. } while( cur_pos );
  1500. this->my_array[cur_pos].first = to_place;
  1501. }
  1502. }
  1503. void reheap() {
  1504. size_type cur_pos=0, child=1;
  1505. while (child < mark) {
  1506. size_type target = child;
  1507. if (child+1<mark &&
  1508. compare(this->my_array[child].first,
  1509. this->my_array[child+1].first))
  1510. ++target;
  1511. // target now has the higher priority child
  1512. if (compare(this->my_array[target].first,
  1513. this->my_array[this->my_tail].first))
  1514. break;
  1515. this->my_array[cur_pos].first = this->my_array[target].first;
  1516. cur_pos = target;
  1517. child = (cur_pos<<1)+1;
  1518. }
  1519. this->my_array[cur_pos].first = this->my_array[this->my_tail].first;
  1520. }
  1521. };
  1522. //! Forwards messages only if the threshold has not been reached
  1523. /** This node forwards items until its threshold is reached.
  1524. It contains no buffering. If the downstream node rejects, the
  1525. message is dropped. */
  1526. template< typename T >
  1527. class limiter_node : public graph_node, public receiver< T >, public sender< T > {
  1528. protected:
  1529. using graph_node::my_graph;
  1530. public:
  1531. typedef T input_type;
  1532. typedef T output_type;
  1533. typedef sender< input_type > predecessor_type;
  1534. typedef receiver< output_type > successor_type;
  1535. private:
  1536. task *my_root_task;
  1537. size_t my_threshold;
  1538. size_t my_count;
  1539. internal::predecessor_cache< T > my_predecessors;
  1540. spin_mutex my_mutex;
  1541. internal::broadcast_cache< T > my_successors;
  1542. int init_decrement_predecessors;
  1543. friend class internal::forward_task_bypass< limiter_node<T> >;
  1544. // Let decrementer call decrement_counter()
  1545. friend class internal::decrementer< limiter_node<T> >;
  1546. // only returns a valid task pointer or NULL, never SUCCESSFULLY_ENQUEUED
  1547. task * decrement_counter() {
  1548. input_type v;
  1549. task *rval = NULL;
  1550. // If we can't get / put an item immediately then drop the count
  1551. if ( my_predecessors.get_item( v ) == false
  1552. || (rval = my_successors.try_put_task(v)) == NULL ) {
  1553. spin_mutex::scoped_lock lock(my_mutex);
  1554. --my_count;
  1555. if ( !my_predecessors.empty() ) {
  1556. task *rtask = new ( task::allocate_additional_child_of( *my_root_task ) )
  1557. internal::forward_task_bypass< limiter_node<T> >( *this );
  1558. __TBB_ASSERT(!rval, "Have two tasks to handle");
  1559. return rtask;
  1560. }
  1561. }
  1562. return rval;
  1563. }
  1564. void forward() {
  1565. {
  1566. spin_mutex::scoped_lock lock(my_mutex);
  1567. if ( my_count < my_threshold )
  1568. ++my_count;
  1569. else
  1570. return;
  1571. }
  1572. task * rtask = decrement_counter();
  1573. if(rtask) task::enqueue(*rtask);
  1574. }
  1575. task *forward_task() {
  1576. spin_mutex::scoped_lock lock(my_mutex);
  1577. if ( my_count >= my_threshold )
  1578. return NULL;
  1579. ++my_count;
  1580. task * rtask = decrement_counter();
  1581. return rtask;
  1582. }
  1583. public:
  1584. //! The internal receiver< continue_msg > that decrements the count
  1585. internal::decrementer< limiter_node<T> > decrement;
  1586. //! Constructor
  1587. limiter_node(graph &g, size_t threshold, int num_decrement_predecessors=0) :
  1588. graph_node(g), my_root_task(g.root_task()), my_threshold(threshold), my_count(0),
  1589. init_decrement_predecessors(num_decrement_predecessors),
  1590. decrement(num_decrement_predecessors)
  1591. {
  1592. my_predecessors.set_owner(this);
  1593. my_successors.set_owner(this);
  1594. decrement.set_owner(this);
  1595. }
  1596. //! Copy constructor
  1597. limiter_node( const limiter_node& src ) :
  1598. graph_node(src.my_graph), receiver<T>(), sender<T>(),
  1599. my_root_task(src.my_root_task), my_threshold(src.my_threshold), my_count(0),
  1600. init_decrement_predecessors(src.init_decrement_predecessors),
  1601. decrement(src.init_decrement_predecessors)
  1602. {
  1603. my_predecessors.set_owner(this);
  1604. my_successors.set_owner(this);
  1605. decrement.set_owner(this);
  1606. }
  1607. //! Replace the current successor with this new successor
  1608. /* override */ bool register_successor( receiver<output_type> &r ) {
  1609. my_successors.register_successor(r);
  1610. return true;
  1611. }
  1612. //! Removes a successor from this node
  1613. /** r.remove_predecessor(*this) is also called. */
  1614. /* override */ bool remove_successor( receiver<output_type> &r ) {
  1615. r.remove_predecessor(*this);
  1616. my_successors.remove_successor(r);
  1617. return true;
  1618. }
  1619. //! Removes src from the list of cached predecessors.
  1620. /* override */ bool register_predecessor( predecessor_type &src ) {
  1621. spin_mutex::scoped_lock lock(my_mutex);
  1622. my_predecessors.add( src );
  1623. if ( my_count < my_threshold && !my_successors.empty() ) {
  1624. task::enqueue( * new ( task::allocate_additional_child_of( *my_root_task ) )
  1625. internal::
  1626. forward_task_bypass
  1627. < limiter_node<T> >( *this ) );
  1628. }
  1629. return true;
  1630. }
  1631. //! Removes src from the list of cached predecessors.
  1632. /* override */ bool remove_predecessor( predecessor_type &src ) {
  1633. my_predecessors.remove( src );
  1634. return true;
  1635. }
  1636. protected:
  1637. template< typename R, typename B > friend class run_and_put_task;
  1638. template<typename X, typename Y> friend class internal::broadcast_cache;
  1639. template<typename X, typename Y> friend class internal::round_robin_cache;
  1640. //! Puts an item to this receiver
  1641. /* override */ task *try_put_task( const T &t ) {
  1642. {
  1643. spin_mutex::scoped_lock lock(my_mutex);
  1644. if ( my_count >= my_threshold )
  1645. return NULL;
  1646. else
  1647. ++my_count;
  1648. }
  1649. task * rtask = my_successors.try_put_task(t);
  1650. if ( !rtask ) { // try_put_task failed.
  1651. spin_mutex::scoped_lock lock(my_mutex);
  1652. --my_count;
  1653. if ( !my_predecessors.empty() ) {
  1654. rtask = new ( task::allocate_additional_child_of( *my_root_task ) )
  1655. internal::forward_task_bypass< limiter_node<T> >( *this );
  1656. }
  1657. }
  1658. return rtask;
  1659. }
  1660. /*override*/void reset() {
  1661. my_count = 0;
  1662. my_predecessors.reset();
  1663. decrement.reset_receiver();
  1664. }
  1665. /*override*/void reset_receiver() { my_predecessors.reset(); }
  1666. }; // limiter_node
  1667. #include "internal/_flow_graph_join_impl.h"
  1668. using internal::reserving_port;
  1669. using internal::queueing_port;
  1670. using internal::tag_matching_port;
  1671. using internal::input_port;
  1672. using internal::tag_value;
  1673. using internal::NO_TAG;
  1674. template<typename OutputTuple, graph_buffer_policy JP=queueing> class join_node;
  1675. template<typename OutputTuple>
  1676. class join_node<OutputTuple,reserving>: public internal::unfolded_join_node<tbb::flow::tuple_size<OutputTuple>::value, reserving_port, OutputTuple, reserving> {
  1677. private:
  1678. static const int N = tbb::flow::tuple_size<OutputTuple>::value;
  1679. typedef typename internal::unfolded_join_node<N, reserving_port, OutputTuple, reserving> unfolded_type;
  1680. public:
  1681. typedef OutputTuple output_type;
  1682. typedef typename unfolded_type::input_ports_type input_ports_type;
  1683. join_node(graph &g) : unfolded_type(g) { }
  1684. join_node(const join_node &other) : unfolded_type(other) {}
  1685. };
  1686. template<typename OutputTuple>
  1687. class join_node<OutputTuple,queueing>: public internal::unfolded_join_node<tbb::flow::tuple_size<OutputTuple>::value, queueing_port, OutputTuple, queueing> {
  1688. private:
  1689. static const int N = tbb::flow::tuple_size<OutputTuple>::value;
  1690. typedef typename internal::unfolded_join_node<N, queueing_port, OutputTuple, queueing> unfolded_type;
  1691. public:
  1692. typedef OutputTuple output_type;
  1693. typedef typename unfolded_type::input_ports_type input_ports_type;
  1694. join_node(graph &g) : unfolded_type(g) { }
  1695. join_node(const join_node &other) : unfolded_type(other) {}
  1696. };
  1697. // template for tag_matching join_node
  1698. template<typename OutputTuple>
  1699. class join_node<OutputTuple, tag_matching> : public internal::unfolded_join_node<tbb::flow::tuple_size<OutputTuple>::value,
  1700. tag_matching_port, OutputTuple, tag_matching> {
  1701. private:
  1702. static const int N = tbb::flow::tuple_size<OutputTuple>::value;
  1703. typedef typename internal::unfolded_join_node<N, tag_matching_port, OutputTuple, tag_matching> unfolded_type;
  1704. public:
  1705. typedef OutputTuple output_type;
  1706. typedef typename unfolded_type::input_ports_type input_ports_type;
  1707. template<typename B0, typename B1>
  1708. join_node(graph &g, B0 b0, B1 b1) : unfolded_type(g, b0, b1) { }
  1709. template<typename B0, typename B1, typename B2>
  1710. join_node(graph &g, B0 b0, B1 b1, B2 b2) : unfolded_type(g, b0, b1, b2) { }
  1711. template<typename B0, typename B1, typename B2, typename B3>
  1712. join_node(graph &g, B0 b0, B1 b1, B2 b2, B3 b3) : unfolded_type(g, b0, b1, b2, b3) { }
  1713. template<typename B0, typename B1, typename B2, typename B3, typename B4>
  1714. join_node(graph &g, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4) : unfolded_type(g, b0, b1, b2, b3, b4) { }
  1715. #if __TBB_VARIADIC_MAX >= 6
  1716. template<typename B0, typename B1, typename B2, typename B3, typename B4, typename B5>
  1717. join_node(graph &g, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4, B5 b5) : unfolded_type(g, b0, b1, b2, b3, b4, b5) { }
  1718. #endif
  1719. #if __TBB_VARIADIC_MAX >= 7
  1720. template<typename B0, typename B1, typename B2, typename B3, typename B4, typename B5, typename B6>
  1721. join_node(graph &g, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4, B5 b5, B6 b6) : unfolded_type(g, b0, b1, b2, b3, b4, b5, b6) { }
  1722. #endif
  1723. #if __TBB_VARIADIC_MAX >= 8
  1724. template<typename B0, typename B1, typename B2, typename B3, typename B4, typename B5, typename B6, typename B7>
  1725. join_node(graph &g, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4, B5 b5, B6 b6, B7 b7) : unfolded_type(g, b0, b1, b2, b3, b4, b5, b6, b7) { }
  1726. #endif
  1727. #if __TBB_VARIADIC_MAX >= 9
  1728. template<typename B0, typename B1, typename B2, typename B3, typename B4, typename B5, typename B6, typename B7, typename B8>
  1729. join_node(graph &g, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4, B5 b5, B6 b6, B7 b7, B8 b8) : unfolded_type(g, b0, b1, b2, b3, b4, b5, b6, b7, b8) { }
  1730. #endif
  1731. #if __TBB_VARIADIC_MAX >= 10
  1732. template<typename B0, typename B1, typename B2, typename B3, typename B4, typename B5, typename B6, typename B7, typename B8, typename B9>
  1733. join_node(graph &g, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4, B5 b5, B6 b6, B7 b7, B8 b8, B9 b9) : unfolded_type(g, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9) { }
  1734. #endif
  1735. join_node(const join_node &other) : unfolded_type(other) {}
  1736. };
  1737. #if TBB_PREVIEW_GRAPH_NODES
  1738. // or node
  1739. #include "internal/_flow_graph_or_impl.h"
  1740. template<typename InputTuple>
  1741. class or_node : public internal::unfolded_or_node<InputTuple> {
  1742. private:
  1743. static const int N = tbb::flow::tuple_size<InputTuple>::value;
  1744. public:
  1745. typedef typename internal::or_output_type<InputTuple>::type output_type;
  1746. typedef typename internal::unfolded_or_node<InputTuple> unfolded_type;
  1747. or_node(graph& g) : unfolded_type(g) { }
  1748. // Copy constructor
  1749. or_node( const or_node& other ) : unfolded_type(other) { }
  1750. };
  1751. #endif // TBB_PREVIEW_GRAPH_NODES
  1752. //! Makes an edge between a single predecessor and a single successor
  1753. template< typename T >
  1754. inline void make_edge( sender<T> &p, receiver<T> &s ) {
  1755. p.register_successor( s );
  1756. }
  1757. //! Makes an edge between a single predecessor and a single successor
  1758. template< typename T >
  1759. inline void remove_edge( sender<T> &p, receiver<T> &s ) {
  1760. p.remove_successor( s );
  1761. }
  1762. //! Returns a copy of the body from a function or continue node
  1763. template< typename Body, typename Node >
  1764. Body copy_body( Node &n ) {
  1765. return n.template copy_function_object<Body>();
  1766. }
  1767. } // interface6
  1768. using interface6::graph;
  1769. using interface6::graph_node;
  1770. using interface6::continue_msg;
  1771. using interface6::sender;
  1772. using interface6::receiver;
  1773. using interface6::continue_receiver;
  1774. using interface6::source_node;
  1775. using interface6::function_node;
  1776. using interface6::multifunction_node;
  1777. using interface6::split_node;
  1778. using interface6::internal::output_port;
  1779. #if TBB_PREVIEW_GRAPH_NODES
  1780. using interface6::or_node;
  1781. #endif
  1782. using interface6::continue_node;
  1783. using interface6::overwrite_node;
  1784. using interface6::write_once_node;
  1785. using interface6::broadcast_node;
  1786. using interface6::buffer_node;
  1787. using interface6::queue_node;
  1788. using interface6::sequencer_node;
  1789. using interface6::priority_queue_node;
  1790. using interface6::limiter_node;
  1791. using namespace interface6::internal::graph_policy_namespace;
  1792. using interface6::join_node;
  1793. using interface6::input_port;
  1794. using interface6::copy_body;
  1795. using interface6::make_edge;
  1796. using interface6::remove_edge;
  1797. using interface6::internal::NO_TAG;
  1798. using interface6::internal::tag_value;
  1799. } // flow
  1800. } // tbb
  1801. #endif // __TBB_flow_graph_H