thread_data.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #ifndef BOOST_THREAD_PTHREAD_THREAD_DATA_HPP
  2. #define BOOST_THREAD_PTHREAD_THREAD_DATA_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 2008 Anthony Williams
  7. // (C) Copyright 2011-2012 Vicente J. Botet Escriba
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/thread/thread_time.hpp>
  10. #include <boost/thread/win32/thread_primitives.hpp>
  11. #include <boost/thread/win32/thread_heap_alloc.hpp>
  12. #include <boost/thread/detail/platform_time.hpp>
  13. #include <boost/predef/platform.h>
  14. #include <boost/intrusive_ptr.hpp>
  15. #ifdef BOOST_THREAD_USES_CHRONO
  16. #include <boost/chrono/system_clocks.hpp>
  17. #endif
  18. #include <map>
  19. #include <vector>
  20. #include <utility>
  21. #include <boost/config/abi_prefix.hpp>
  22. #ifdef BOOST_MSVC
  23. #pragma warning(push)
  24. #pragma warning(disable:4251)
  25. #endif
  26. namespace boost
  27. {
  28. class condition_variable;
  29. class mutex;
  30. class thread_attributes {
  31. public:
  32. thread_attributes() BOOST_NOEXCEPT {
  33. val_.stack_size = 0;
  34. //val_.lpThreadAttributes=0;
  35. }
  36. ~thread_attributes() {
  37. }
  38. // stack size
  39. void set_stack_size(std::size_t size) BOOST_NOEXCEPT {
  40. val_.stack_size = size;
  41. }
  42. std::size_t get_stack_size() const BOOST_NOEXCEPT {
  43. return val_.stack_size;
  44. }
  45. //void set_security(LPSECURITY_ATTRIBUTES lpThreadAttributes)
  46. //{
  47. // val_.lpThreadAttributes=lpThreadAttributes;
  48. //}
  49. //LPSECURITY_ATTRIBUTES get_security()
  50. //{
  51. // return val_.lpThreadAttributes;
  52. //}
  53. struct win_attrs {
  54. std::size_t stack_size;
  55. //LPSECURITY_ATTRIBUTES lpThreadAttributes;
  56. };
  57. typedef win_attrs native_handle_type;
  58. native_handle_type* native_handle() {return &val_;}
  59. const native_handle_type* native_handle() const {return &val_;}
  60. private:
  61. win_attrs val_;
  62. };
  63. namespace detail
  64. {
  65. struct shared_state_base;
  66. struct tss_cleanup_function;
  67. struct thread_exit_callback_node;
  68. struct tss_data_node
  69. {
  70. boost::shared_ptr<boost::detail::tss_cleanup_function> func;
  71. void* value;
  72. tss_data_node(boost::shared_ptr<boost::detail::tss_cleanup_function> func_,
  73. void* value_):
  74. func(func_),value(value_)
  75. {}
  76. };
  77. struct thread_data_base;
  78. void intrusive_ptr_add_ref(thread_data_base * p);
  79. void intrusive_ptr_release(thread_data_base * p);
  80. struct BOOST_THREAD_DECL thread_data_base
  81. {
  82. long count;
  83. // Win32 threading APIs are not available in store apps so
  84. // use abstraction on top of Windows::System::Threading.
  85. #if BOOST_PLAT_WINDOWS_RUNTIME
  86. detail::win32::scoped_winrt_thread thread_handle;
  87. #else
  88. detail::win32::handle_manager thread_handle;
  89. #endif
  90. boost::detail::thread_exit_callback_node* thread_exit_callbacks;
  91. unsigned id;
  92. std::map<void const*,boost::detail::tss_data_node> tss_data;
  93. typedef std::vector<std::pair<condition_variable*, mutex*>
  94. //, hidden_allocator<std::pair<condition_variable*, mutex*> >
  95. > notify_list_t;
  96. notify_list_t notify;
  97. //#ifndef BOOST_NO_EXCEPTIONS
  98. typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
  99. async_states_t async_states_;
  100. //#endif
  101. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  102. // These data must be at the end so that the access to the other fields doesn't change
  103. // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined
  104. // Another option is to have them always
  105. detail::win32::handle_manager interruption_handle;
  106. bool interruption_enabled;
  107. //#endif
  108. thread_data_base():
  109. count(0),
  110. thread_handle(),
  111. thread_exit_callbacks(0),
  112. id(0),
  113. tss_data(),
  114. notify()
  115. //#ifndef BOOST_NO_EXCEPTIONS
  116. , async_states_()
  117. //#endif
  118. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  119. , interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
  120. , interruption_enabled(true)
  121. //#endif
  122. {}
  123. virtual ~thread_data_base();
  124. friend void intrusive_ptr_add_ref(thread_data_base * p)
  125. {
  126. BOOST_INTERLOCKED_INCREMENT(&p->count);
  127. }
  128. friend void intrusive_ptr_release(thread_data_base * p)
  129. {
  130. if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
  131. {
  132. detail::heap_delete(p);
  133. }
  134. }
  135. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  136. void interrupt()
  137. {
  138. BOOST_VERIFY(winapi::SetEvent(interruption_handle)!=0);
  139. }
  140. #endif
  141. typedef detail::win32::handle native_handle_type;
  142. virtual void run()=0;
  143. virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
  144. {
  145. notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
  146. }
  147. //#ifndef BOOST_NO_EXCEPTIONS
  148. void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
  149. {
  150. async_states_.push_back(as);
  151. }
  152. //#endif
  153. };
  154. BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
  155. typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
  156. }
  157. namespace this_thread
  158. {
  159. void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
  160. bool BOOST_THREAD_DECL interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
  161. #if defined BOOST_THREAD_USES_DATETIME
  162. template<typename TimeDuration>
  163. BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
  164. {
  165. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
  166. }
  167. inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
  168. {
  169. const detail::real_platform_timepoint ts(abs_time);
  170. detail::platform_duration d(ts - detail::real_platform_clock::now());
  171. while (d > detail::platform_duration::zero())
  172. {
  173. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  174. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
  175. d = ts - detail::real_platform_clock::now();
  176. }
  177. }
  178. #endif
  179. #ifdef BOOST_THREAD_USES_CHRONO
  180. template <class Rep, class Period>
  181. void sleep_for(const chrono::duration<Rep, Period>& d)
  182. {
  183. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
  184. }
  185. template <class Duration>
  186. void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  187. {
  188. sleep_for(t - chrono::steady_clock::now());
  189. }
  190. template <class Clock, class Duration>
  191. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  192. {
  193. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  194. common_duration d(t - Clock::now());
  195. while (d > common_duration::zero())
  196. {
  197. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  198. sleep_for(d);
  199. d = t - Clock::now();
  200. }
  201. }
  202. #endif
  203. namespace no_interruption_point
  204. {
  205. bool BOOST_THREAD_DECL non_interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
  206. #if defined BOOST_THREAD_USES_DATETIME
  207. template<typename TimeDuration>
  208. BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
  209. {
  210. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
  211. }
  212. inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
  213. {
  214. const detail::real_platform_timepoint ts(abs_time);
  215. detail::platform_duration d(ts - detail::real_platform_clock::now());
  216. while (d > detail::platform_duration::zero())
  217. {
  218. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  219. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
  220. d = ts - detail::real_platform_clock::now();
  221. }
  222. }
  223. #endif
  224. #ifdef BOOST_THREAD_USES_CHRONO
  225. template <class Rep, class Period>
  226. void sleep_for(const chrono::duration<Rep, Period>& d)
  227. {
  228. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
  229. }
  230. template <class Duration>
  231. void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  232. {
  233. sleep_for(t - chrono::steady_clock::now());
  234. }
  235. template <class Clock, class Duration>
  236. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  237. {
  238. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  239. common_duration d(t - Clock::now());
  240. while (d > common_duration::zero())
  241. {
  242. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  243. sleep_for(d);
  244. d = t - Clock::now();
  245. }
  246. }
  247. #endif
  248. }
  249. }
  250. }
  251. #ifdef BOOST_MSVC
  252. #pragma warning(pop)
  253. #endif
  254. #include <boost/config/abi_suffix.hpp>
  255. #endif