tbb_thread.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "internal/_deprecated_header_message_guard.h"
  14. #if !defined(__TBB_show_deprecation_message_tbb_thread_H) && defined(__TBB_show_deprecated_header_message)
  15. #define __TBB_show_deprecation_message_tbb_thread_H
  16. #pragma message("TBB Warning: tbb/tbb_thread.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
  17. #endif
  18. #if defined(__TBB_show_deprecated_header_message)
  19. #undef __TBB_show_deprecated_header_message
  20. #endif
  21. #ifndef __TBB_tbb_thread_H
  22. #define __TBB_tbb_thread_H
  23. #define __TBB_tbb_thread_H_include_area
  24. #include "internal/_warning_suppress_enable_notice.h"
  25. #include "tbb_stddef.h"
  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. #include "atomic.h"
  46. #include "internal/_tbb_hash_compare_impl.h"
  47. #include "tick_count.h"
  48. #include __TBB_STD_SWAP_HEADER
  49. #include <iosfwd>
  50. namespace tbb {
  51. namespace internal {
  52. class tbb_thread_v3;
  53. }
  54. inline void swap( internal::tbb_thread_v3& t1, internal::tbb_thread_v3& t2 ) __TBB_NOEXCEPT(true);
  55. namespace internal {
  56. //! Allocate a closure
  57. void* __TBB_EXPORTED_FUNC allocate_closure_v3( size_t size );
  58. //! Free a closure allocated by allocate_closure_v3
  59. void __TBB_EXPORTED_FUNC free_closure_v3( void* );
  60. struct thread_closure_base {
  61. void* operator new( size_t size ) {return allocate_closure_v3(size);}
  62. void operator delete( void* ptr ) {free_closure_v3(ptr);}
  63. };
  64. template<class F> struct thread_closure_0: thread_closure_base {
  65. F function;
  66. static __TBB_NATIVE_THREAD_ROUTINE start_routine( void* c ) {
  67. thread_closure_0 *self = static_cast<thread_closure_0*>(c);
  68. self->function();
  69. delete self;
  70. return 0;
  71. }
  72. thread_closure_0( const F& f ) : function(f) {}
  73. };
  74. //! Structure used to pass user function with 1 argument to thread.
  75. template<class F, class X> struct thread_closure_1: thread_closure_base {
  76. F function;
  77. X arg1;
  78. //! Routine passed to Windows's _beginthreadex by thread::internal_start() inside tbb.dll
  79. static __TBB_NATIVE_THREAD_ROUTINE start_routine( void* c ) {
  80. thread_closure_1 *self = static_cast<thread_closure_1*>(c);
  81. self->function(self->arg1);
  82. delete self;
  83. return 0;
  84. }
  85. thread_closure_1( const F& f, const X& x ) : function(f), arg1(x) {}
  86. };
  87. template<class F, class X, class Y> struct thread_closure_2: thread_closure_base {
  88. F function;
  89. X arg1;
  90. Y arg2;
  91. //! Routine passed to Windows's _beginthreadex by thread::internal_start() inside tbb.dll
  92. static __TBB_NATIVE_THREAD_ROUTINE start_routine( void* c ) {
  93. thread_closure_2 *self = static_cast<thread_closure_2*>(c);
  94. self->function(self->arg1, self->arg2);
  95. delete self;
  96. return 0;
  97. }
  98. thread_closure_2( const F& f, const X& x, const Y& y ) : function(f), arg1(x), arg2(y) {}
  99. };
  100. //! Versioned thread class.
  101. class tbb_thread_v3 {
  102. #if __TBB_IF_NO_COPY_CTOR_MOVE_SEMANTICS_BROKEN
  103. // Workaround for a compiler bug: declaring the copy constructor as public
  104. // enables use of the moving constructor.
  105. // The definition is not provided in order to prohibit copying.
  106. public:
  107. #endif
  108. tbb_thread_v3(const tbb_thread_v3&); // = delete; // Deny access
  109. public:
  110. #if _WIN32||_WIN64
  111. typedef HANDLE native_handle_type;
  112. #else
  113. typedef pthread_t native_handle_type;
  114. #endif // _WIN32||_WIN64
  115. class id;
  116. //! Constructs a thread object that does not represent a thread of execution.
  117. tbb_thread_v3() __TBB_NOEXCEPT(true) : my_handle(0)
  118. #if _WIN32||_WIN64
  119. , my_thread_id(0)
  120. #endif // _WIN32||_WIN64
  121. {}
  122. //! Constructs an object and executes f() in a new thread
  123. template <class F> explicit tbb_thread_v3(F f) {
  124. typedef internal::thread_closure_0<F> closure_type;
  125. internal_start(closure_type::start_routine, new closure_type(f));
  126. }
  127. //! Constructs an object and executes f(x) in a new thread
  128. template <class F, class X> tbb_thread_v3(F f, X x) {
  129. typedef internal::thread_closure_1<F,X> closure_type;
  130. internal_start(closure_type::start_routine, new closure_type(f,x));
  131. }
  132. //! Constructs an object and executes f(x,y) in a new thread
  133. template <class F, class X, class Y> tbb_thread_v3(F f, X x, Y y) {
  134. typedef internal::thread_closure_2<F,X,Y> closure_type;
  135. internal_start(closure_type::start_routine, new closure_type(f,x,y));
  136. }
  137. #if __TBB_CPP11_RVALUE_REF_PRESENT
  138. tbb_thread_v3(tbb_thread_v3&& x) __TBB_NOEXCEPT(true)
  139. : my_handle(x.my_handle)
  140. #if _WIN32||_WIN64
  141. , my_thread_id(x.my_thread_id)
  142. #endif
  143. {
  144. x.internal_wipe();
  145. }
  146. tbb_thread_v3& operator=(tbb_thread_v3&& x) __TBB_NOEXCEPT(true) {
  147. internal_move(x);
  148. return *this;
  149. }
  150. private:
  151. tbb_thread_v3& operator=(const tbb_thread_v3& x); // = delete;
  152. public:
  153. #else // __TBB_CPP11_RVALUE_REF_PRESENT
  154. tbb_thread_v3& operator=(tbb_thread_v3& x) {
  155. internal_move(x);
  156. return *this;
  157. }
  158. #endif // __TBB_CPP11_RVALUE_REF_PRESENT
  159. void swap( tbb_thread_v3& t ) __TBB_NOEXCEPT(true) {tbb::swap( *this, t );}
  160. bool joinable() const __TBB_NOEXCEPT(true) {return my_handle!=0; }
  161. //! The completion of the thread represented by *this happens before join() returns.
  162. void __TBB_EXPORTED_METHOD join();
  163. //! When detach() returns, *this no longer represents the possibly continuing thread of execution.
  164. void __TBB_EXPORTED_METHOD detach();
  165. ~tbb_thread_v3() {if( joinable() ) detach();}
  166. inline id get_id() const __TBB_NOEXCEPT(true);
  167. native_handle_type native_handle() { return my_handle; }
  168. //! The number of hardware thread contexts.
  169. /** Before TBB 3.0 U4 this methods returned the number of logical CPU in
  170. the system. Currently on Windows, Linux and FreeBSD it returns the
  171. number of logical CPUs available to the current process in accordance
  172. with its affinity mask.
  173. NOTE: The return value of this method never changes after its first
  174. invocation. This means that changes in the process affinity mask that
  175. took place after this method was first invoked will not affect the
  176. number of worker threads in the TBB worker threads pool. **/
  177. static unsigned __TBB_EXPORTED_FUNC hardware_concurrency() __TBB_NOEXCEPT(true);
  178. private:
  179. native_handle_type my_handle;
  180. #if _WIN32||_WIN64
  181. thread_id_type my_thread_id;
  182. #endif // _WIN32||_WIN64
  183. void internal_wipe() __TBB_NOEXCEPT(true) {
  184. my_handle = 0;
  185. #if _WIN32||_WIN64
  186. my_thread_id = 0;
  187. #endif
  188. }
  189. void internal_move(tbb_thread_v3& x) __TBB_NOEXCEPT(true) {
  190. if (joinable()) detach();
  191. my_handle = x.my_handle;
  192. #if _WIN32||_WIN64
  193. my_thread_id = x.my_thread_id;
  194. #endif // _WIN32||_WIN64
  195. x.internal_wipe();
  196. }
  197. /** Runs start_routine(closure) on another thread and sets my_handle to the handle of the created thread. */
  198. void __TBB_EXPORTED_METHOD internal_start( __TBB_NATIVE_THREAD_ROUTINE_PTR(start_routine),
  199. void* closure );
  200. friend void __TBB_EXPORTED_FUNC move_v3( tbb_thread_v3& t1, tbb_thread_v3& t2 );
  201. friend void tbb::swap( tbb_thread_v3& t1, tbb_thread_v3& t2 ) __TBB_NOEXCEPT(true);
  202. };
  203. class tbb_thread_v3::id {
  204. thread_id_type my_id;
  205. id( thread_id_type id_ ) : my_id(id_) {}
  206. friend class tbb_thread_v3;
  207. public:
  208. id() __TBB_NOEXCEPT(true) : my_id(0) {}
  209. friend bool operator==( tbb_thread_v3::id x, tbb_thread_v3::id y ) __TBB_NOEXCEPT(true);
  210. friend bool operator!=( tbb_thread_v3::id x, tbb_thread_v3::id y ) __TBB_NOEXCEPT(true);
  211. friend bool operator<( tbb_thread_v3::id x, tbb_thread_v3::id y ) __TBB_NOEXCEPT(true);
  212. friend bool operator<=( tbb_thread_v3::id x, tbb_thread_v3::id y ) __TBB_NOEXCEPT(true);
  213. friend bool operator>( tbb_thread_v3::id x, tbb_thread_v3::id y ) __TBB_NOEXCEPT(true);
  214. friend bool operator>=( tbb_thread_v3::id x, tbb_thread_v3::id y ) __TBB_NOEXCEPT(true);
  215. template<class charT, class traits>
  216. friend std::basic_ostream<charT, traits>&
  217. operator<< (std::basic_ostream<charT, traits> &out,
  218. tbb_thread_v3::id id)
  219. {
  220. out << id.my_id;
  221. return out;
  222. }
  223. friend tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3();
  224. friend inline size_t tbb_hasher( const tbb_thread_v3::id& id ) {
  225. __TBB_STATIC_ASSERT(sizeof(id.my_id) <= sizeof(size_t), "Implementation assumes that thread_id_type fits into machine word");
  226. return tbb::tbb_hasher(id.my_id);
  227. }
  228. // A workaround for lack of tbb::atomic<id> (which would require id to be POD in C++03).
  229. friend id atomic_compare_and_swap(id& location, const id& value, const id& comparand){
  230. return as_atomic(location.my_id).compare_and_swap(value.my_id, comparand.my_id);
  231. }
  232. }; // tbb_thread_v3::id
  233. tbb_thread_v3::id tbb_thread_v3::get_id() const __TBB_NOEXCEPT(true) {
  234. #if _WIN32||_WIN64
  235. return id(my_thread_id);
  236. #else
  237. return id(my_handle);
  238. #endif // _WIN32||_WIN64
  239. }
  240. void __TBB_EXPORTED_FUNC move_v3( tbb_thread_v3& t1, tbb_thread_v3& t2 );
  241. tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3();
  242. void __TBB_EXPORTED_FUNC thread_yield_v3();
  243. void __TBB_EXPORTED_FUNC thread_sleep_v3(const tick_count::interval_t &i);
  244. inline bool operator==(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
  245. {
  246. return x.my_id == y.my_id;
  247. }
  248. inline bool operator!=(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
  249. {
  250. return x.my_id != y.my_id;
  251. }
  252. inline bool operator<(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
  253. {
  254. return x.my_id < y.my_id;
  255. }
  256. inline bool operator<=(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
  257. {
  258. return x.my_id <= y.my_id;
  259. }
  260. inline bool operator>(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
  261. {
  262. return x.my_id > y.my_id;
  263. }
  264. inline bool operator>=(tbb_thread_v3::id x, tbb_thread_v3::id y) __TBB_NOEXCEPT(true)
  265. {
  266. return x.my_id >= y.my_id;
  267. }
  268. } // namespace internal;
  269. //! Users reference thread class by name tbb_thread
  270. __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::thread is deprecated, use std::thread") typedef internal::tbb_thread_v3 tbb_thread;
  271. using internal::operator==;
  272. using internal::operator!=;
  273. using internal::operator<;
  274. using internal::operator>;
  275. using internal::operator<=;
  276. using internal::operator>=;
  277. inline void move( tbb_thread& t1, tbb_thread& t2 ) {
  278. internal::move_v3(t1, t2);
  279. }
  280. inline void swap( internal::tbb_thread_v3& t1, internal::tbb_thread_v3& t2 ) __TBB_NOEXCEPT(true) {
  281. std::swap(t1.my_handle, t2.my_handle);
  282. #if _WIN32||_WIN64
  283. std::swap(t1.my_thread_id, t2.my_thread_id);
  284. #endif /* _WIN32||_WIN64 */
  285. }
  286. namespace this_tbb_thread {
  287. __TBB_DEPRECATED_IN_VERBOSE_MODE inline tbb_thread::id get_id() { return internal::thread_get_id_v3(); }
  288. //! Offers the operating system the opportunity to schedule another thread.
  289. __TBB_DEPRECATED_IN_VERBOSE_MODE inline void yield() { internal::thread_yield_v3(); }
  290. //! The current thread blocks at least until the time specified.
  291. __TBB_DEPRECATED_IN_VERBOSE_MODE inline void sleep(const tick_count::interval_t &i) {
  292. internal::thread_sleep_v3(i);
  293. }
  294. } // namespace this_tbb_thread
  295. } // namespace tbb
  296. #include "internal/_warning_suppress_disable_notice.h"
  297. #undef __TBB_tbb_thread_H_include_area
  298. #endif /* __TBB_tbb_thread_H */