thread.hpp 26 KB

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