thread_data.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 2007 Anthony Williams
  7. // (C) Copyright 2011-2012 Vicente J. Botet Escriba
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/thread/exceptions.hpp>
  10. #include <boost/thread/lock_guard.hpp>
  11. #include <boost/thread/lock_types.hpp>
  12. #include <boost/thread/mutex.hpp>
  13. #include <boost/thread/pthread/condition_variable_fwd.hpp>
  14. #include <boost/shared_ptr.hpp>
  15. #include <boost/enable_shared_from_this.hpp>
  16. #include <boost/optional.hpp>
  17. #include <boost/assert.hpp>
  18. #ifdef BOOST_THREAD_USES_CHRONO
  19. #include <boost/chrono/system_clocks.hpp>
  20. #endif
  21. #include <map>
  22. #include <vector>
  23. #include <utility>
  24. #if defined(__ANDROID__)
  25. #include <asm/page.h> // http://code.google.com/p/android/issues/detail?id=39983
  26. #endif
  27. #include <pthread.h>
  28. #include <unistd.h>
  29. #include <boost/config/abi_prefix.hpp>
  30. namespace boost
  31. {
  32. class thread_attributes {
  33. public:
  34. thread_attributes() BOOST_NOEXCEPT {
  35. int res = pthread_attr_init(&val_);
  36. BOOST_VERIFY(!res && "pthread_attr_init failed");
  37. }
  38. ~thread_attributes() {
  39. int res = pthread_attr_destroy(&val_);
  40. BOOST_VERIFY(!res && "pthread_attr_destroy failed");
  41. }
  42. // stack
  43. void set_stack_size(std::size_t size) BOOST_NOEXCEPT {
  44. if (size==0) return;
  45. std::size_t page_size = getpagesize();
  46. #ifdef PTHREAD_STACK_MIN
  47. if (size<PTHREAD_STACK_MIN) size=PTHREAD_STACK_MIN;
  48. #endif
  49. size = ((size+page_size-1)/page_size)*page_size;
  50. int res = pthread_attr_setstacksize(&val_, size);
  51. BOOST_VERIFY(!res && "pthread_attr_setstacksize failed");
  52. }
  53. std::size_t get_stack_size() const BOOST_NOEXCEPT {
  54. std::size_t size;
  55. int res = pthread_attr_getstacksize(&val_, &size);
  56. BOOST_VERIFY(!res && "pthread_attr_getstacksize failed");
  57. return size;
  58. }
  59. #define BOOST_THREAD_DEFINES_THREAD_ATTRIBUTES_NATIVE_HANDLE
  60. typedef pthread_attr_t native_handle_type;
  61. native_handle_type* native_handle() BOOST_NOEXCEPT {
  62. return &val_;
  63. }
  64. const native_handle_type* native_handle() const BOOST_NOEXCEPT {
  65. return &val_;
  66. }
  67. private:
  68. pthread_attr_t val_;
  69. };
  70. class thread;
  71. namespace detail
  72. {
  73. struct shared_state_base;
  74. struct tss_cleanup_function;
  75. struct thread_exit_callback_node;
  76. struct tss_data_node
  77. {
  78. boost::shared_ptr<boost::detail::tss_cleanup_function> func;
  79. void* value;
  80. tss_data_node(boost::shared_ptr<boost::detail::tss_cleanup_function> func_,
  81. void* value_):
  82. func(func_),value(value_)
  83. {}
  84. };
  85. struct thread_data_base;
  86. typedef boost::shared_ptr<thread_data_base> thread_data_ptr;
  87. struct BOOST_THREAD_DECL thread_data_base:
  88. enable_shared_from_this<thread_data_base>
  89. {
  90. thread_data_ptr self;
  91. pthread_t thread_handle;
  92. boost::mutex data_mutex;
  93. boost::condition_variable done_condition;
  94. boost::mutex sleep_mutex;
  95. boost::condition_variable sleep_condition;
  96. bool done;
  97. bool join_started;
  98. bool joined;
  99. boost::detail::thread_exit_callback_node* thread_exit_callbacks;
  100. std::map<void const*,boost::detail::tss_data_node> tss_data;
  101. pthread_mutex_t* cond_mutex;
  102. pthread_cond_t* current_cond;
  103. typedef std::vector<std::pair<condition_variable*, mutex*>
  104. //, hidden_allocator<std::pair<condition_variable*, mutex*> >
  105. > notify_list_t;
  106. notify_list_t notify;
  107. typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
  108. async_states_t async_states_;
  109. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  110. // These data must be at the end so that the access to the other fields doesn't change
  111. // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined.
  112. // Another option is to have them always
  113. bool interrupt_enabled;
  114. bool interrupt_requested;
  115. //#endif
  116. thread_data_base():
  117. thread_handle(0),
  118. done(false),join_started(false),joined(false),
  119. thread_exit_callbacks(0),
  120. cond_mutex(0),
  121. current_cond(0),
  122. notify(),
  123. async_states_()
  124. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  125. , interrupt_enabled(true)
  126. , interrupt_requested(false)
  127. //#endif
  128. {}
  129. virtual ~thread_data_base();
  130. typedef pthread_t native_handle_type;
  131. virtual void run()=0;
  132. virtual void notify_all_at_thread_exit(condition_variable* cv, mutex* m)
  133. {
  134. notify.push_back(std::pair<condition_variable*, mutex*>(cv, m));
  135. }
  136. void make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
  137. {
  138. async_states_.push_back(as);
  139. }
  140. };
  141. BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
  142. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  143. class interruption_checker
  144. {
  145. thread_data_base* const thread_info;
  146. pthread_mutex_t* m;
  147. bool set;
  148. void check_for_interruption()
  149. {
  150. #ifndef BOOST_NO_EXCEPTIONS
  151. if(thread_info->interrupt_requested)
  152. {
  153. thread_info->interrupt_requested=false;
  154. throw thread_interrupted(); // BOOST_NO_EXCEPTIONS protected
  155. }
  156. #endif
  157. }
  158. void operator=(interruption_checker&);
  159. public:
  160. explicit interruption_checker(pthread_mutex_t* cond_mutex,pthread_cond_t* cond):
  161. thread_info(detail::get_current_thread_data()),m(cond_mutex),
  162. set(thread_info && thread_info->interrupt_enabled)
  163. {
  164. if(set)
  165. {
  166. lock_guard<mutex> guard(thread_info->data_mutex);
  167. check_for_interruption();
  168. thread_info->cond_mutex=cond_mutex;
  169. thread_info->current_cond=cond;
  170. BOOST_VERIFY(!pthread_mutex_lock(m));
  171. }
  172. else
  173. {
  174. BOOST_VERIFY(!pthread_mutex_lock(m));
  175. }
  176. }
  177. ~interruption_checker()
  178. {
  179. if(set)
  180. {
  181. BOOST_VERIFY(!pthread_mutex_unlock(m));
  182. lock_guard<mutex> guard(thread_info->data_mutex);
  183. thread_info->cond_mutex=NULL;
  184. thread_info->current_cond=NULL;
  185. }
  186. else
  187. {
  188. BOOST_VERIFY(!pthread_mutex_unlock(m));
  189. }
  190. }
  191. };
  192. #endif
  193. }
  194. namespace this_thread
  195. {
  196. namespace hiden
  197. {
  198. void BOOST_THREAD_DECL sleep_for(const timespec& ts);
  199. void BOOST_THREAD_DECL sleep_until(const timespec& ts);
  200. }
  201. #ifdef BOOST_THREAD_USES_CHRONO
  202. #ifdef BOOST_THREAD_SLEEP_FOR_IS_STEADY
  203. inline
  204. void BOOST_SYMBOL_VISIBLE sleep_for(const chrono::nanoseconds& ns)
  205. {
  206. return boost::this_thread::hiden::sleep_for(boost::detail::to_timespec(ns));
  207. }
  208. #endif
  209. #endif // BOOST_THREAD_USES_CHRONO
  210. void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
  211. #if defined BOOST_THREAD_USES_DATETIME
  212. #ifdef __DECXXX
  213. /// Workaround of DECCXX issue of incorrect template substitution
  214. template<>
  215. #endif
  216. inline void sleep(system_time const& abs_time)
  217. {
  218. return boost::this_thread::hiden::sleep_until(boost::detail::to_timespec(abs_time));
  219. }
  220. template<typename TimeDuration>
  221. inline BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
  222. {
  223. this_thread::sleep(get_system_time()+rel_time);
  224. }
  225. #endif // BOOST_THREAD_USES_DATETIME
  226. } // this_thread
  227. }
  228. #include <boost/config/abi_suffix.hpp>
  229. #endif