| 1 | /* |
| 2 | Copyright (c) 2005-2019 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 | #define __TBB_ARENA_OBSERVER 0 |
| 18 | #include "tbb/task_scheduler_observer.h" |
| 19 | |
| 20 | typedef uintptr_t FlagType; |
| 21 | const int MaxFlagIndex = sizeof(FlagType)*8-1; |
| 22 | |
| 23 | class MyObserver: public tbb::task_scheduler_observer { |
| 24 | FlagType flags; |
| 25 | void on_scheduler_entry( bool is_worker ) __TBB_override; |
| 26 | void on_scheduler_exit( bool is_worker ) __TBB_override; |
| 27 | public: |
| 28 | MyObserver( FlagType flags_ ) : flags(flags_) { |
| 29 | observe(true); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | #include "harness_assert.h" |
| 34 | #include "tbb/atomic.h" |
| 35 | |
| 36 | tbb::atomic<int> EntryCount; |
| 37 | tbb::atomic<int> ExitCount; |
| 38 | |
| 39 | struct State { |
| 40 | FlagType MyFlags; |
| 41 | bool IsMaster; |
| 42 | State() : MyFlags(), IsMaster() {} |
| 43 | }; |
| 44 | |
| 45 | #include "../tbb/tls.h" |
| 46 | tbb::internal::tls<State*> LocalState; |
| 47 | |
| 48 | void MyObserver::on_scheduler_entry( bool is_worker ) { |
| 49 | State& state = *LocalState; |
| 50 | ASSERT( is_worker==!state.IsMaster, NULL ); |
| 51 | ++EntryCount; |
| 52 | state.MyFlags |= flags; |
| 53 | } |
| 54 | |
| 55 | void MyObserver::on_scheduler_exit( bool is_worker ) { |
| 56 | State& state = *LocalState; |
| 57 | ASSERT( is_worker==!state.IsMaster, NULL ); |
| 58 | ++ExitCount; |
| 59 | state.MyFlags &= ~flags; |
| 60 | } |
| 61 | |
| 62 | #include "tbb/task.h" |
| 63 | |
| 64 | class FibTask: public tbb::task { |
| 65 | const int n; |
| 66 | FlagType flags; |
| 67 | public: |
| 68 | FibTask( int n_, FlagType flags_ ) : n(n_), flags(flags_) {} |
| 69 | tbb::task* execute() __TBB_override { |
| 70 | ASSERT( !(~LocalState->MyFlags & flags), NULL ); |
| 71 | if( n>=2 ) { |
| 72 | set_ref_count(3); |
| 73 | spawn(*new( allocate_child() ) FibTask(n-1,flags)); |
| 74 | spawn_and_wait_for_all(*new( allocate_child() ) FibTask(n-2,flags)); |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | void DoFib( FlagType flags ) { |
| 81 | tbb::task* t = new( tbb::task::allocate_root() ) FibTask(10,flags); |
| 82 | tbb::task::spawn_root_and_wait(*t); |
| 83 | } |
| 84 | |
| 85 | #include "tbb/task_scheduler_init.h" |
| 86 | #include "harness.h" |
| 87 | |
| 88 | class DoTest { |
| 89 | int nthread; |
| 90 | public: |
| 91 | DoTest( int n ) : nthread(n) {} |
| 92 | void operator()( int i ) const { |
| 93 | LocalState->IsMaster = true; |
| 94 | if( i==0 ) { |
| 95 | tbb::task_scheduler_init init(nthread); |
| 96 | DoFib(0); |
| 97 | } else { |
| 98 | FlagType f = i<=MaxFlagIndex? 1<<i : 0; |
| 99 | MyObserver w(f); |
| 100 | tbb::task_scheduler_init init(nthread); |
| 101 | DoFib(f); |
| 102 | } |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | void TestObserver( int p, int q ) { |
| 107 | NativeParallelFor( p, DoTest(q) ); |
| 108 | } |
| 109 | |
| 110 | int TestMain () { |
| 111 | for( int p=MinThread; p<=MaxThread; ++p ) |
| 112 | for( int q=MinThread; q<=MaxThread; ++q ) |
| 113 | TestObserver(p,q); |
| 114 | ASSERT( EntryCount>0, "on_scheduler_entry not exercised" ); |
| 115 | ASSERT( ExitCount>0, "on_scheduler_exit not exercised" ); |
| 116 | return Harness::Done; |
| 117 | } |
| 118 | |