thread.hpp 25 KB

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