thread_data.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/thread/pthread/pthread_helpers.hpp>
  15. #include <boost/shared_ptr.hpp>
  16. #include <boost/enable_shared_from_this.hpp>
  17. #include <boost/assert.hpp>
  18. #include <boost/thread/detail/platform_time.hpp>
  19. #ifdef BOOST_THREAD_USES_CHRONO
  20. #include <boost/chrono/system_clocks.hpp>
  21. #endif
  22. #include <map>
  23. #include <vector>
  24. #include <utility>
  25. #if defined(__ANDROID__)
  26. # ifndef PAGE_SIZE
  27. # define PAGE_SIZE 4096
  28. # endif
  29. #endif
  30. #include <pthread.h>
  31. #include <unistd.h>
  32. #include <boost/config/abi_prefix.hpp>
  33. namespace boost
  34. {
  35. class thread_attributes {
  36. public:
  37. thread_attributes() BOOST_NOEXCEPT {
  38. int res = pthread_attr_init(&val_);
  39. BOOST_VERIFY(!res && "pthread_attr_init failed");
  40. }
  41. ~thread_attributes() {
  42. int res = pthread_attr_destroy(&val_);
  43. BOOST_VERIFY(!res && "pthread_attr_destroy failed");
  44. }
  45. // stack
  46. void set_stack_size(std::size_t size) BOOST_NOEXCEPT {
  47. if (size==0) return;
  48. #ifdef BOOST_THREAD_USES_GETPAGESIZE
  49. std::size_t page_size = getpagesize();
  50. #else
  51. std::size_t page_size = ::sysconf( _SC_PAGESIZE);
  52. #endif
  53. #if PTHREAD_STACK_MIN > 0
  54. if (size<PTHREAD_STACK_MIN) size=PTHREAD_STACK_MIN;
  55. #endif
  56. size = ((size+page_size-1)/page_size)*page_size;
  57. int res = pthread_attr_setstacksize(&val_, size);
  58. BOOST_VERIFY(!res && "pthread_attr_setstacksize failed");
  59. }
  60. std::size_t get_stack_size() const BOOST_NOEXCEPT {
  61. std::size_t size;
  62. int res = pthread_attr_getstacksize(&val_, &size);
  63. BOOST_VERIFY(!res && "pthread_attr_getstacksize failed");
  64. return size;
  65. }
  66. #define BOOST_THREAD_DEFINES_THREAD_ATTRIBUTES_NATIVE_HANDLE
  67. typedef pthread_attr_t native_handle_type;
  68. native_handle_type* native_handle() BOOST_NOEXCEPT {
  69. return &val_;
  70. }
  71. const native_handle_type* native_handle() const BOOST_NOEXCEPT {
  72. return &val_;
  73. }
  74. private:
  75. pthread_attr_t val_;
  76. };
  77. class thread;
  78. namespace detail
  79. {
  80. struct shared_state_base;
  81. struct tss_cleanup_function;
  82. struct thread_exit_callback_node;
  83. struct tss_data_node
  84. {
  85. typedef void(*cleanup_func_t)(void*);
  86. typedef void(*cleanup_caller_t)(cleanup_func_t, void*);
  87. cleanup_caller_t caller;
  88. cleanup_func_t func;
  89. void* value;
  90. tss_data_node(cleanup_caller_t caller_,cleanup_func_t func_,void* value_):
  91. caller(caller_),func(func_),value(value_)
  92. {}
  93. };
  94. struct thread_data_base;
  95. typedef boost::shared_ptr<thread_data_base> thread_data_ptr;
  96. struct BOOST_THREAD_DECL thread_data_base:
  97. enable_shared_from_this<thread_data_base>
  98. {
  99. thread_data_ptr self;
  100. pthread_t thread_handle;
  101. boost::mutex data_mutex;
  102. boost::condition_variable done_condition;
  103. bool done;
  104. bool join_started;
  105. bool joined;
  106. boost::detail::thread_exit_callback_node* thread_exit_callbacks;
  107. std::map<void const*,boost::detail::tss_data_node> tss_data;
  108. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  109. // These data must be at the end so that the access to the other fields doesn't change
  110. // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined.
  111. // Another option is to have them always
  112. pthread_mutex_t* cond_mutex;
  113. pthread_cond_t* current_cond;
  114. //#endif
  115. typedef std::vector<std::pair<condition_variable*, mutex*>
  116. //, hidden_allocator<std::pair<condition_variable*, mutex*> >
  117. > notify_list_t;
  118. notify_list_t notify;
  119. //#ifndef BOOST_NO_EXCEPTIONS
  120. typedef std::vector<shared_ptr<shared_state_base> > async_states_t;
  121. async_states_t async_states_;
  122. //#endif
  123. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  124. // These data must be at the end so that the access to the other fields doesn't change
  125. // when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined.
  126. // Another option is to have them always
  127. bool interrupt_enabled;
  128. bool interrupt_requested;
  129. //#endif
  130. thread_data_base():
  131. thread_handle(0),
  132. done(false),join_started(false),joined(false),
  133. thread_exit_callbacks(0),
  134. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  135. cond_mutex(0),
  136. current_cond(0),
  137. //#endif
  138. notify()
  139. //#ifndef BOOST_NO_EXCEPTIONS
  140. , async_states_()
  141. //#endif
  142. //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  143. , interrupt_enabled(true)
  144. , interrupt_requested(false)
  145. //#endif
  146. {}
  147. virtual ~thread_data_base();
  148. typedef pthread_t 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. BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
  162. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  163. class interruption_checker
  164. {
  165. thread_data_base* const thread_info;
  166. pthread_mutex_t* m;
  167. bool set;
  168. bool done;
  169. void check_for_interruption()
  170. {
  171. #ifndef BOOST_NO_EXCEPTIONS
  172. if(thread_info->interrupt_requested)
  173. {
  174. thread_info->interrupt_requested=false;
  175. throw thread_interrupted(); // BOOST_NO_EXCEPTIONS protected
  176. }
  177. #endif
  178. }
  179. void operator=(interruption_checker&);
  180. public:
  181. explicit interruption_checker(pthread_mutex_t* cond_mutex,pthread_cond_t* cond):
  182. thread_info(detail::get_current_thread_data()),m(cond_mutex),
  183. set(thread_info && thread_info->interrupt_enabled), done(false)
  184. {
  185. if(set)
  186. {
  187. lock_guard<mutex> guard(thread_info->data_mutex);
  188. check_for_interruption();
  189. thread_info->cond_mutex=cond_mutex;
  190. thread_info->current_cond=cond;
  191. BOOST_VERIFY(!posix::pthread_mutex_lock(m));
  192. }
  193. else
  194. {
  195. BOOST_VERIFY(!posix::pthread_mutex_lock(m));
  196. }
  197. }
  198. void unlock_if_locked()
  199. {
  200. if ( ! done) {
  201. if (set)
  202. {
  203. BOOST_VERIFY(!posix::pthread_mutex_unlock(m));
  204. lock_guard<mutex> guard(thread_info->data_mutex);
  205. thread_info->cond_mutex=NULL;
  206. thread_info->current_cond=NULL;
  207. }
  208. else
  209. {
  210. BOOST_VERIFY(!posix::pthread_mutex_unlock(m));
  211. }
  212. done = true;
  213. }
  214. }
  215. ~interruption_checker() BOOST_NOEXCEPT_IF(false)
  216. {
  217. unlock_if_locked();
  218. }
  219. };
  220. #endif
  221. }
  222. namespace this_thread
  223. {
  224. void BOOST_THREAD_DECL yield() BOOST_NOEXCEPT;
  225. namespace hidden
  226. {
  227. inline bool always_false()
  228. {
  229. return false;
  230. }
  231. }
  232. #if defined BOOST_THREAD_USES_DATETIME
  233. #ifdef __DECXXX
  234. /// Workaround of DECCXX issue of incorrect template substitution
  235. template<>
  236. #endif
  237. inline void sleep(system_time const& abs_time)
  238. {
  239. mutex mx;
  240. unique_lock<mutex> lock(mx);
  241. condition_variable cond;
  242. cond.timed_wait(lock, abs_time, hidden::always_false);
  243. }
  244. template<typename TimeDuration>
  245. void sleep(TimeDuration const& rel_time)
  246. {
  247. mutex mx;
  248. unique_lock<mutex> lock(mx);
  249. condition_variable cond;
  250. cond.timed_wait(lock, rel_time, hidden::always_false);
  251. }
  252. #endif
  253. #ifdef BOOST_THREAD_USES_CHRONO
  254. template <class Clock, class Duration>
  255. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  256. {
  257. mutex mut;
  258. unique_lock<mutex> lk(mut);
  259. condition_variable cv;
  260. cv.wait_until(lk, t, hidden::always_false);
  261. }
  262. template <class Rep, class Period>
  263. void sleep_for(const chrono::duration<Rep, Period>& d)
  264. {
  265. mutex mut;
  266. unique_lock<mutex> lk(mut);
  267. condition_variable cv;
  268. cv.wait_for(lk, d, hidden::always_false);
  269. }
  270. #endif
  271. namespace no_interruption_point
  272. {
  273. #if defined BOOST_THREAD_SLEEP_FOR_IS_STEADY
  274. // Use pthread_delay_np or nanosleep when available
  275. // because they do not provide an interruption point.
  276. namespace hidden
  277. {
  278. void BOOST_THREAD_DECL sleep_for_internal(const detail::platform_duration& ts);
  279. }
  280. #if defined BOOST_THREAD_USES_DATETIME
  281. #ifdef __DECXXX
  282. /// Workaround of DECCXX issue of incorrect template substitution
  283. template<>
  284. #endif
  285. inline void sleep(system_time const& abs_time)
  286. {
  287. const detail::real_platform_timepoint ts(abs_time);
  288. detail::platform_duration d(ts - detail::real_platform_clock::now());
  289. while (d > detail::platform_duration::zero())
  290. {
  291. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  292. hidden::sleep_for_internal(d);
  293. d = ts - detail::real_platform_clock::now();
  294. }
  295. }
  296. template<typename TimeDuration>
  297. void sleep(TimeDuration const& rel_time)
  298. {
  299. hidden::sleep_for_internal(detail::platform_duration(rel_time));
  300. }
  301. #endif
  302. #ifdef BOOST_THREAD_USES_CHRONO
  303. template <class Rep, class Period>
  304. void sleep_for(const chrono::duration<Rep, Period>& d)
  305. {
  306. hidden::sleep_for_internal(detail::platform_duration(d));
  307. }
  308. template <class Duration>
  309. void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  310. {
  311. sleep_for(t - chrono::steady_clock::now());
  312. }
  313. template <class Clock, class Duration>
  314. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  315. {
  316. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  317. common_duration d(t - Clock::now());
  318. while (d > common_duration::zero())
  319. {
  320. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  321. hidden::sleep_for_internal(detail::platform_duration(d));
  322. d = t - Clock::now();
  323. }
  324. }
  325. #endif
  326. #else // BOOST_THREAD_SLEEP_FOR_IS_STEADY
  327. // When pthread_delay_np and nanosleep are not available,
  328. // fall back to using the interruptible sleep functions.
  329. #if defined BOOST_THREAD_USES_DATETIME
  330. #ifdef __DECXXX
  331. /// Workaround of DECCXX issue of incorrect template substitution
  332. template<>
  333. #endif
  334. inline void sleep(system_time const& abs_time)
  335. {
  336. this_thread::sleep(abs_time);
  337. }
  338. template<typename TimeDuration>
  339. void sleep(TimeDuration const& rel_time)
  340. {
  341. this_thread::sleep(rel_time);
  342. }
  343. #endif
  344. #ifdef BOOST_THREAD_USES_CHRONO
  345. template <class Clock, class Duration>
  346. void sleep_until(const chrono::time_point<Clock, Duration>& t)
  347. {
  348. this_thread::sleep_until(t);
  349. }
  350. template <class Rep, class Period>
  351. void sleep_for(const chrono::duration<Rep, Period>& d)
  352. {
  353. this_thread::sleep_for(d);
  354. }
  355. #endif
  356. #endif // BOOST_THREAD_SLEEP_FOR_IS_STEADY
  357. } // no_interruption_point
  358. } // this_thread
  359. }
  360. #include <boost/config/abi_suffix.hpp>
  361. #endif