thread.hpp 26 KB

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