thread 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // <thread> -*- C++ -*-
  2. // Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/thread
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_THREAD
  24. #define _GLIBCXX_THREAD 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <chrono>
  30. #include <functional>
  31. #include <memory>
  32. #include <bits/functexcept.h>
  33. #include <bits/functional_hash.h>
  34. #include <bits/gthr.h>
  35. #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
  36. namespace std _GLIBCXX_VISIBILITY(default)
  37. {
  38. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  39. /**
  40. * @defgroup threads Threads
  41. * @ingroup concurrency
  42. *
  43. * Classes for thread support.
  44. * @{
  45. */
  46. /// thread
  47. class thread
  48. {
  49. public:
  50. typedef __gthread_t native_handle_type;
  51. struct _Impl_base;
  52. typedef shared_ptr<_Impl_base> __shared_base_type;
  53. /// thread::id
  54. class id
  55. {
  56. native_handle_type _M_thread;
  57. public:
  58. id() noexcept : _M_thread() { }
  59. explicit
  60. id(native_handle_type __id) : _M_thread(__id) { }
  61. private:
  62. friend class thread;
  63. friend class hash<thread::id>;
  64. friend bool
  65. operator==(thread::id __x, thread::id __y) noexcept
  66. { return __gthread_equal(__x._M_thread, __y._M_thread); }
  67. friend bool
  68. operator<(thread::id __x, thread::id __y) noexcept
  69. { return __x._M_thread < __y._M_thread; }
  70. template<class _CharT, class _Traits>
  71. friend basic_ostream<_CharT, _Traits>&
  72. operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
  73. };
  74. // Simple base type that the templatized, derived class containing
  75. // an arbitrary functor can be converted to and called.
  76. struct _Impl_base
  77. {
  78. __shared_base_type _M_this_ptr;
  79. inline virtual ~_Impl_base();
  80. virtual void _M_run() = 0;
  81. };
  82. template<typename _Callable>
  83. struct _Impl : public _Impl_base
  84. {
  85. _Callable _M_func;
  86. _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
  87. { }
  88. void
  89. _M_run() { _M_func(); }
  90. };
  91. private:
  92. id _M_id;
  93. public:
  94. thread() noexcept = default;
  95. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  96. // 2097. packaged_task constructors should be constrained
  97. thread(thread&) = delete;
  98. thread(const thread&) = delete;
  99. thread(thread&& __t) noexcept
  100. { swap(__t); }
  101. template<typename _Callable, typename... _Args>
  102. explicit
  103. thread(_Callable&& __f, _Args&&... __args)
  104. {
  105. #ifdef GTHR_ACTIVE_PROXY
  106. // Create a reference to pthread_create, not just the gthr weak symbol
  107. _M_start_thread(_M_make_routine(std::__bind_simple(
  108. std::forward<_Callable>(__f),
  109. std::forward<_Args>(__args)...)),
  110. reinterpret_cast<void(*)()>(&pthread_create));
  111. #else
  112. _M_start_thread(_M_make_routine(std::__bind_simple(
  113. std::forward<_Callable>(__f),
  114. std::forward<_Args>(__args)...)));
  115. #endif
  116. }
  117. ~thread()
  118. {
  119. if (joinable())
  120. std::terminate();
  121. }
  122. thread& operator=(const thread&) = delete;
  123. thread& operator=(thread&& __t) noexcept
  124. {
  125. if (joinable())
  126. std::terminate();
  127. swap(__t);
  128. return *this;
  129. }
  130. void
  131. swap(thread& __t) noexcept
  132. { std::swap(_M_id, __t._M_id); }
  133. bool
  134. joinable() const noexcept
  135. { return !(_M_id == id()); }
  136. void
  137. join();
  138. void
  139. detach();
  140. thread::id
  141. get_id() const noexcept
  142. { return _M_id; }
  143. /** @pre thread is joinable
  144. */
  145. native_handle_type
  146. native_handle()
  147. { return _M_id._M_thread; }
  148. // Returns a value that hints at the number of hardware thread contexts.
  149. static unsigned int
  150. hardware_concurrency() noexcept;
  151. private:
  152. void
  153. _M_start_thread(__shared_base_type, void (*)());
  154. void
  155. _M_start_thread(__shared_base_type);
  156. template<typename _Callable>
  157. shared_ptr<_Impl<_Callable>>
  158. _M_make_routine(_Callable&& __f)
  159. {
  160. // Create and allocate full data structure, not base.
  161. return std::make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
  162. }
  163. };
  164. inline thread::_Impl_base::~_Impl_base() = default;
  165. inline void
  166. swap(thread& __x, thread& __y) noexcept
  167. { __x.swap(__y); }
  168. inline bool
  169. operator!=(thread::id __x, thread::id __y) noexcept
  170. { return !(__x == __y); }
  171. inline bool
  172. operator<=(thread::id __x, thread::id __y) noexcept
  173. { return !(__y < __x); }
  174. inline bool
  175. operator>(thread::id __x, thread::id __y) noexcept
  176. { return __y < __x; }
  177. inline bool
  178. operator>=(thread::id __x, thread::id __y) noexcept
  179. { return !(__x < __y); }
  180. // DR 889.
  181. /// std::hash specialization for thread::id.
  182. template<>
  183. struct hash<thread::id>
  184. : public __hash_base<size_t, thread::id>
  185. {
  186. size_t
  187. operator()(const thread::id& __id) const noexcept
  188. { return std::_Hash_impl::hash(__id._M_thread); }
  189. };
  190. template<class _CharT, class _Traits>
  191. inline basic_ostream<_CharT, _Traits>&
  192. operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
  193. {
  194. if (__id == thread::id())
  195. return __out << "thread::id of a non-executing thread";
  196. else
  197. return __out << __id._M_thread;
  198. }
  199. _GLIBCXX_END_NAMESPACE_VERSION
  200. /** @namespace std::this_thread
  201. * @brief ISO C++ 2011 entities sub-namespace for thread.
  202. * 30.3.2 Namespace this_thread.
  203. */
  204. namespace this_thread
  205. {
  206. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  207. /// get_id
  208. inline thread::id
  209. get_id() noexcept { return thread::id(__gthread_self()); }
  210. /// yield
  211. inline void
  212. yield() noexcept
  213. {
  214. #ifdef _GLIBCXX_USE_SCHED_YIELD
  215. __gthread_yield();
  216. #endif
  217. }
  218. void
  219. __sleep_for(chrono::seconds, chrono::nanoseconds);
  220. /// sleep_for
  221. template<typename _Rep, typename _Period>
  222. inline void
  223. sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
  224. {
  225. if (__rtime <= __rtime.zero())
  226. return;
  227. auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
  228. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
  229. #ifdef _GLIBCXX_USE_NANOSLEEP
  230. __gthread_time_t __ts =
  231. {
  232. static_cast<std::time_t>(__s.count()),
  233. static_cast<long>(__ns.count())
  234. };
  235. ::nanosleep(&__ts, 0);
  236. #else
  237. __sleep_for(__s, __ns);
  238. #endif
  239. }
  240. /// sleep_until
  241. template<typename _Clock, typename _Duration>
  242. inline void
  243. sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
  244. {
  245. auto __now = _Clock::now();
  246. if (__now < __atime)
  247. sleep_for(__atime - __now);
  248. }
  249. _GLIBCXX_END_NAMESPACE_VERSION
  250. }
  251. // @} group threads
  252. } // namespace
  253. #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
  254. #endif // C++11
  255. #endif // _GLIBCXX_THREAD