Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb_thread.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 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 
18 
19 */
20 
21 #ifndef __TBB_tbb_thread_H
22 #define __TBB_tbb_thread_H
23 
24 #include "tbb_stddef.h"
25 
26 #if _WIN32||_WIN64
27 #include "machine/windows_api.h"
28 #define __TBB_NATIVE_THREAD_ROUTINE unsigned WINAPI
29 #define __TBB_NATIVE_THREAD_ROUTINE_PTR(r) unsigned (WINAPI* r)( void* )
30 namespace tbb { namespace internal {
31 #if __TBB_WIN8UI_SUPPORT
32  typedef size_t thread_id_type;
33 #else // __TBB_WIN8UI_SUPPORT
34  typedef DWORD thread_id_type;
35 #endif // __TBB_WIN8UI_SUPPORT
36 }} //namespace tbb::internal
37 #else
38 #define __TBB_NATIVE_THREAD_ROUTINE void*
39 #define __TBB_NATIVE_THREAD_ROUTINE_PTR(r) void* (*r)( void* )
40 #include <pthread.h>
41 namespace tbb { namespace internal {
42  typedef pthread_t thread_id_type;
43 }} //namespace tbb::internal
44 #endif // _WIN32||_WIN64
45 
46 #include "atomic.h"
48 #include "tick_count.h"
49 
50 #include __TBB_STD_SWAP_HEADER
51 #include <iosfwd>
52 
53 namespace tbb {
54 
55 namespace internal {
56  class tbb_thread_v3;
57 }
58 
59 inline void swap( internal::tbb_thread_v3& t1, internal::tbb_thread_v3& t2 ) __TBB_NOEXCEPT(true);
60 
61 namespace internal {
62 
67 
69  void* operator new( size_t size ) {return allocate_closure_v3(size);}
70  void operator delete( void* ptr ) {free_closure_v3(ptr);}
71  };
72 
73  template<class F> struct thread_closure_0: thread_closure_base {
74  F function;
75 
77  thread_closure_0 *self = static_cast<thread_closure_0*>(c);
78  self->function();
79  delete self;
80  return 0;
81  }
82  thread_closure_0( const F& f ) : function(f) {}
83  };
85  template<class F, class X> struct thread_closure_1: thread_closure_base {
86  F function;
87  X arg1;
90  thread_closure_1 *self = static_cast<thread_closure_1*>(c);
91  self->function(self->arg1);
92  delete self;
93  return 0;
94  }
95  thread_closure_1( const F& f, const X& x ) : function(f), arg1(x) {}
96  };
97  template<class F, class X, class Y> struct thread_closure_2: thread_closure_base {
98  F function;
99  X arg1;
100  Y arg2;
103  thread_closure_2 *self = static_cast<thread_closure_2*>(c);
104  self->function(self->arg1, self->arg2);
105  delete self;
106  return 0;
107  }
108  thread_closure_2( const F& f, const X& x, const Y& y ) : function(f), arg1(x), arg2(y) {}
109  };
110 
113 #if __TBB_IF_NO_COPY_CTOR_MOVE_SEMANTICS_BROKEN
114  // Workaround for a compiler bug: declaring the copy constructor as public
115  // enables use of the moving constructor.
116  // The definition is not provided in order to prohibit copying.
117  public:
118 #endif
119  tbb_thread_v3(const tbb_thread_v3&); // = delete; // Deny access
120  public:
121 #if _WIN32||_WIN64
122  typedef HANDLE native_handle_type;
123 #else
124  typedef pthread_t native_handle_type;
125 #endif // _WIN32||_WIN64
126 
127  class id;
129  tbb_thread_v3() __TBB_NOEXCEPT(true) : my_handle(0)
130 #if _WIN32||_WIN64
131  , my_thread_id(0)
132 #endif // _WIN32||_WIN64
133  {}
134 
136  template <class F> explicit tbb_thread_v3(F f) {
137  typedef internal::thread_closure_0<F> closure_type;
138  internal_start(closure_type::start_routine, new closure_type(f));
139  }
141  template <class F, class X> tbb_thread_v3(F f, X x) {
142  typedef internal::thread_closure_1<F,X> closure_type;
143  internal_start(closure_type::start_routine, new closure_type(f,x));
144  }
146  template <class F, class X, class Y> tbb_thread_v3(F f, X x, Y y) {
147  typedef internal::thread_closure_2<F,X,Y> closure_type;
148  internal_start(closure_type::start_routine, new closure_type(f,x,y));
149  }
150 
151 #if __TBB_CPP11_RVALUE_REF_PRESENT
153  : my_handle(x.my_handle)
154 #if _WIN32||_WIN64
155  , my_thread_id(x.my_thread_id)
156 #endif
157  {
158  x.internal_wipe();
159  }
161  internal_move(x);
162  return *this;
163  }
164  private:
165  tbb_thread_v3& operator=(const tbb_thread_v3& x); // = delete;
166  public:
167 #else // __TBB_CPP11_RVALUE_REF_PRESENT
168  tbb_thread_v3& operator=(tbb_thread_v3& x) {
169  internal_move(x);
170  return *this;
171  }
172 #endif // __TBB_CPP11_RVALUE_REF_PRESENT
173 
174  void swap( tbb_thread_v3& t ) __TBB_NOEXCEPT(true) {tbb::swap( *this, t );}
175  bool joinable() const __TBB_NOEXCEPT(true) {return my_handle!=0; }
177  void __TBB_EXPORTED_METHOD join();
179  void __TBB_EXPORTED_METHOD detach();
180  ~tbb_thread_v3() {if( joinable() ) detach();}
181  inline id get_id() const __TBB_NOEXCEPT(true);
182  native_handle_type native_handle() { return my_handle; }
183 
185 
194  static unsigned __TBB_EXPORTED_FUNC hardware_concurrency() __TBB_NOEXCEPT(true);
195  private:
196  native_handle_type my_handle;
197 #if _WIN32||_WIN64
198  thread_id_type my_thread_id;
199 #endif // _WIN32||_WIN64
200 
201  void internal_wipe() __TBB_NOEXCEPT(true) {
202  my_handle = 0;
203 #if _WIN32||_WIN64
204  my_thread_id = 0;
205 #endif
206  }
208  if (joinable()) detach();
209  my_handle = x.my_handle;
210 #if _WIN32||_WIN64
211  my_thread_id = x.my_thread_id;
212 #endif // _WIN32||_WIN64
213  x.internal_wipe();
214  }
215 
217  void __TBB_EXPORTED_METHOD internal_start( __TBB_NATIVE_THREAD_ROUTINE_PTR(start_routine),
218  void* closure );
219  friend void __TBB_EXPORTED_FUNC move_v3( tbb_thread_v3& t1, tbb_thread_v3& t2 );
220  friend void tbb::swap( tbb_thread_v3& t1, tbb_thread_v3& t2 ) __TBB_NOEXCEPT(true);
221  };
222 
225  id( thread_id_type id_ ) : my_id(id_) {}
226 
227  friend class tbb_thread_v3;
228  public:
229  id() __TBB_NOEXCEPT(true) : my_id(0) {}
230 
237 
238  template<class charT, class traits>
239  friend std::basic_ostream<charT, traits>&
240  operator<< (std::basic_ostream<charT, traits> &out,
242  {
243  out << id.my_id;
244  return out;
245  }
247 
248  friend inline size_t tbb_hasher( const tbb_thread_v3::id& id ) {
249  __TBB_STATIC_ASSERT(sizeof(id.my_id) <= sizeof(size_t), "Implementaion assumes that thread_id_type fits into machine word");
250  return tbb::tbb_hasher(id.my_id);
251  }
252 
253  // A workaround for lack of tbb::atomic<id> (which would require id to be POD in C++03).
254  friend id atomic_compare_and_swap(id& location, const id& value, const id& comparand){
255  return as_atomic(location.my_id).compare_and_swap(value.my_id, comparand.my_id);
256  }
257  }; // tbb_thread_v3::id
258 
259  tbb_thread_v3::id tbb_thread_v3::get_id() const __TBB_NOEXCEPT(true) {
260 #if _WIN32||_WIN64
261  return id(my_thread_id);
262 #else
263  return id(my_handle);
264 #endif // _WIN32||_WIN64
265  }
266 
271 
273  {
274  return x.my_id == y.my_id;
275  }
277  {
278  return x.my_id != y.my_id;
279  }
281  {
282  return x.my_id < y.my_id;
283  }
285  {
286  return x.my_id <= y.my_id;
287  }
289  {
290  return x.my_id > y.my_id;
291  }
293  {
294  return x.my_id >= y.my_id;
295  }
296 
297 } // namespace internal;
298 
300 typedef internal::tbb_thread_v3 tbb_thread;
301 
302 using internal::operator==;
303 using internal::operator!=;
304 using internal::operator<;
305 using internal::operator>;
306 using internal::operator<=;
307 using internal::operator>=;
308 
309 inline void move( tbb_thread& t1, tbb_thread& t2 ) {
310  internal::move_v3(t1, t2);
311 }
312 
313 inline void swap( internal::tbb_thread_v3& t1, internal::tbb_thread_v3& t2 ) __TBB_NOEXCEPT(true) {
314  std::swap(t1.my_handle, t2.my_handle);
315 #if _WIN32||_WIN64
316  std::swap(t1.my_thread_id, t2.my_thread_id);
317 #endif /* _WIN32||_WIN64 */
318 }
319 
320 namespace this_tbb_thread {
323  inline void yield() { internal::thread_yield_v3(); }
325  inline void sleep(const tick_count::interval_t &i) {
327  }
328 } // namespace this_tbb_thread
329 
330 } // namespace tbb
331 
332 #endif /* __TBB_tbb_thread_H */
Versioned thread class.
Definition: tbb_thread.h:112
size_t tbb_hasher(const T &t)
Hasher functions.
#define __TBB_NATIVE_THREAD_ROUTINE
Definition: tbb_thread.h:38
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
#define __TBB_STATIC_ASSERT(condition, msg)
Definition: tbb_stddef.h:536
bool operator==(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
tbb_thread_v3(F f, X x, Y y)
Constructs an object and executes f(x,y) in a new thread.
Definition: tbb_thread.h:146
void internal_wipe() __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:201
#define __TBB_NOEXCEPT(expression)
Definition: tbb_stddef.h:114
void swap(tbb_thread_v3 &t) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:174
friend size_t tbb_hasher(const tbb_thread_v3::id &id)
Definition: tbb_thread.h:248
void swap(internal::tbb_thread_v3 &t1, internal::tbb_thread_v3 &t2) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:313
void __TBB_EXPORTED_FUNC free_closure_v3(void *)
Free a closure allocated by allocate_closure_v3.
Definition: tbb_thread.cpp:45
friend id atomic_compare_and_swap(id &location, const id &value, const id &comparand)
Definition: tbb_thread.h:254
tbb_thread_v3 & operator=(tbb_thread_v3 &&x) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:160
tbb_thread_v3(F f)
Constructs an object and executes f() in a new thread.
Definition: tbb_thread.h:136
void swap(atomic< T > &lhs, atomic< T > &rhs)
Definition: atomic.h:539
pthread_t thread_id_type
Definition: tbb_thread.h:42
bool operator!=(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
thread_closure_1(const F &f, const X &x)
Definition: tbb_thread.h:95
void sleep(const tick_count::interval_t &i)
The current thread blocks at least until the time specified.
Definition: tbb_thread.h:325
thread_closure_2(const F &f, const X &x, const Y &y)
Definition: tbb_thread.h:108
static __TBB_NATIVE_THREAD_ROUTINE start_routine(void *c)
Routine passed to Windows&#39;s _beginthreadex by thread::internal_start() inside tbb.dll.
Definition: tbb_thread.h:89
bool operator>(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
bool operator<=(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
tbb_thread_v3(F f, X x)
Constructs an object and executes f(x) in a new thread.
Definition: tbb_thread.h:141
void internal_move(tbb_thread_v3 &x) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:207
void __TBB_EXPORTED_FUNC move_v3(tbb_thread_v3 &t1, tbb_thread_v3 &t2)
Definition: tbb_thread.cpp:151
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:102
#define __TBB_NATIVE_THREAD_ROUTINE_PTR(r)
Definition: tbb_thread.h:39
The graph class.
native_handle_type native_handle()
Definition: tbb_thread.h:182
void swap(concurrent_hash_map< Key, T, HashCompare, A > &a, concurrent_hash_map< Key, T, HashCompare, A > &b)
static __TBB_NATIVE_THREAD_ROUTINE start_routine(void *c)
Routine passed to Windows&#39;s _beginthreadex by thread::internal_start() inside tbb.dll.
Definition: tbb_thread.h:102
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 ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
void __TBB_EXPORTED_FUNC thread_yield_v3()
Definition: tbb_thread.cpp:163
tbb_thread::id get_id()
Definition: tbb_thread.h:321
tbb_thread_v3(tbb_thread_v3 &&x) __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:152
Relative time interval.
Definition: tick_count.h:41
native_handle_type my_handle
Definition: tbb_thread.h:196
#define __TBB_EXPORTED_FUNC
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:309
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
id get_id() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:259
internal::tbb_thread_v3 tbb_thread
Users reference thread class by name tbb_thread.
Definition: tbb_thread.h:300
bool operator>=(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
void *__TBB_EXPORTED_FUNC allocate_closure_v3(size_t size)
Allocate a closure.
Definition: tbb_thread.cpp:39
bool operator<(const vector_iterator< Container, T > &i, const vector_iterator< Container, U > &j)
Structure used to pass user function with 1 argument to thread.
Definition: tbb_thread.h:85
tbb_thread_v3() __TBB_NOEXCEPT(true)
Constructs a thread object that does not represent a thread of execution.
Definition: tbb_thread.h:129
id() __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:229
void yield()
Offers the operating system the opportunity to schedule another thread.
Definition: tbb_thread.h:323
atomic< T > & as_atomic(T &t)
Definition: atomic.h:547
bool joinable() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:175
tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3()
Definition: tbb_thread.cpp:143
static __TBB_NATIVE_THREAD_ROUTINE start_routine(void *c)
Definition: tbb_thread.h:76
void __TBB_EXPORTED_FUNC thread_sleep_v3(const tick_count::interval_t &i)
Definition: tbb_thread.cpp:168

Copyright © 2005-2019 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.