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