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 #include "tbb/task.h"
18 #include "harness.h"
19 #include "tbb/task_scheduler_init.h"
20 
21 using tbb::task;
22 
23 #if __TBB_ipf
24     const unsigned StackSize = 1024*1024*6;
25 #else /*  */
26     const unsigned StackSize = 1024*1024*3;
27 #endif
28 
29 // GCC and ICC on Linux store TLS data in the stack space. This test makes sure
30 // that the stealing limiting heuristic used by the task scheduler does not
31 // switch off stealing when a large amount of TLS data is reserved.
32 #if _MSC_VER
33 __declspec(thread)
34 #elif __linux__ || ((__MINGW32__ || __MINGW64__) && __TBB_GCC_VERSION >= 40500)
35 __thread
36 #endif
37     char map2[1024*1024*2];
38 
39 class TestTask : public task {
40 public:
41     static volatile int completed;
execute()42     task* execute() __TBB_override {
43         completed = 1;
44         return NULL;
45     };
46 };
47 
48 volatile int TestTask::completed = 0;
49 
TestStealingIsEnabled()50 void TestStealingIsEnabled () {
51     tbb::task_scheduler_init init(2, StackSize);
52     task &r = *new( task::allocate_root() ) tbb::empty_task;
53     task &t = *new( r.allocate_child() ) TestTask;
54     r.set_ref_count(2);
55     r.spawn(t);
56     int count = 0;
57     while ( !TestTask::completed && ++count < 6 )
58         Harness::Sleep(1000);
59     ASSERT( TestTask::completed, "Stealing is disabled or the machine is heavily oversubscribed" );
60     r.wait_for_all();
61     task::destroy(r);
62 }
63 
TestMain()64 int TestMain () {
65 #if !__TBB_THREAD_LOCAL_VARIABLES_PRESENT
66     REPORT( "Known issue: Test skipped because no compiler support for __thread keyword.\n" );
67     return Harness::Skipped;
68 #endif
69     if ( tbb::task_scheduler_init::default_num_threads() == 1 ) {
70         REPORT( "Known issue: Test requires at least 2 hardware threads.\n" );
71         return Harness::Skipped;
72     }
73     TestStealingIsEnabled();
74     return Harness::Done;
75 }
76