1 /* 2 Copyright (c) 2005-2022 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 // All platform-specific threading support is encapsulated here. */ 18 19 #ifndef __RML_thread_monitor_H 20 #define __RML_thread_monitor_H 21 22 #if __TBB_USE_WINAPI 23 #include <windows.h> 24 #include <process.h> 25 #include <malloc.h> //_alloca 26 #include "misc.h" // support for processor groups 27 #if __TBB_WIN8UI_SUPPORT && (_WIN32_WINNT < 0x0A00) 28 #include <thread> 29 #endif 30 #elif __TBB_USE_POSIX 31 #include <pthread.h> 32 #include <cstring> 33 #include <cstdlib> 34 #else 35 #error Unsupported platform 36 #endif 37 #include <cstdio> 38 39 #include "oneapi/tbb/detail/_template_helpers.h" 40 41 #include "itt_notify.h" 42 #include "semaphore.h" 43 44 // All platform-specific threading support is in this header. 45 46 #if (_WIN32||_WIN64)&&!__TBB_ipf 47 // Deal with 64K aliasing. The formula for "offset" is a Fibonacci hash function, 48 // which has the desirable feature of spreading out the offsets fairly evenly 49 // without knowing the total number of offsets, and furthermore unlikely to 50 // accidentally cancel out other 64K aliasing schemes that Microsoft might implement later. 51 // See Knuth Vol 3. "Theorem S" for details on Fibonacci hashing. 52 // The second statement is really does need "volatile", otherwise the compiler might remove the _alloca. 53 #define AVOID_64K_ALIASING(idx) \ 54 std::size_t offset = (idx+1) * 40503U % (1U<<16); \ 55 void* volatile sink_for_alloca = _alloca(offset); \ 56 __TBB_ASSERT_EX(sink_for_alloca, "_alloca failed"); 57 #else 58 // Linux thread allocators avoid 64K aliasing. 59 #define AVOID_64K_ALIASING(idx) tbb::detail::suppress_unused_warning(idx) 60 #endif /* _WIN32||_WIN64 */ 61 62 namespace tbb { 63 namespace detail { 64 namespace r1 { 65 66 // Forward declaration: throws std::runtime_error with what() returning error_code description prefixed with aux_info 67 void handle_perror(int error_code, const char* aux_info); 68 69 namespace rml { 70 namespace internal { 71 72 #if __TBB_USE_ITT_NOTIFY 73 static const ::tbb::detail::r1::tchar *SyncType_RML = _T("%Constant"); 74 static const ::tbb::detail::r1::tchar *SyncObj_ThreadMonitor = _T("RML Thr Monitor"); 75 #endif /* __TBB_USE_ITT_NOTIFY */ 76 77 //! Monitor with limited two-phase commit form of wait. 78 /** At most one thread should wait on an instance at a time. */ 79 class thread_monitor { 80 public: 81 thread_monitor() { 82 ITT_SYNC_CREATE(&my_sema, SyncType_RML, SyncObj_ThreadMonitor); 83 } 84 ~thread_monitor() {} 85 86 //! Notify waiting thread 87 /** Can be called by any thread. */ 88 void notify(); 89 90 //! Wait for notification 91 void wait(); 92 93 #if __TBB_USE_WINAPI 94 typedef HANDLE handle_type; 95 96 #define __RML_DECL_THREAD_ROUTINE unsigned WINAPI 97 typedef unsigned (WINAPI *thread_routine_type)(void*); 98 99 //! Launch a thread 100 static handle_type launch( thread_routine_type thread_routine, void* arg, std::size_t stack_size, const size_t* worker_index = nullptr ); 101 102 #elif __TBB_USE_POSIX 103 typedef pthread_t handle_type; 104 105 #define __RML_DECL_THREAD_ROUTINE void* 106 typedef void*(*thread_routine_type)(void*); 107 108 //! Launch a thread 109 static handle_type launch( thread_routine_type thread_routine, void* arg, std::size_t stack_size ); 110 #endif /* __TBB_USE_POSIX */ 111 112 //! Join thread 113 static void join(handle_type handle); 114 115 //! Detach thread 116 static void detach_thread(handle_type handle); 117 private: 118 // The protection from double notification of the binary semaphore 119 std::atomic<bool> my_notified{ false }; 120 binary_semaphore my_sema; 121 #if __TBB_USE_POSIX 122 static void check( int error_code, const char* routine ); 123 #endif 124 }; 125 126 #if __TBB_USE_WINAPI 127 128 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION 129 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000 130 #endif 131 132 // _beginthreadex API is not available in Windows 8 Store* applications, so use std::thread instead 133 #if __TBB_WIN8UI_SUPPORT && (_WIN32_WINNT < 0x0A00) 134 inline thread_monitor::handle_type thread_monitor::launch( thread_routine_type thread_function, void* arg, std::size_t, const std::size_t*) { 135 //TODO: check that exception thrown from std::thread is not swallowed silently 136 std::thread* thread_tmp=new std::thread(thread_function, arg); 137 return thread_tmp->native_handle(); 138 } 139 #else 140 inline thread_monitor::handle_type thread_monitor::launch( thread_routine_type thread_routine, void* arg, std::size_t stack_size, const std::size_t* worker_index ) { 141 unsigned thread_id; 142 int number_of_processor_groups = ( worker_index ) ? NumberOfProcessorGroups() : 0; 143 unsigned create_flags = ( number_of_processor_groups > 1 ) ? CREATE_SUSPENDED : 0; 144 HANDLE h = (HANDLE)_beginthreadex( nullptr, unsigned(stack_size), thread_routine, arg, STACK_SIZE_PARAM_IS_A_RESERVATION | create_flags, &thread_id ); 145 if( !h ) { 146 handle_perror(0, "thread_monitor::launch: _beginthreadex failed\n"); 147 } 148 if ( number_of_processor_groups > 1 ) { 149 MoveThreadIntoProcessorGroup( h, FindProcessorGroupIndex( static_cast<int>(*worker_index) ) ); 150 ResumeThread( h ); 151 } 152 return h; 153 } 154 #endif //__TBB_WIN8UI_SUPPORT && (_WIN32_WINNT < 0x0A00) 155 156 void thread_monitor::join(handle_type handle) { 157 #if TBB_USE_ASSERT 158 DWORD res = 159 #endif 160 WaitForSingleObjectEx(handle, INFINITE, FALSE); 161 __TBB_ASSERT( res==WAIT_OBJECT_0, nullptr); 162 #if TBB_USE_ASSERT 163 BOOL val = 164 #endif 165 CloseHandle(handle); 166 __TBB_ASSERT( val, nullptr); 167 } 168 169 void thread_monitor::detach_thread(handle_type handle) { 170 #if TBB_USE_ASSERT 171 BOOL val = 172 #endif 173 CloseHandle(handle); 174 __TBB_ASSERT( val, nullptr); 175 } 176 177 #endif /* __TBB_USE_WINAPI */ 178 179 #if __TBB_USE_POSIX 180 inline void thread_monitor::check( int error_code, const char* routine ) { 181 if( error_code ) { 182 handle_perror(error_code, routine); 183 } 184 } 185 186 inline thread_monitor::handle_type thread_monitor::launch( void* (*thread_routine)(void*), void* arg, std::size_t stack_size ) { 187 // FIXME - consider more graceful recovery than just exiting if a thread cannot be launched. 188 // Note that there are some tricky situations to deal with, such that the thread is already 189 // grabbed as part of an OpenMP team. 190 pthread_attr_t s; 191 check(pthread_attr_init( &s ), "pthread_attr_init has failed"); 192 if( stack_size>0 ) 193 check(pthread_attr_setstacksize( &s, stack_size ), "pthread_attr_setstack_size has failed" ); 194 pthread_t handle; 195 check( pthread_create( &handle, &s, thread_routine, arg ), "pthread_create has failed" ); 196 check( pthread_attr_destroy( &s ), "pthread_attr_destroy has failed" ); 197 return handle; 198 } 199 200 void thread_monitor::join(handle_type handle) { 201 check(pthread_join(handle, nullptr), "pthread_join has failed"); 202 } 203 204 void thread_monitor::detach_thread(handle_type handle) { 205 check(pthread_detach(handle), "pthread_detach has failed"); 206 } 207 #endif /* __TBB_USE_POSIX */ 208 209 inline void thread_monitor::notify() { 210 // Check that the semaphore is not notified twice 211 if (!my_notified.exchange(true, std::memory_order_release)) { 212 my_sema.V(); 213 } 214 } 215 216 inline void thread_monitor::wait() { 217 my_sema.P(); 218 // memory_order_seq_cst is required here to be ordered with 219 // further load checking shutdown state 220 my_notified.store(false, std::memory_order_seq_cst); 221 } 222 223 } // namespace internal 224 } // namespace rml 225 } // namespace r1 226 } // namespace detail 227 } // namespace tbb 228 229 #endif /* __RML_thread_monitor_H */ 230