thread.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. #ifndef BOOST_THREAD_THREAD_COMMON_HPP
  2. #define BOOST_THREAD_THREAD_COMMON_HPP
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // (C) Copyright 2007-2010 Anthony Williams
  7. // (C) Copyright 2011-2012 Vicente J. Botet Escriba
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/predef/platform.h>
  10. #include <boost/thread/exceptions.hpp>
  11. #ifndef BOOST_NO_IOSTREAM
  12. #include <ostream>
  13. #endif
  14. #include <boost/thread/detail/move.hpp>
  15. #include <boost/thread/mutex.hpp>
  16. #if defined BOOST_THREAD_USES_DATETIME
  17. #include <boost/thread/xtime.hpp>
  18. #endif
  19. #include <boost/thread/detail/thread_heap_alloc.hpp>
  20. #include <boost/thread/detail/make_tuple_indices.hpp>
  21. #include <boost/thread/detail/invoke.hpp>
  22. #include <boost/thread/detail/is_convertible.hpp>
  23. #include <boost/assert.hpp>
  24. #include <list>
  25. #include <algorithm>
  26. #include <boost/core/ref.hpp>
  27. #include <boost/cstdint.hpp>
  28. #include <boost/bind.hpp>
  29. #include <stdlib.h>
  30. #include <memory>
  31. #include <boost/core/enable_if.hpp>
  32. #include <boost/type_traits/remove_reference.hpp>
  33. #include <boost/io/ios_state.hpp>
  34. #include <boost/type_traits/is_same.hpp>
  35. #include <boost/type_traits/decay.hpp>
  36. #include <boost/functional/hash.hpp>
  37. #include <boost/thread/detail/platform_time.hpp>
  38. #ifdef BOOST_THREAD_USES_CHRONO
  39. #include <boost/chrono/system_clocks.hpp>
  40. #include <boost/chrono/ceil.hpp>
  41. #endif
  42. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  43. #include <tuple>
  44. #endif
  45. #include <boost/config/abi_prefix.hpp>
  46. #ifdef BOOST_MSVC
  47. #pragma warning(push)
  48. #pragma warning(disable:4251)
  49. #endif
  50. namespace boost
  51. {
  52. namespace detail
  53. {
  54. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  55. template<typename F, class ...ArgTypes>
  56. class thread_data:
  57. public detail::thread_data_base
  58. {
  59. public:
  60. BOOST_THREAD_NO_COPYABLE(thread_data)
  61. thread_data(BOOST_THREAD_RV_REF(F) f_, BOOST_THREAD_RV_REF(ArgTypes)... args_):
  62. fp(boost::forward<F>(f_), boost::forward<ArgTypes>(args_)...)
  63. {}
  64. template <std::size_t ...Indices>
  65. void run2(tuple_indices<Indices...>)
  66. {
  67. detail::invoke(std::move(std::get<0>(fp)), std::move(std::get<Indices>(fp))...);
  68. }
  69. void run()
  70. {
  71. typedef typename make_tuple_indices<std::tuple_size<std::tuple<F, ArgTypes...> >::value, 1>::type index_type;
  72. run2(index_type());
  73. }
  74. private:
  75. std::tuple<typename decay<F>::type, typename decay<ArgTypes>::type...> fp;
  76. };
  77. #else // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  78. template<typename F>
  79. class thread_data:
  80. public detail::thread_data_base
  81. {
  82. public:
  83. BOOST_THREAD_NO_COPYABLE(thread_data)
  84. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  85. thread_data(BOOST_THREAD_RV_REF(F) f_):
  86. f(boost::forward<F>(f_))
  87. {}
  88. // This overloading must be removed if we want the packaged_task's tests to pass.
  89. // thread_data(F& f_):
  90. // f(f_)
  91. // {}
  92. #else
  93. thread_data(BOOST_THREAD_RV_REF(F) f_):
  94. f(f_)
  95. {}
  96. thread_data(F f_):
  97. f(f_)
  98. {}
  99. #endif
  100. //thread_data() {}
  101. void run()
  102. {
  103. f();
  104. }
  105. private:
  106. F f;
  107. };
  108. template<typename F>
  109. class thread_data<boost::reference_wrapper<F> >:
  110. public detail::thread_data_base
  111. {
  112. private:
  113. F& f;
  114. public:
  115. BOOST_THREAD_NO_COPYABLE(thread_data)
  116. thread_data(boost::reference_wrapper<F> f_):
  117. f(f_)
  118. {}
  119. void run()
  120. {
  121. f();
  122. }
  123. };
  124. template<typename F>
  125. class thread_data<const boost::reference_wrapper<F> >:
  126. public detail::thread_data_base
  127. {
  128. private:
  129. F& f;
  130. public:
  131. BOOST_THREAD_NO_COPYABLE(thread_data)
  132. thread_data(const boost::reference_wrapper<F> f_):
  133. f(f_)
  134. {}
  135. void run()
  136. {
  137. f();
  138. }
  139. };
  140. #endif
  141. }
  142. class BOOST_THREAD_DECL thread
  143. {
  144. public:
  145. typedef thread_attributes attributes;
  146. BOOST_THREAD_MOVABLE_ONLY(thread)
  147. private:
  148. struct dummy;
  149. void release_handle();
  150. detail::thread_data_ptr thread_info;
  151. private:
  152. bool start_thread_noexcept();
  153. bool start_thread_noexcept(const attributes& attr);
  154. void start_thread()
  155. {
  156. if (!start_thread_noexcept())
  157. {
  158. boost::throw_exception(thread_resource_error());
  159. }
  160. }
  161. void start_thread(const attributes& attr)
  162. {
  163. if (!start_thread_noexcept(attr))
  164. {
  165. boost::throw_exception(thread_resource_error());
  166. }
  167. }
  168. explicit thread(detail::thread_data_ptr data);
  169. detail::thread_data_ptr get_thread_info BOOST_PREVENT_MACRO_SUBSTITUTION () const;
  170. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  171. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  172. template<typename F, class ...ArgTypes>
  173. static inline detail::thread_data_ptr make_thread_info(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_RV_REF(ArgTypes)... args)
  174. {
  175. return detail::thread_data_ptr(detail::heap_new<
  176. detail::thread_data<typename boost::remove_reference<F>::type, ArgTypes...>
  177. >(
  178. boost::forward<F>(f), boost::forward<ArgTypes>(args)...
  179. )
  180. );
  181. }
  182. #else
  183. template<typename F>
  184. static inline detail::thread_data_ptr make_thread_info(BOOST_THREAD_RV_REF(F) f)
  185. {
  186. return detail::thread_data_ptr(detail::heap_new<detail::thread_data<typename boost::remove_reference<F>::type> >(
  187. boost::forward<F>(f)));
  188. }
  189. #endif
  190. static inline detail::thread_data_ptr make_thread_info(void (*f)())
  191. {
  192. return detail::thread_data_ptr(detail::heap_new<detail::thread_data<void(*)()> >(
  193. boost::forward<void(*)()>(f)));
  194. }
  195. #else
  196. template<typename F>
  197. static inline detail::thread_data_ptr make_thread_info(F f
  198. , typename disable_if_c<
  199. //boost::thread_detail::is_convertible<F&,BOOST_THREAD_RV_REF(F)>::value ||
  200. is_same<typename decay<F>::type, thread>::value,
  201. dummy* >::type=0
  202. )
  203. {
  204. return detail::thread_data_ptr(detail::heap_new<detail::thread_data<F> >(f));
  205. }
  206. template<typename F>
  207. static inline detail::thread_data_ptr make_thread_info(BOOST_THREAD_RV_REF(F) f)
  208. {
  209. return detail::thread_data_ptr(detail::heap_new<detail::thread_data<F> >(f));
  210. }
  211. #endif
  212. public:
  213. #if 0 // This should not be needed anymore. Use instead BOOST_THREAD_MAKE_RV_REF.
  214. #if BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100)
  215. thread(const volatile thread&);
  216. #endif
  217. #endif
  218. thread() BOOST_NOEXCEPT;
  219. ~thread()
  220. {
  221. #if defined BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
  222. if (joinable()) {
  223. std::terminate();
  224. }
  225. #else
  226. detach();
  227. #endif
  228. }
  229. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  230. template <
  231. class F
  232. >
  233. explicit thread(BOOST_THREAD_RV_REF(F) f
  234. //, typename disable_if<is_same<typename decay<F>::type, thread>, dummy* >::type=0
  235. ):
  236. thread_info(make_thread_info(thread_detail::decay_copy(boost::forward<F>(f))))
  237. {
  238. start_thread();
  239. }
  240. template <
  241. class F
  242. >
  243. thread(attributes const& attrs, BOOST_THREAD_RV_REF(F) f):
  244. thread_info(make_thread_info(thread_detail::decay_copy(boost::forward<F>(f))))
  245. {
  246. start_thread(attrs);
  247. }
  248. #else
  249. #ifdef BOOST_NO_SFINAE
  250. template <class F>
  251. explicit thread(F f):
  252. thread_info(make_thread_info(f))
  253. {
  254. start_thread();
  255. }
  256. template <class F>
  257. thread(attributes const& attrs, F f):
  258. thread_info(make_thread_info(f))
  259. {
  260. start_thread(attrs);
  261. }
  262. #else
  263. template <class F>
  264. explicit thread(F f
  265. , typename disable_if_c<
  266. boost::thread_detail::is_rv<F>::value // todo as a thread_detail::is_rv
  267. //boost::thread_detail::is_convertible<F&,BOOST_THREAD_RV_REF(F)>::value
  268. //|| is_same<typename decay<F>::type, thread>::value
  269. , dummy* >::type=0
  270. ):
  271. thread_info(make_thread_info(f))
  272. {
  273. start_thread();
  274. }
  275. template <class F>
  276. thread(attributes const& attrs, F f
  277. , typename disable_if<boost::thread_detail::is_rv<F>, dummy* >::type=0
  278. //, typename disable_if<boost::thread_detail::is_convertible<F&,BOOST_THREAD_RV_REF(F) >, dummy* >::type=0
  279. ):
  280. thread_info(make_thread_info(f))
  281. {
  282. start_thread(attrs);
  283. }
  284. #endif
  285. template <class F>
  286. explicit thread(BOOST_THREAD_RV_REF(F) f
  287. , typename disable_if<is_same<typename decay<F>::type, thread>, dummy* >::type=0
  288. ):
  289. #ifdef BOOST_THREAD_USES_MOVE
  290. thread_info(make_thread_info(boost::move<F>(f))) // todo : Add forward
  291. #else
  292. thread_info(make_thread_info(f)) // todo : Add forward
  293. #endif
  294. {
  295. start_thread();
  296. }
  297. template <class F>
  298. thread(attributes const& attrs, BOOST_THREAD_RV_REF(F) f):
  299. #ifdef BOOST_THREAD_USES_MOVE
  300. thread_info(make_thread_info(boost::move<F>(f))) // todo : Add forward
  301. #else
  302. thread_info(make_thread_info(f)) // todo : Add forward
  303. #endif
  304. {
  305. start_thread(attrs);
  306. }
  307. #endif
  308. thread(BOOST_THREAD_RV_REF(thread) x) BOOST_NOEXCEPT
  309. {
  310. thread_info=BOOST_THREAD_RV(x).thread_info;
  311. BOOST_THREAD_RV(x).thread_info.reset();
  312. }
  313. #if 0 // This should not be needed anymore. Use instead BOOST_THREAD_MAKE_RV_REF.
  314. #if BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100)
  315. thread& operator=(thread x)
  316. {
  317. swap(x);
  318. return *this;
  319. }
  320. #endif
  321. #endif
  322. thread& operator=(BOOST_THREAD_RV_REF(thread) other) BOOST_NOEXCEPT
  323. {
  324. #if defined BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE
  325. if (joinable()) std::terminate();
  326. #else
  327. detach();
  328. #endif
  329. thread_info=BOOST_THREAD_RV(other).thread_info;
  330. BOOST_THREAD_RV(other).thread_info.reset();
  331. return *this;
  332. }
  333. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  334. template <class F, class Arg, class ...Args>
  335. thread(F&& f, Arg&& arg, Args&&... args) :
  336. thread_info(make_thread_info(
  337. thread_detail::decay_copy(boost::forward<F>(f)),
  338. thread_detail::decay_copy(boost::forward<Arg>(arg)),
  339. thread_detail::decay_copy(boost::forward<Args>(args))...)
  340. )
  341. {
  342. start_thread();
  343. }
  344. template <class F, class Arg, class ...Args>
  345. thread(attributes const& attrs, F&& f, Arg&& arg, Args&&... args) :
  346. thread_info(make_thread_info(
  347. thread_detail::decay_copy(boost::forward<F>(f)),
  348. thread_detail::decay_copy(boost::forward<Arg>(arg)),
  349. thread_detail::decay_copy(boost::forward<Args>(args))...)
  350. )
  351. {
  352. start_thread(attrs);
  353. }
  354. #else
  355. template <class F,class A1>
  356. thread(F f,A1 a1,typename disable_if<boost::thread_detail::is_convertible<F&,thread_attributes >, dummy* >::type=0):
  357. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1)))
  358. {
  359. start_thread();
  360. }
  361. template <class F,class A1,class A2>
  362. thread(F f,A1 a1,A2 a2):
  363. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2)))
  364. {
  365. start_thread();
  366. }
  367. template <class F,class A1,class A2,class A3>
  368. thread(F f,A1 a1,A2 a2,A3 a3):
  369. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3)))
  370. {
  371. start_thread();
  372. }
  373. template <class F,class A1,class A2,class A3,class A4>
  374. thread(F f,A1 a1,A2 a2,A3 a3,A4 a4):
  375. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4)))
  376. {
  377. start_thread();
  378. }
  379. template <class F,class A1,class A2,class A3,class A4,class A5>
  380. thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5):
  381. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5)))
  382. {
  383. start_thread();
  384. }
  385. template <class F,class A1,class A2,class A3,class A4,class A5,class A6>
  386. thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6):
  387. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6)))
  388. {
  389. start_thread();
  390. }
  391. template <class F,class A1,class A2,class A3,class A4,class A5,class A6,class A7>
  392. thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7):
  393. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6,a7)))
  394. {
  395. start_thread();
  396. }
  397. template <class F,class A1,class A2,class A3,class A4,class A5,class A6,class A7,class A8>
  398. thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7,A8 a8):
  399. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6,a7,a8)))
  400. {
  401. start_thread();
  402. }
  403. template <class F,class A1,class A2,class A3,class A4,class A5,class A6,class A7,class A8,class A9>
  404. thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7,A8 a8,A9 a9):
  405. thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6,a7,a8,a9)))
  406. {
  407. start_thread();
  408. }
  409. #endif
  410. void swap(thread& x) BOOST_NOEXCEPT
  411. {
  412. thread_info.swap(x.thread_info);
  413. }
  414. class id;
  415. id get_id() const BOOST_NOEXCEPT;
  416. bool joinable() const BOOST_NOEXCEPT;
  417. private:
  418. bool join_noexcept();
  419. bool do_try_join_until_noexcept(detail::internal_platform_timepoint const &timeout, bool& res);
  420. bool do_try_join_until(detail::internal_platform_timepoint const &timeout);
  421. public:
  422. void join();
  423. #ifdef BOOST_THREAD_USES_CHRONO
  424. template <class Duration>
  425. bool try_join_until(const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
  426. {
  427. return do_try_join_until(boost::detail::internal_platform_timepoint(t));
  428. }
  429. template <class Clock, class Duration>
  430. bool try_join_until(const chrono::time_point<Clock, Duration>& t)
  431. {
  432. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  433. common_duration d(t - Clock::now());
  434. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  435. while ( ! try_join_until(detail::internal_chrono_clock::now() + d) )
  436. {
  437. d = t - Clock::now();
  438. if ( d <= common_duration::zero() ) return false; // timeout occurred
  439. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  440. }
  441. return true;
  442. }
  443. template <class Rep, class Period>
  444. bool try_join_for(const chrono::duration<Rep, Period>& rel_time)
  445. {
  446. return try_join_until(chrono::steady_clock::now() + rel_time);
  447. }
  448. #endif
  449. #if defined BOOST_THREAD_USES_DATETIME
  450. bool timed_join(const system_time& abs_time)
  451. {
  452. const detail::real_platform_timepoint ts(abs_time);
  453. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  454. detail::platform_duration d(ts - detail::real_platform_clock::now());
  455. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  456. while ( ! do_try_join_until(detail::internal_platform_clock::now() + d) )
  457. {
  458. d = ts - detail::real_platform_clock::now();
  459. if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
  460. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  461. }
  462. return true;
  463. #else
  464. return do_try_join_until(ts);
  465. #endif
  466. }
  467. template<typename TimeDuration>
  468. bool timed_join(TimeDuration const& rel_time)
  469. {
  470. detail::platform_duration d(rel_time);
  471. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  472. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  473. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  474. while ( ! do_try_join_until(detail::internal_platform_clock::now() + d) )
  475. {
  476. d = ts - detail::mono_platform_clock::now();
  477. if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
  478. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  479. }
  480. return true;
  481. #else
  482. return do_try_join_until(detail::internal_platform_clock::now() + d);
  483. #endif
  484. }
  485. #endif
  486. void detach();
  487. static unsigned hardware_concurrency() BOOST_NOEXCEPT;
  488. static unsigned physical_concurrency() BOOST_NOEXCEPT;
  489. #define BOOST_THREAD_DEFINES_THREAD_NATIVE_HANDLE
  490. typedef detail::thread_data_base::native_handle_type native_handle_type;
  491. native_handle_type native_handle();
  492. #if defined BOOST_THREAD_PROVIDES_THREAD_EQ
  493. // Use thread::id when comparisions are needed
  494. // backwards compatibility
  495. bool operator==(const thread& other) const;
  496. bool operator!=(const thread& other) const;
  497. #endif
  498. #if defined BOOST_THREAD_USES_DATETIME
  499. static inline void yield() BOOST_NOEXCEPT
  500. {
  501. this_thread::yield();
  502. }
  503. static inline void sleep(const system_time& xt)
  504. {
  505. this_thread::sleep(xt);
  506. }
  507. #endif
  508. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  509. // extensions
  510. void interrupt();
  511. bool interruption_requested() const BOOST_NOEXCEPT;
  512. #endif
  513. };
  514. inline void swap(thread& lhs,thread& rhs) BOOST_NOEXCEPT
  515. {
  516. return lhs.swap(rhs);
  517. }
  518. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  519. inline thread&& move(thread& t) BOOST_NOEXCEPT
  520. {
  521. return static_cast<thread&&>(t);
  522. }
  523. #endif
  524. BOOST_THREAD_DCL_MOVABLE(thread)
  525. namespace this_thread
  526. {
  527. #ifdef BOOST_THREAD_PLATFORM_PTHREAD
  528. thread::id get_id() BOOST_NOEXCEPT;
  529. #else
  530. thread::id BOOST_THREAD_DECL get_id() BOOST_NOEXCEPT;
  531. #endif
  532. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  533. void BOOST_THREAD_DECL interruption_point();
  534. bool BOOST_THREAD_DECL interruption_enabled() BOOST_NOEXCEPT;
  535. bool BOOST_THREAD_DECL interruption_requested() BOOST_NOEXCEPT;
  536. #endif
  537. #if defined BOOST_THREAD_USES_DATETIME
  538. inline BOOST_SYMBOL_VISIBLE void sleep(xtime const& abs_time)
  539. {
  540. sleep(system_time(abs_time));
  541. }
  542. #endif
  543. }
  544. class BOOST_SYMBOL_VISIBLE thread::id
  545. {
  546. private:
  547. friend inline
  548. std::size_t
  549. hash_value(const thread::id &v)
  550. {
  551. #if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
  552. return hash_value(v.thread_data);
  553. #else
  554. return hash_value(v.thread_data.get());
  555. #endif
  556. }
  557. #if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
  558. #if defined(BOOST_THREAD_PLATFORM_WIN32)
  559. typedef unsigned int data;
  560. #else
  561. typedef thread::native_handle_type data;
  562. #endif
  563. #else
  564. typedef detail::thread_data_ptr data;
  565. #endif
  566. data thread_data;
  567. id(data thread_data_):
  568. thread_data(thread_data_)
  569. {}
  570. friend class thread;
  571. friend id BOOST_THREAD_DECL this_thread::get_id() BOOST_NOEXCEPT;
  572. public:
  573. id() BOOST_NOEXCEPT:
  574. #if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
  575. thread_data(0)
  576. #else
  577. thread_data()
  578. #endif
  579. {}
  580. id(const id& other) BOOST_NOEXCEPT :
  581. thread_data(other.thread_data)
  582. {}
  583. bool operator==(const id& y) const BOOST_NOEXCEPT
  584. {
  585. return thread_data==y.thread_data;
  586. }
  587. bool operator!=(const id& y) const BOOST_NOEXCEPT
  588. {
  589. return thread_data!=y.thread_data;
  590. }
  591. bool operator<(const id& y) const BOOST_NOEXCEPT
  592. {
  593. return thread_data<y.thread_data;
  594. }
  595. bool operator>(const id& y) const BOOST_NOEXCEPT
  596. {
  597. return y.thread_data<thread_data;
  598. }
  599. bool operator<=(const id& y) const BOOST_NOEXCEPT
  600. {
  601. return !(y.thread_data<thread_data);
  602. }
  603. bool operator>=(const id& y) const BOOST_NOEXCEPT
  604. {
  605. return !(thread_data<y.thread_data);
  606. }
  607. #ifndef BOOST_NO_IOSTREAM
  608. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  609. template<class charT, class traits>
  610. friend BOOST_SYMBOL_VISIBLE
  611. std::basic_ostream<charT, traits>&
  612. operator<<(std::basic_ostream<charT, traits>& os, const id& x)
  613. {
  614. if(x.thread_data)
  615. {
  616. io::ios_flags_saver ifs( os );
  617. return os<< std::hex << x.thread_data;
  618. }
  619. else
  620. {
  621. return os<<"{Not-any-thread}";
  622. }
  623. }
  624. #else
  625. template<class charT, class traits>
  626. BOOST_SYMBOL_VISIBLE
  627. std::basic_ostream<charT, traits>&
  628. print(std::basic_ostream<charT, traits>& os) const
  629. {
  630. if(thread_data)
  631. {
  632. io::ios_flags_saver ifs( os );
  633. return os<< std::hex << thread_data;
  634. }
  635. else
  636. {
  637. return os<<"{Not-any-thread}";
  638. }
  639. }
  640. #endif
  641. #endif
  642. };
  643. #ifdef BOOST_THREAD_PLATFORM_PTHREAD
  644. inline thread::id thread::get_id() const BOOST_NOEXCEPT
  645. {
  646. #if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
  647. return const_cast<thread*>(this)->native_handle();
  648. #else
  649. detail::thread_data_ptr const local_thread_info=(get_thread_info)();
  650. return (local_thread_info? id(local_thread_info) : id());
  651. #endif
  652. }
  653. namespace this_thread
  654. {
  655. inline thread::id get_id() BOOST_NOEXCEPT
  656. {
  657. #if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
  658. return pthread_self();
  659. #else
  660. boost::detail::thread_data_base* const thread_info=get_or_make_current_thread_data();
  661. return (thread_info?thread::id(thread_info->shared_from_this()):thread::id());
  662. #endif
  663. }
  664. }
  665. #endif
  666. inline void thread::join() {
  667. if (this_thread::get_id() == get_id())
  668. boost::throw_exception(thread_resource_error(static_cast<int>(system::errc::resource_deadlock_would_occur), "boost thread: trying joining itself"));
  669. BOOST_THREAD_VERIFY_PRECONDITION( join_noexcept(),
  670. thread_resource_error(static_cast<int>(system::errc::invalid_argument), "boost thread: thread not joinable")
  671. );
  672. }
  673. inline bool thread::do_try_join_until(detail::internal_platform_timepoint const &timeout)
  674. {
  675. if (this_thread::get_id() == get_id())
  676. boost::throw_exception(thread_resource_error(static_cast<int>(system::errc::resource_deadlock_would_occur), "boost thread: trying joining itself"));
  677. bool res;
  678. if (do_try_join_until_noexcept(timeout, res))
  679. {
  680. return res;
  681. }
  682. else
  683. {
  684. BOOST_THREAD_THROW_ELSE_RETURN(
  685. (thread_resource_error(static_cast<int>(system::errc::invalid_argument), "boost thread: thread not joinable")),
  686. false
  687. );
  688. }
  689. }
  690. #if !defined(BOOST_NO_IOSTREAM) && defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  691. template<class charT, class traits>
  692. BOOST_SYMBOL_VISIBLE
  693. std::basic_ostream<charT, traits>&
  694. operator<<(std::basic_ostream<charT, traits>& os, const thread::id& x)
  695. {
  696. return x.print(os);
  697. }
  698. #endif
  699. #if defined BOOST_THREAD_PROVIDES_THREAD_EQ
  700. inline bool thread::operator==(const thread& other) const
  701. {
  702. return get_id()==other.get_id();
  703. }
  704. inline bool thread::operator!=(const thread& other) const
  705. {
  706. return get_id()!=other.get_id();
  707. }
  708. #endif
  709. namespace detail
  710. {
  711. struct thread_exit_function_base
  712. {
  713. virtual ~thread_exit_function_base()
  714. {}
  715. virtual void operator()()=0;
  716. };
  717. template<typename F>
  718. struct thread_exit_function:
  719. thread_exit_function_base
  720. {
  721. F f;
  722. thread_exit_function(F f_):
  723. f(f_)
  724. {}
  725. void operator()()
  726. {
  727. f();
  728. }
  729. };
  730. void BOOST_THREAD_DECL add_thread_exit_function(thread_exit_function_base*);
  731. //#ifndef BOOST_NO_EXCEPTIONS
  732. struct shared_state_base;
  733. #if defined(BOOST_THREAD_PLATFORM_WIN32)
  734. inline void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
  735. {
  736. detail::thread_data_base* const current_thread_data(detail::get_current_thread_data());
  737. if(current_thread_data)
  738. {
  739. current_thread_data->make_ready_at_thread_exit(as);
  740. }
  741. }
  742. #else
  743. void BOOST_THREAD_DECL make_ready_at_thread_exit(shared_ptr<shared_state_base> as);
  744. #endif
  745. //#endif
  746. }
  747. namespace this_thread
  748. {
  749. template<typename F>
  750. void at_thread_exit(F f)
  751. {
  752. detail::thread_exit_function_base* const thread_exit_func=detail::heap_new<detail::thread_exit_function<F> >(f);
  753. detail::add_thread_exit_function(thread_exit_func);
  754. }
  755. }
  756. }
  757. #ifdef BOOST_MSVC
  758. #pragma warning(pop)
  759. #endif
  760. #include <boost/config/abi_suffix.hpp>
  761. #endif