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.

2432 lines
91 KiB

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