You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
11 KiB

  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_tbb_thread_H
  24. #define __TBB_tbb_thread_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. #if __TBB_WIN8UI_SUPPORT
  31. typedef size_t thread_id_type;
  32. #else // __TBB_WIN8UI_SUPPORT
  33. typedef DWORD thread_id_type;
  34. #endif // __TBB_WIN8UI_SUPPORT
  35. #else
  36. #define __TBB_NATIVE_THREAD_ROUTINE void*
  37. #define __TBB_NATIVE_THREAD_ROUTINE_PTR(r) void* (*r)( void* )
  38. #include <pthread.h>
  39. #endif // _WIN32||_WIN64
  40. #include "tick_count.h"
  41. #if !TBB_USE_EXCEPTIONS && _MSC_VER
  42. // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
  43. #pragma warning (push)
  44. #pragma warning (disable: 4530)
  45. #endif
  46. #include <iosfwd>
  47. #if !TBB_USE_EXCEPTIONS && _MSC_VER
  48. #pragma warning (pop)
  49. #endif
  50. namespace tbb {
  51. //! @cond INTERNAL
  52. namespace internal {
  53. class tbb_thread_v3;
  54. } // namespace internal
  55. inline void swap( internal::tbb_thread_v3& t1, internal::tbb_thread_v3& t2 );
  56. namespace internal {
  57. //! Allocate a closure
  58. void* __TBB_EXPORTED_FUNC allocate_closure_v3( size_t size );
  59. //! Free a closure allocated by allocate_closure_v3
  60. void __TBB_EXPORTED_FUNC free_closure_v3( void* );
  61. struct thread_closure_base {
  62. void* operator new( size_t size ) {return allocate_closure_v3(size);}
  63. void operator delete( void* ptr ) {free_closure_v3(ptr);}
  64. };
  65. template<class F> struct thread_closure_0: thread_closure_base {
  66. F function;
  67. static __TBB_NATIVE_THREAD_ROUTINE start_routine( void* c ) {
  68. thread_closure_0 *self = static_cast<thread_closure_0*>(c);
  69. self->function();
  70. delete self;
  71. return 0;
  72. }
  73. thread_closure_0( const F& f ) : function(f) {}
  74. };
  75. //! Structure used to pass user function with 1 argument to thread.
  76. template<class F, class X> struct thread_closure_1: thread_closure_base {
  77. F function;
  78. X arg1;
  79. //! Routine passed to Windows's _beginthreadex by thread::internal_start() inside tbb.dll
  80. static __TBB_NATIVE_THREAD_ROUTINE start_routine( void* c ) {
  81. thread_closure_1 *self = static_cast<thread_closure_1*>(c);
  82. self->function(self->arg1);
  83. delete self;
  84. return 0;
  85. }
  86. thread_closure_1( const F& f, const X& x ) : function(f), arg1(x) {}
  87. };
  88. template<class F, class X, class Y> struct thread_closure_2: thread_closure_base {
  89. F function;
  90. X arg1;
  91. Y arg2;
  92. //! Routine passed to Windows's _beginthreadex by thread::internal_start() inside tbb.dll
  93. static __TBB_NATIVE_THREAD_ROUTINE start_routine( void* c ) {
  94. thread_closure_2 *self = static_cast<thread_closure_2*>(c);
  95. self->function(self->arg1, self->arg2);
  96. delete self;
  97. return 0;
  98. }
  99. thread_closure_2( const F& f, const X& x, const Y& y ) : function(f), arg1(x), arg2(y) {}
  100. };
  101. //! Versioned thread class.
  102. class tbb_thread_v3 {
  103. tbb_thread_v3(const tbb_thread_v3&); // = delete; // Deny access
  104. public:
  105. #if _WIN32||_WIN64
  106. typedef HANDLE native_handle_type;
  107. #else
  108. typedef pthread_t native_handle_type;
  109. #endif // _WIN32||_WIN64
  110. class id;
  111. //! Constructs a thread object that does not represent a thread of execution.
  112. tbb_thread_v3() : my_handle(0)
  113. #if _WIN32||_WIN64
  114. , my_thread_id(0)
  115. #endif // _WIN32||_WIN64
  116. {}
  117. //! Constructs an object and executes f() in a new thread
  118. template <class F> explicit tbb_thread_v3(F f) {
  119. typedef internal::thread_closure_0<F> closure_type;
  120. internal_start(closure_type::start_routine, new closure_type(f));
  121. }
  122. //! Constructs an object and executes f(x) in a new thread
  123. template <class F, class X> tbb_thread_v3(F f, X x) {
  124. typedef internal::thread_closure_1<F,X> closure_type;
  125. internal_start(closure_type::start_routine, new closure_type(f,x));
  126. }
  127. //! Constructs an object and executes f(x,y) in a new thread
  128. template <class F, class X, class Y> tbb_thread_v3(F f, X x, Y y) {
  129. typedef internal::thread_closure_2<F,X,Y> closure_type;
  130. internal_start(closure_type::start_routine, new closure_type(f,x,y));
  131. }
  132. tbb_thread_v3& operator=(tbb_thread_v3& x) {
  133. if (joinable()) detach();
  134. my_handle = x.my_handle;
  135. x.my_handle = 0;
  136. #if _WIN32||_WIN64
  137. my_thread_id = x.my_thread_id;
  138. x.my_thread_id = 0;
  139. #endif // _WIN32||_WIN64
  140. return *this;
  141. }
  142. void swap( tbb_thread_v3& t ) {tbb::swap( *this, t );}
  143. bool joinable() const {return my_handle!=0; }
  144. //! The completion of the thread represented by *this happens before join() returns.
  145. void __TBB_EXPORTED_METHOD join();
  146. //! When detach() returns, *this no longer represents the possibly continuing thread of execution.
  147. void __TBB_EXPORTED_METHOD detach();
  148. ~tbb_thread_v3() {if( joinable() ) detach();}
  149. inline id get_id() const;
  150. native_handle_type native_handle() { return my_handle; }
  151. //! The number of hardware thread contexts.
  152. /** Before TBB 3.0 U4 this methods returned the number of logical CPU in
  153. the system. Currently on Windows, Linux and FreeBSD it returns the
  154. number of logical CPUs available to the current process in accordance
  155. with its affinity mask.
  156. NOTE: The return value of this method never changes after its first
  157. invocation. This means that changes in the process affinity mask that
  158. took place after this method was first invoked will not affect the
  159. number of worker threads in the TBB worker threads pool. **/
  160. static unsigned __TBB_EXPORTED_FUNC hardware_concurrency();
  161. private:
  162. native_handle_type my_handle;
  163. #if _WIN32||_WIN64
  164. thread_id_type my_thread_id;
  165. #endif // _WIN32||_WIN64
  166. /** Runs start_routine(closure) on another thread and sets my_handle to the handle of the created thread. */
  167. void __TBB_EXPORTED_METHOD internal_start( __TBB_NATIVE_THREAD_ROUTINE_PTR(start_routine),
  168. void* closure );
  169. friend void __TBB_EXPORTED_FUNC move_v3( tbb_thread_v3& t1, tbb_thread_v3& t2 );
  170. friend void tbb::swap( tbb_thread_v3& t1, tbb_thread_v3& t2 );
  171. };
  172. class tbb_thread_v3::id {
  173. #if _WIN32||_WIN64
  174. thread_id_type my_id;
  175. id( thread_id_type id_ ) : my_id(id_) {}
  176. #else
  177. pthread_t my_id;
  178. id( pthread_t id_ ) : my_id(id_) {}
  179. #endif // _WIN32||_WIN64
  180. friend class tbb_thread_v3;
  181. public:
  182. id() : my_id(0) {}
  183. friend bool operator==( tbb_thread_v3::id x, tbb_thread_v3::id y );
  184. friend bool operator!=( tbb_thread_v3::id x, tbb_thread_v3::id y );
  185. friend bool operator<( tbb_thread_v3::id x, tbb_thread_v3::id y );
  186. friend bool operator<=( tbb_thread_v3::id x, tbb_thread_v3::id y );
  187. friend bool operator>( tbb_thread_v3::id x, tbb_thread_v3::id y );
  188. friend bool operator>=( tbb_thread_v3::id x, tbb_thread_v3::id y );
  189. template<class charT, class traits>
  190. friend std::basic_ostream<charT, traits>&
  191. operator<< (std::basic_ostream<charT, traits> &out,
  192. tbb_thread_v3::id id)
  193. {
  194. out << id.my_id;
  195. return out;
  196. }
  197. friend tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3();
  198. }; // tbb_thread_v3::id
  199. tbb_thread_v3::id tbb_thread_v3::get_id() const {
  200. #if _WIN32||_WIN64
  201. return id(my_thread_id);
  202. #else
  203. return id(my_handle);
  204. #endif // _WIN32||_WIN64
  205. }
  206. void __TBB_EXPORTED_FUNC move_v3( tbb_thread_v3& t1, tbb_thread_v3& t2 );
  207. tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3();
  208. void __TBB_EXPORTED_FUNC thread_yield_v3();
  209. void __TBB_EXPORTED_FUNC thread_sleep_v3(const tick_count::interval_t &i);
  210. inline bool operator==(tbb_thread_v3::id x, tbb_thread_v3::id y)
  211. {
  212. return x.my_id == y.my_id;
  213. }
  214. inline bool operator!=(tbb_thread_v3::id x, tbb_thread_v3::id y)
  215. {
  216. return x.my_id != y.my_id;
  217. }
  218. inline bool operator<(tbb_thread_v3::id x, tbb_thread_v3::id y)
  219. {
  220. return x.my_id < y.my_id;
  221. }
  222. inline bool operator<=(tbb_thread_v3::id x, tbb_thread_v3::id y)
  223. {
  224. return x.my_id <= y.my_id;
  225. }
  226. inline bool operator>(tbb_thread_v3::id x, tbb_thread_v3::id y)
  227. {
  228. return x.my_id > y.my_id;
  229. }
  230. inline bool operator>=(tbb_thread_v3::id x, tbb_thread_v3::id y)
  231. {
  232. return x.my_id >= y.my_id;
  233. }
  234. } // namespace internal;
  235. //! Users reference thread class by name tbb_thread
  236. typedef internal::tbb_thread_v3 tbb_thread;
  237. using internal::operator==;
  238. using internal::operator!=;
  239. using internal::operator<;
  240. using internal::operator>;
  241. using internal::operator<=;
  242. using internal::operator>=;
  243. inline void move( tbb_thread& t1, tbb_thread& t2 ) {
  244. internal::move_v3(t1, t2);
  245. }
  246. inline void swap( internal::tbb_thread_v3& t1, internal::tbb_thread_v3& t2 ) {
  247. tbb::tbb_thread::native_handle_type h = t1.my_handle;
  248. t1.my_handle = t2.my_handle;
  249. t2.my_handle = h;
  250. #if _WIN32||_WIN64
  251. thread_id_type i = t1.my_thread_id;
  252. t1.my_thread_id = t2.my_thread_id;
  253. t2.my_thread_id = i;
  254. #endif /* _WIN32||_WIN64 */
  255. }
  256. namespace this_tbb_thread {
  257. inline tbb_thread::id get_id() { return internal::thread_get_id_v3(); }
  258. //! Offers the operating system the opportunity to schedule another thread.
  259. inline void yield() { internal::thread_yield_v3(); }
  260. //! The current thread blocks at least until the time specified.
  261. inline void sleep(const tick_count::interval_t &i) {
  262. internal::thread_sleep_v3(i);
  263. }
  264. } // namespace this_tbb_thread
  265. } // namespace tbb
  266. #endif /* __TBB_tbb_thread_H */