thread_data.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #if !defined(BOOST_EMBTC)
  127. friend void intrusive_ptr_add_ref(thread_data_base * p)
  128. {
  129. BOOST_INTERLOCKED_INCREMENT(&p->count);
  130. }
  131. friend void intrusive_ptr_release(thread_data_base * p)
  132. {
  133. if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
  134. {
  135. detail::heap_delete(p);
  136. }
  137. }
  138. #else
  139. friend void intrusive_ptr_add_ref(thread_data_base * p);
  140. friend void intrusive_ptr_release(thread_data_base * p);
  141. #endif
  142. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  143. void interrupt()
  144. {
  145. BOOST_VERIFY(winapi::SetEvent(interruption_handle)!=0);
  146. }
  147. #endif
  148. typedef detail::win32::handle native_handle_type;
  149. virtual void run()=0;
  150. virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
  151. {
  152. notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
  153. }
  154. //#ifndef BOOST_NO_EXCEPTIONS
  155. void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
  156. {
  157. async_states_.push_back(as);
  158. }
  159. //#endif
  160. };
  161. #if defined(BOOST_EMBTC)
  162. inline void intrusive_ptr_add_ref(thread_data_base * p)
  163. {
  164. BOOST_INTERLOCKED_INCREMENT(&p->count);
  165. }
  166. inline void intrusive_ptr_release(thread_data_base * p)
  167. {
  168. if(!BOOST_INTERLOCKED_DECREMENT(&p->count))
  169. {
  170. detail::heap_delete(p);
  171. }
  172. }
  173. #endif
  174. BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
  175. typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
  176. }
  177. namespace this_thread
  178. {
  179. void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
  180. bool BOOST_THREAD_DECL interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
  181. #if defined BOOST_THREAD_USES_DATETIME
  182. template<typename TimeDuration>
  183. BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
  184. {
  185. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
  186. }
  187. inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
  188. {
  189. const detail::real_platform_timepoint ts(abs_time);
  190. detail::platform_duration d(ts - detail::real_platform_clock::now());
  191. while (d > detail::platform_duration::zero())
  192. {
  193. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  194. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
  195. d = ts - detail::real_platform_clock::now();
  196. }
  197. }
  198. #endif
  199. #ifdef BOOST_THREAD_USES_CHRONO
  200. template <class Rep, class Period>
  201. void sleep_for(const chrono::duration<Rep, Period>& d)
  202. {
  203. interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
  204. }
  205. template <class Duration>
  206. void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  207. {
  208. sleep_for(t - chrono::steady_clock::now());
  209. }
  210. template <class Clock, class Duration>
  211. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  212. {
  213. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  214. common_duration d(t - Clock::now());
  215. while (d > common_duration::zero())
  216. {
  217. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  218. sleep_for(d);
  219. d = t - Clock::now();
  220. }
  221. }
  222. #endif
  223. namespace no_interruption_point
  224. {
  225. bool BOOST_THREAD_DECL non_interruptible_wait(detail::win32::handle handle_to_wait_for, detail::internal_platform_timepoint const &timeout);
  226. #if defined BOOST_THREAD_USES_DATETIME
  227. template<typename TimeDuration>
  228. BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
  229. {
  230. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(rel_time));
  231. }
  232. inline BOOST_SYMBOL_VISIBLE void sleep(system_time const& abs_time)
  233. {
  234. const detail::real_platform_timepoint ts(abs_time);
  235. detail::platform_duration d(ts - detail::real_platform_clock::now());
  236. while (d > detail::platform_duration::zero())
  237. {
  238. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  239. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + d);
  240. d = ts - detail::real_platform_clock::now();
  241. }
  242. }
  243. #endif
  244. #ifdef BOOST_THREAD_USES_CHRONO
  245. template <class Rep, class Period>
  246. void sleep_for(const chrono::duration<Rep, Period>& d)
  247. {
  248. non_interruptible_wait(detail::win32::invalid_handle_value, detail::internal_platform_clock::now() + detail::platform_duration(d));
  249. }
  250. template <class Duration>
  251. void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  252. {
  253. sleep_for(t - chrono::steady_clock::now());
  254. }
  255. template <class Clock, class Duration>
  256. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  257. {
  258. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  259. common_duration d(t - Clock::now());
  260. while (d > common_duration::zero())
  261. {
  262. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  263. sleep_for(d);
  264. d = t - Clock::now();
  265. }
  266. }
  267. #endif
  268. }
  269. }
  270. }
  271. #ifdef BOOST_MSVC
  272. #pragma warning(pop)
  273. #endif
  274. #include <boost/config/abi_suffix.hpp>
  275. #endif