1 /*
2     Copyright (c) 2020-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_test_conformance_conformance_flowgraph_H
18 #define __TBB_test_conformance_conformance_flowgraph_H
19 
20 struct passthru_body {
21     int operator()( int i ) {
22         return i;
23     }
24     void operator()( const int& argument, oneapi::tbb::flow::multifunction_node<int, std::tuple<int>>::output_ports_type &op ) {
25        std::get<0>(op).try_put(argument);
26     }
27 
28 };
29 
30 template<typename V>
31 using test_push_receiver = oneapi::tbb::flow::queue_node<V>;
32 
33 template<typename V>
34 int get_count( test_push_receiver<V>& rr ){
35     int val = 0;
36     for(V tmp; rr.try_get(tmp); ++val);
37 
38     return val;
39 }
40 
41 
42 template< typename OutputType >
43 struct first_functor {
44     int my_id;
45     static std::atomic<int> first_id;
46 
47     first_functor(int id) : my_id(id) {}
48 
49     OutputType operator()( OutputType argument ) {
50         int old_value = first_id;
51         while(first_id == -1 &&
52               !first_id.compare_exchange_weak(old_value, my_id))
53             ;
54 
55         return argument;
56     }
57 
58     OutputType operator()( const oneapi::tbb::flow::continue_msg&  ) {
59         return operator()(OutputType());
60     }
61 
62     void operator()( const OutputType& argument, oneapi::tbb::flow::multifunction_node<int, std::tuple<int>>::output_ports_type &op ) {
63         operator()(OutputType());
64         std::get<0>(op).try_put(argument);
65     }
66 };
67 
68 template<typename OutputType>
69 std::atomic<int> first_functor<OutputType>::first_id;
70 
71 
72 template< typename OutputType >
73 struct inc_functor {
74     static std::atomic<size_t> execute_count;
75 
76     OutputType operator()( oneapi::tbb::flow::continue_msg ) {
77        ++execute_count;
78        return OutputType();
79     }
80 
81     OutputType operator()( int argument ) {
82        ++execute_count;
83        return argument;
84     }
85 };
86 
87 template<typename OutputType>
88 std::atomic<size_t> inc_functor<OutputType>::execute_count;
89 
90 #endif // __TBB_test_conformance_conformance_flowgraph_H
91