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 | // Test that __TBB_Yield works. |
18 | // On Red Hat EL4 U1, it does not work, because sched_yield is broken. |
19 | |
20 | #define HARNESS_DEFAULT_MIN_THREADS 4 |
21 | #define HARNESS_DEFAULT_MAX_THREADS 8 |
22 | |
23 | #include "tbb/tbb_machine.h" |
24 | #include "tbb/tick_count.h" |
25 | #include "harness.h" |
26 | |
27 | static volatile long CyclicCounter; |
28 | static volatile bool Quit; |
29 | double SingleThreadTime; |
30 | |
31 | struct RoundRobin: NoAssign { |
32 | const int number_of_threads; |
33 | RoundRobin( long p ) : number_of_threads(p) {} |
34 | void operator()( long k ) const { |
35 | tbb::tick_count t0 = tbb::tick_count::now(); |
36 | for( long i=0; i<10000; ++i ) { |
37 | // Wait for previous thread to notify us |
38 | for( int j=0; CyclicCounter!=k && !Quit; ++j ) { |
39 | __TBB_Yield(); |
40 | if( j%100==0 ) { |
41 | tbb::tick_count t1 = tbb::tick_count::now(); |
42 | if( (t1-t0).seconds()>=1.0*number_of_threads ) { |
43 | REPORT("Warning: __TBB_Yield failing to yield with %d threads (or system is heavily loaded)\n" ,number_of_threads); |
44 | Quit = true; |
45 | return; |
46 | } |
47 | } |
48 | } |
49 | // Notify next thread that it can run |
50 | CyclicCounter = (k+1)%number_of_threads; |
51 | } |
52 | } |
53 | }; |
54 | |
55 | int TestMain () { |
56 | for( int p=MinThread; p<=MaxThread; ++p ) { |
57 | REMARK("testing with %d threads\n" , p ); |
58 | CyclicCounter = 0; |
59 | Quit = false; |
60 | NativeParallelFor( long(p), RoundRobin(p) ); |
61 | } |
62 | return Harness::Done; |
63 | } |
64 | |
65 | |