1 /*
2     Copyright (c) 2005-2020 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 // This file is a common part of test_cilk_interop and test_cilk_dynamic_load tests
18 
19 int TBB_Fib( int n );
20 
21 class FibCilkSubtask: public tbb::task {
22     int n;
23     int& result;
execute()24     task* execute() __TBB_override {
25         if( n<2 ) {
26             result = n;
27         } else {
28             int x, y;
29             x = cilk_spawn TBB_Fib(n-2);
30             y = cilk_spawn TBB_Fib(n-1);
31             cilk_sync;
32             result = x+y;
33         }
34         return NULL;
35     }
36 public:
FibCilkSubtask(int & result_,int n_)37     FibCilkSubtask( int& result_, int n_ ) : result(result_), n(n_) {}
38 };
39 
40 class FibTask: public tbb::task {
41     int n;
42     int& result;
execute()43     task* execute() __TBB_override {
44         if( !g_sandwich && n<2 ) {
45             result = n;
46         } else {
47             int x,y;
48             tbb::task_scheduler_init init(P_nested);
49             task* self0 = &task::self();
50             set_ref_count( 3 );
51             if ( g_sandwich ) {
52                 spawn (*new( allocate_child() ) FibCilkSubtask(x,n-1));
53                 spawn (*new( allocate_child() ) FibCilkSubtask(y,n-2));
54             }
55             else {
56                 spawn (*new( allocate_child() ) FibTask(x,n-1));
57                 spawn (*new( allocate_child() ) FibTask(y,n-2));
58             }
59             wait_for_all();
60             task* self1 = &task::self();
61             ASSERT( self0 == self1, "failed to preserve TBB TLS" );
62             result = x+y;
63         }
64         return NULL;
65     }
66 public:
FibTask(int & result_,int n_)67     FibTask( int& result_, int n_ ) : result(result_), n(n_) {}
68 };
69 
TBB_Fib(int n)70 int TBB_Fib( int n ) {
71     if( n<2 ) {
72         return n;
73     } else {
74         int result;
75         tbb::task_scheduler_init init(P_nested);
76         tbb::task::spawn_root_and_wait(*new( tbb::task::allocate_root()) FibTask(result,n) );
77         return result;
78     }
79 }
80