xref: /oneTBB/include/oneapi/tbb/global_control.h (revision a1f53c8f)
1 /*
2     Copyright (c) 2005-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 #ifndef __TBB_global_control_H
18 #define __TBB_global_control_H
19 
20 #include "detail/_config.h"
21 #include "detail/_namespace_injection.h"
22 #include "detail/_assert.h"
23 #include "detail/_template_helpers.h"
24 #include "detail/_exception.h"
25 
26 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
27 #include <new> // std::nothrow_t
28 #endif
29 #include <cstddef>
30 
31 namespace tbb {
32 namespace detail {
33 
34 namespace d1 {
35 class global_control;
36 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
37 class task_scheduler_handle;
38 #endif
39 }
40 
41 namespace r1 {
42 TBB_EXPORT void __TBB_EXPORTED_FUNC create(d1::global_control&);
43 TBB_EXPORT void __TBB_EXPORTED_FUNC destroy(d1::global_control&);
44 TBB_EXPORT std::size_t __TBB_EXPORTED_FUNC global_control_active_value(int);
45 struct global_control_impl;
46 struct control_storage_comparator;
47 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
48 void release_impl(d1::task_scheduler_handle& handle);
49 bool finalize_impl(d1::task_scheduler_handle& handle);
50 TBB_EXPORT void __TBB_EXPORTED_FUNC get(d1::task_scheduler_handle&);
51 TBB_EXPORT bool __TBB_EXPORTED_FUNC finalize(d1::task_scheduler_handle&, std::intptr_t mode);
52 #endif
53 }
54 
55 namespace d1 {
56 
57 class global_control {
58 public:
59     enum parameter {
60         max_allowed_parallelism,
61         thread_stack_size,
62         terminate_on_exception,
63 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
64         scheduler_handle, // not a public parameter
65 #else
66         reserved1, // not a public parameter
67 #endif
68         parameter_max // insert new parameters above this point
69     };
70 
71     global_control(parameter p, std::size_t value) :
72         my_value(value), my_reserved(), my_param(p) {
73         suppress_unused_warning(my_reserved);
74         __TBB_ASSERT(my_param < parameter_max, "Invalid parameter");
75 #if __TBB_WIN8UI_SUPPORT && (_WIN32_WINNT < 0x0A00)
76         // For Windows 8 Store* apps it's impossible to set stack size
77         if (p==thread_stack_size)
78             return;
79 #elif __TBB_x86_64 && (_WIN32 || _WIN64)
80         if (p==thread_stack_size)
81             __TBB_ASSERT_RELEASE((unsigned)value == value, "Stack size is limited to unsigned int range");
82 #endif
83         if (my_param==max_allowed_parallelism)
84             __TBB_ASSERT_RELEASE(my_value>0, "max_allowed_parallelism cannot be 0.");
85         r1::create(*this);
86     }
87 
88     ~global_control() {
89         __TBB_ASSERT(my_param < parameter_max, "Invalid parameter");
90 #if __TBB_WIN8UI_SUPPORT && (_WIN32_WINNT < 0x0A00)
91         // For Windows 8 Store* apps it's impossible to set stack size
92         if (my_param==thread_stack_size)
93             return;
94 #endif
95         r1::destroy(*this);
96     }
97 
98     static std::size_t active_value(parameter p) {
99         __TBB_ASSERT(p < parameter_max, "Invalid parameter");
100         return r1::global_control_active_value((int)p);
101     }
102 
103 private:
104     std::size_t my_value;
105     std::intptr_t my_reserved; // TODO: substitution of global_control* not to break backward compatibility
106     parameter my_param;
107 
108     friend struct r1::global_control_impl;
109     friend struct r1::control_storage_comparator;
110 };
111 
112 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
113 //! Finalization options.
114 //! Outside of the class to avoid extensive friendship.
115 static constexpr std::intptr_t release_nothrowing = 0;
116 static constexpr std::intptr_t finalize_nothrowing = 1;
117 static constexpr std::intptr_t finalize_throwing = 2;
118 
119 //! User side wrapper for a task scheduler lifetime control object
120 class task_scheduler_handle {
121 public:
122     task_scheduler_handle() = default;
123     ~task_scheduler_handle() {
124         release(*this);
125     }
126 
127     //! No copy
128     task_scheduler_handle(const task_scheduler_handle& other) = delete;
129     task_scheduler_handle& operator=(const task_scheduler_handle& other) = delete;
130 
131     //! Move only
132     task_scheduler_handle(task_scheduler_handle&& other) noexcept : m_ctl{nullptr} {
133         std::swap(m_ctl, other.m_ctl);
134     }
135     task_scheduler_handle& operator=(task_scheduler_handle&& other) noexcept {
136         std::swap(m_ctl, other.m_ctl);
137         return *this;
138     };
139 
140     explicit operator bool() const noexcept {
141         return m_ctl != nullptr;
142     }
143 
144     //! Get and active instance of task_scheduler_handle
145     static task_scheduler_handle get() {
146          task_scheduler_handle handle;
147          r1::get(handle);
148          return handle;
149     }
150 
151     //! Release the reference and deactivate handle
152     static void release(task_scheduler_handle& handle) {
153         if (handle.m_ctl != nullptr) {
154             r1::finalize(handle, release_nothrowing);
155         }
156     }
157 
158 private:
159     friend void r1::release_impl(task_scheduler_handle& handle);
160     friend bool r1::finalize_impl(task_scheduler_handle& handle);
161     friend void __TBB_EXPORTED_FUNC r1::get(task_scheduler_handle&);
162 
163     global_control* m_ctl{nullptr};
164 };
165 
166 #if TBB_USE_EXCEPTIONS
167 //! Waits for worker threads termination. Throws exception on error.
168 inline void finalize(task_scheduler_handle& handle) {
169     r1::finalize(handle, finalize_throwing);
170 }
171 #endif
172 //! Waits for worker threads termination. Returns false on error.
173 inline bool finalize(task_scheduler_handle& handle, const std::nothrow_t&) noexcept {
174     return r1::finalize(handle, finalize_nothrowing);
175 }
176 #endif // __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
177 
178 } // namespace d1
179 } // namespace detail
180 
181 inline namespace v1 {
182 using detail::d1::global_control;
183 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
184 using detail::d1::finalize;
185 using detail::d1::task_scheduler_handle;
186 using detail::r1::unsafe_wait;
187 #endif
188 } // namespace v1
189 
190 } // namespace tbb
191 
192 #endif // __TBB_global_control_H
193