thread_data.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. typedef void(*cleanup_func_t)(void*);
  71. typedef void(*cleanup_caller_t)(cleanup_func_t, void*);
  72. cleanup_caller_t caller;
  73. cleanup_func_t func;
  74. void* value;
  75. tss_data_node(cleanup_caller_t caller_,cleanup_func_t func_,void* value_):
  76. caller(caller_),func(func_),value(value_)
  77. {}
  78. };
  79. struct thread_data_base;
  80. void intrusive_ptr_add_ref(thread_data_base * p);
  81. void intrusive_ptr_release(thread_data_base * p);
  82. struct BOOST_THREAD_DECL thread_data_base
  83. {
  84. long count;
  85. // Win32 threading APIs are not available in store apps so
  86. // use abstraction on top of Windows::System::Threading.
  87. #if BOOST_PLAT_WINDOWS_RUNTIME
  88. detail::win32::scoped_winrt_thread thread_handle;
  89. #else
  90. detail::win32::handle_manager thread_handle;
  91. #endif
  92. boost::detail::thread_exit_callback_node* thread_exit_callbacks;
  93. unsigned id;
  94. std::map<void const*,boost::detail::tss_data_node> tss_data;
  95. typedef std::vector<std::pair<condition_variable*, mutex*>
  96. //, hidden_allocator<std::pair<condition_variable*, mutex*> >
  97. > notify_list_t;
  98. notify_list_t notify;
  99. //#ifndef BOOST_NO_EXCEPTIONS
  100. typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
  101. async_states_t async_states_;
  102. //#endif
  103. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  104. // These data must be at the end so that the access to the other fields doesn't change
  105. // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined
  106. // Another option is to have them always
  107. detail::win32::handle_manager interruption_handle;
  108. bool interruption_enabled;
  109. //#endif
  110. thread_data_base():
  111. count(0),
  112. thread_handle(),
  113. thread_exit_callbacks(0),
  114. id(0),
  115. tss_data(),
  116. notify()
  117. //#ifndef BOOST_NO_EXCEPTIONS
  118. , async_states_()
  119. //#endif
  120. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  121. , interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
  122. , interruption_enabled(true)
  123. //#endif
  124. {}
  125. virtual ~thread_data_base();
  126. friend void intrusive_ptr_add_ref(thread_data_base * p)
  127. {
  128. BOOST_INTERLOCKED_INCREMENT(&p->count);
  129. }
  130. friend void intrusive_ptr_release(thread_data_base * p)
  131. {
  132. if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
  133. {
  134. detail::heap_delete(p);
  135. }
  136. }
  137. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  138. void interrupt()
  139. {
  140. BOOST_VERIFY(winapi::SetEvent(interruption_handle)!=0);
  141. }
  142. #endif
  143. typedef detail::win32::handle native_handle_type;
  144. virtual void run()=0;
  145. virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
  146. {
  147. notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
  148. }
  149. //#ifndef BOOST_NO_EXCEPTIONS
  150. void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
  151. {
  152. async_states_.push_back(as);
  153. }
  154. //#endif
  155. };
  156. BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
  157. typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
  158. }
  159. namespace this_thread
  160. {
  161. void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
  162. bool BOOST_THREAD_DECL interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
  163. #if defined BOOST_THREAD_USES_DATETIME
  164. template<typename TimeDuration>
  165. BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
  166. {
  167. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
  168. }
  169. inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
  170. {
  171. const detail::real_platform_timepoint ts(abs_time);
  172. detail::platform_duration d(ts - detail::real_platform_clock::now());
  173. while (d > detail::platform_duration::zero())
  174. {
  175. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  176. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
  177. d = ts - detail::real_platform_clock::now();
  178. }
  179. }
  180. #endif
  181. #ifdef BOOST_THREAD_USES_CHRONO
  182. template <class Rep, class Period>
  183. void sleep_for(const chrono::duration<Rep, Period>& d)
  184. {
  185. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
  186. }
  187. template <class Duration>
  188. void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  189. {
  190. sleep_for(t - chrono::steady_clock::now());
  191. }
  192. template <class Clock, class Duration>
  193. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  194. {
  195. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  196. common_duration d(t - Clock::now());
  197. while (d > common_duration::zero())
  198. {
  199. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  200. sleep_for(d);
  201. d = t - Clock::now();
  202. }
  203. }
  204. #endif
  205. namespace no_interruption_point
  206. {
  207. bool BOOST_THREAD_DECL non_interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
  208. #if defined BOOST_THREAD_USES_DATETIME
  209. template<typename TimeDuration>
  210. BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
  211. {
  212. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
  213. }
  214. inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
  215. {
  216. const detail::real_platform_timepoint ts(abs_time);
  217. detail::platform_duration d(ts - detail::real_platform_clock::now());
  218. while (d > detail::platform_duration::zero())
  219. {
  220. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  221. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
  222. d = ts - detail::real_platform_clock::now();
  223. }
  224. }
  225. #endif
  226. #ifdef BOOST_THREAD_USES_CHRONO
  227. template <class Rep, class Period>
  228. void sleep_for(const chrono::duration<Rep, Period>& d)
  229. {
  230. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
  231. }
  232. template <class Duration>
  233. void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  234. {
  235. sleep_for(t - chrono::steady_clock::now());
  236. }
  237. template <class Clock, class Duration>
  238. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  239. {
  240. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  241. common_duration d(t - Clock::now());
  242. while (d > common_duration::zero())
  243. {
  244. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  245. sleep_for(d);
  246. d = t - Clock::now();
  247. }
  248. }
  249. #endif
  250. }
  251. }
  252. }
  253. #ifdef BOOST_MSVC
  254. #pragma warning(pop)
  255. #endif
  256. #include <boost/config/abi_suffix.hpp>
  257. #endif