Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb_thread.cpp
Go to the documentation of this file.
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 #if _WIN32||_WIN64
18 #include <process.h> // _beginthreadex()
19 #endif
20 #include <errno.h>
21 #include "tbb_misc.h" // handle_win_error()
22 #include "tbb/tbb_stddef.h"
23 #include "tbb/tbb_thread.h"
24 #include "tbb/tbb_allocator.h"
25 #include "tbb/global_control.h" // thread_stack_size
26 #include "governor.h" // default_num_threads()
27 #if __TBB_WIN8UI_SUPPORT
28 #include <thread>
29 #endif
30 
31 namespace tbb {
32 namespace internal {
33 
35 void* allocate_closure_v3( size_t size )
36 {
37  return allocate_via_handler_v3( size );
38 }
39 
41 void free_closure_v3( void *ptr )
42 {
44 }
45 
47 {
48  if (!joinable())
49  handle_perror( EINVAL, "tbb_thread::join" ); // Invalid argument
51  handle_perror( EDEADLK, "tbb_thread::join" ); // Resource deadlock avoided
52 #if _WIN32||_WIN64
53 #if __TBB_WIN8UI_SUPPORT
54  std::thread* thread_tmp=(std::thread*)my_thread_id;
55  thread_tmp->join();
56  delete thread_tmp;
57 #else // __TBB_WIN8UI_SUPPORT
58  DWORD status = WaitForSingleObjectEx( my_handle, INFINITE, FALSE );
59  if ( status == WAIT_FAILED )
60  handle_win_error( GetLastError() );
61  BOOL close_stat = CloseHandle( my_handle );
62  if ( close_stat == 0 )
63  handle_win_error( GetLastError() );
64  my_thread_id = 0;
65 #endif // __TBB_WIN8UI_SUPPORT
66 #else
67  int status = pthread_join( my_handle, NULL );
68  if( status )
69  handle_perror( status, "pthread_join" );
70 #endif // _WIN32||_WIN64
71  my_handle = 0;
72 }
73 
75  if (!joinable())
76  handle_perror( EINVAL, "tbb_thread::detach" ); // Invalid argument
77 #if _WIN32||_WIN64
78  BOOL status = CloseHandle( my_handle );
79  if ( status == 0 )
80  handle_win_error( GetLastError() );
81  my_thread_id = 0;
82 #else
83  int status = pthread_detach( my_handle );
84  if( status )
85  handle_perror( status, "pthread_detach" );
86 #endif // _WIN32||_WIN64
87  my_handle = 0;
88 }
89 
91  void* closure ) {
92 #if _WIN32||_WIN64
93 #if __TBB_WIN8UI_SUPPORT
94  std::thread* thread_tmp=new std::thread(start_routine, closure);
95  my_handle = thread_tmp->native_handle();
96 // TODO: to find out the way to find thread_id without GetThreadId and other
97 // desktop functions.
98 // Now tbb_thread does have its own thread_id that stores std::thread object
99  my_thread_id = (size_t)thread_tmp;
100 #else
101  unsigned thread_id;
102  // The return type of _beginthreadex is "uintptr_t" on new MS compilers,
103  // and 'unsigned long' on old MS compilers. uintptr_t works for both.
104  uintptr_t status = _beginthreadex( NULL, (unsigned)global_control::active_value(global_control::thread_stack_size),
105  start_routine, closure, 0, &thread_id );
106  if( status==0 )
107  handle_perror(errno,"__beginthreadex");
108  else {
109  my_handle = (HANDLE)status;
110  my_thread_id = thread_id;
111  }
112 #endif
113 #else
114  pthread_t thread_handle;
115  int status;
116  pthread_attr_t stack_size;
117  status = pthread_attr_init( &stack_size );
118  if( status )
119  handle_perror( status, "pthread_attr_init" );
120  status = pthread_attr_setstacksize( &stack_size, global_control::active_value(global_control::thread_stack_size) );
121  if( status )
122  handle_perror( status, "pthread_attr_setstacksize" );
123 
124  status = pthread_create( &thread_handle, &stack_size, start_routine, closure );
125  if( status )
126  handle_perror( status, "pthread_create" );
127  status = pthread_attr_destroy( &stack_size );
128  if( status )
129  handle_perror( status, "pthread_attr_destroy" );
130 
132 #endif // _WIN32||_WIN64
133 }
134 
137 }
138 
140 #if _WIN32||_WIN64
141  return tbb_thread_v3::id( GetCurrentThreadId() );
142 #else
143  return tbb_thread_v3::id( pthread_self() );
144 #endif // _WIN32||_WIN64
145 }
146 
148 {
149  if (t1.joinable())
150  t1.detach();
151  t1.my_handle = t2.my_handle;
152  t2.my_handle = 0;
153 #if _WIN32||_WIN64
154  t1.my_thread_id = t2.my_thread_id;
155  t2.my_thread_id = 0;
156 #endif // _WIN32||_WIN64
157 }
158 
160 {
161  __TBB_Yield();
162 }
163 
165 {
166 #if _WIN32||_WIN64
168  tick_count t1 = t0;
169  for(;;) {
170  double remainder = (i-(t1-t0)).seconds()*1e3; // milliseconds remaining to sleep
171  if( remainder<=0 ) break;
172  DWORD t = remainder>=INFINITE ? INFINITE-1 : DWORD(remainder);
173 #if !__TBB_WIN8UI_SUPPORT
174  Sleep( t );
175 #else
176  std::chrono::milliseconds sleep_time( t );
177  std::this_thread::sleep_for( sleep_time );
178 #endif
179  t1 = tick_count::now();
180  }
181 #else
182  struct timespec req;
183  double sec = i.seconds();
184 
185  req.tv_sec = static_cast<long>(sec);
186  req.tv_nsec = static_cast<long>( (sec - req.tv_sec)*1e9 );
187  nanosleep(&req, NULL);
188 #endif // _WIN32||_WIN64
189 }
190 
191 } // internal
192 } // tbb
governor.h
tbb::internal::deallocate_via_handler_v3
void __TBB_EXPORTED_FUNC deallocate_via_handler_v3(void *p)
Deallocates memory using FreeHandler.
Definition: cache_aligned_allocator.cpp:232
tbb_thread.h
tbb::internal::thread_sleep_v3
void __TBB_EXPORTED_FUNC thread_sleep_v3(const tick_count::interval_t &i)
Definition: tbb_thread.cpp:164
tbb::internal::tbb_thread_v3::detach
void __TBB_EXPORTED_METHOD detach()
When detach() returns, *this no longer represents the possibly continuing thread of execution.
Definition: tbb_thread.cpp:74
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::internal::allocate_closure_v3
void *__TBB_EXPORTED_FUNC allocate_closure_v3(size_t size)
Allocate a closure.
Definition: tbb_thread.cpp:35
tbb::tick_count
Absolute timestamp.
Definition: tick_count.h:34
id
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id id
Definition: ittnotify_static.h:172
tbb::internal::tbb_thread_v3::hardware_concurrency
static unsigned __TBB_EXPORTED_FUNC hardware_concurrency() __TBB_NOEXCEPT(true)
The number of hardware thread contexts.
Definition: tbb_thread.cpp:135
tbb::internal::governor::default_num_threads
static unsigned default_num_threads()
Definition: governor.h:84
tbb::internal::handle_perror
void __TBB_EXPORTED_FUNC handle_perror(int error_code, const char *aux_info)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info.
Definition: tbb_misc.cpp:87
global_control.h
tbb::internal::handle_win_error
void handle_win_error(int error_code)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info.
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::internal::free_closure_v3
void __TBB_EXPORTED_FUNC free_closure_v3(void *)
Free a closure allocated by allocate_closure_v3.
Definition: tbb_thread.cpp:41
size
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size
Definition: ittnotify_static.h:109
tbb::internal::tbb_thread_v3::my_handle
native_handle_type my_handle
Definition: tbb_thread.h:206
tbb::interface9::global_control::thread_stack_size
@ thread_stack_size
Definition: global_control.h:29
tbb::internal::tbb_thread_v3::get_id
id get_id() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:269
tbb::internal::rml::thread_handle
thread_monitor::handle_type thread_handle
Definition: private_server.cpp:31
tbb::tick_count::now
static tick_count now()
Return current time.
Definition: tick_count.h:101
tbb::interface9::global_control::active_value
static size_t active_value(parameter p)
Definition: global_control.h:59
tbb::internal::tbb_thread_v3
Versioned thread class.
Definition: tbb_thread.h:122
__TBB_Yield
#define __TBB_Yield()
Definition: ibm_aix51.h:44
tbb::this_tbb_thread::get_id
__TBB_DEPRECATED_IN_VERBOSE_MODE tbb_thread::id get_id()
Definition: tbb_thread.h:331
tbb::internal::thread_yield_v3
void __TBB_EXPORTED_FUNC thread_yield_v3()
Definition: tbb_thread.cpp:159
tbb_misc.h
tbb::internal::allocate_via_handler_v3
void *__TBB_EXPORTED_FUNC allocate_via_handler_v3(size_t n)
Allocates memory using MallocHandler.
Definition: cache_aligned_allocator.cpp:224
tbb::tick_count::interval_t::seconds
double seconds() const
Return the length of a time interval in seconds.
Definition: tick_count.h:130
tbb::internal::tbb_thread_v3::joinable
bool joinable() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:185
tbb::internal::thread_get_id_v3
tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3()
Definition: tbb_thread.cpp:139
tbb::internal::tbb_thread_v3::internal_start
void __TBB_EXPORTED_METHOD internal_start(__TBB_NATIVE_THREAD_ROUTINE_PTR(start_routine), void *closure)
Definition: tbb_thread.cpp:90
tbb::internal::tbb_thread_v3::join
void __TBB_EXPORTED_METHOD join()
The completion of the thread represented by *this happens before join() returns.
Definition: tbb_thread.cpp:46
__TBB_NOEXCEPT
#define __TBB_NOEXCEPT(expression)
Definition: tbb_stddef.h:110
tbb::tick_count::interval_t
Relative time interval.
Definition: tick_count.h:37
tbb_stddef.h
tbb::internal::tbb_thread_v3::id
Definition: tbb_thread.h:233
tbb::internal::move_v3
void __TBB_EXPORTED_FUNC move_v3(tbb_thread_v3 &t1, tbb_thread_v3 &t2)
Definition: tbb_thread.cpp:147
__TBB_NATIVE_THREAD_ROUTINE_PTR
#define __TBB_NATIVE_THREAD_ROUTINE_PTR(r)
Definition: tbb_thread.h:49
tbb_allocator.h

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.