xref: /oneTBB/test/tbb/test_eh_thread.cpp (revision 67c11716)
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 #include "common/config.h"
18 
19 #include "tbb/parallel_for.h"
20 #include "tbb/global_control.h"
21 
22 #include "common/test.h"
23 #include "common/utils.h"
24 #include "common/utils_concurrency_limit.h"
25 
26 #include <atomic>
27 #include <condition_variable>
28 #include <thread>
29 #include <vector>
30 
31 //! \file test_eh_thread.cpp
32 //! \brief Test for [internal] functionality
33 
34 // On Windows there is no real thread number limit beside available memory.
35 // Therefore, the test for thread limit is unreasonable.
36 //
37 // Under ASAN current approach is not viable as it breaks the ASAN itself as well
38 #if TBB_USE_EXCEPTIONS && !_WIN32 && !__ANDROID__ && !__TBB_TEST_USE_ADDRESS_SANITIZER
39 
40 static bool g_exception_caught = false;
41 static std::mutex m;
42 static std::condition_variable cv;
43 static std::atomic<bool> stop{ false };
44 
45 static void* thread_routine(void*)
46 {
47     std::unique_lock<std::mutex> lock(m);
48     cv.wait(lock, [] { return stop == true; });
49     return 0;
50 }
51 
52 class Thread {
53     pthread_t mHandle{};
54     bool mValid{};
55 public:
56     Thread() {
57         mValid = false;
58         pthread_attr_t attr;
59         // Limit the stack size not to consume all virtual memory on 32 bit platforms.
60         if (pthread_attr_init(&attr) == 0 && pthread_attr_setstacksize(&attr, 100*1024) == 0) {
61             mValid = pthread_create(&mHandle, &attr, thread_routine, /* arg = */ nullptr) == 0;
62         }
63     }
64     bool isValid() const { return mValid; }
65     void join() {
66         pthread_join(mHandle, nullptr);
67     }
68 };
69 
70 //! Test for exception when too many threads
71 //! \brief \ref resource_usage
72 TEST_CASE("Too many threads") {
73     if (utils::get_platform_max_threads() < 2) {
74         // The test expects that the scheduler will try to create at least one thread.
75         return;
76     }
77     std::thread /* isolate test */ ([] {
78         std::vector<Thread> threads;
79         stop = false;
80         for (;;) {
81             Thread thread;
82             if (!thread.isValid()) {
83                 break;
84             }
85             threads.push_back(thread);
86         }
87         g_exception_caught = false;
88         try {
89             // Initialize the library to create worker threads
90             tbb::parallel_for(0, 2, [](int) {});
91         } catch (const std::exception & e) {
92             g_exception_caught = true;
93             // Do not CHECK to avoid memory allocation (we can be out of memory)
94             if (e.what()== nullptr) {
95                 FAIL("Exception does not have description");
96             }
97         }
98         // Do not CHECK to avoid memory allocation (we can be out of memory)
99         if (!g_exception_caught) {
100             FAIL("No exception was caught");
101         }
102         stop = true;
103         cv.notify_all();
104         for (auto& t : threads) {
105             t.join();
106         }
107     }).join();
108 }
109 #endif
110