thread 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // <thread> -*- C++ -*-
  2. // Copyright (C) 2008, 2009 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 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. #ifndef __GXX_EXPERIMENTAL_CXX0X__
  27. # include <c++0x_warning.h>
  28. #else
  29. #include <chrono>
  30. #include <functional>
  31. #include <memory>
  32. #include <mutex>
  33. #include <condition_variable>
  34. #include <cstddef>
  35. #include <bits/functexcept.h>
  36. #include <bits/gthr.h>
  37. #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
  38. namespace std
  39. {
  40. /**
  41. * @defgroup threads Threads
  42. * @ingroup concurrency
  43. *
  44. * Classes for thread support.
  45. * @{
  46. */
  47. /// thread
  48. class thread
  49. {
  50. public:
  51. typedef __gthread_t native_handle_type;
  52. struct _Impl_base;
  53. typedef shared_ptr<_Impl_base> __shared_base_type;
  54. /// thread::id
  55. class id
  56. {
  57. native_handle_type _M_thread;
  58. public:
  59. id() : _M_thread() { }
  60. explicit
  61. id(native_handle_type __id) : _M_thread(__id) { }
  62. private:
  63. friend class thread;
  64. friend bool
  65. operator==(thread::id __x, thread::id __y)
  66. { return __gthread_equal(__x._M_thread, __y._M_thread); }
  67. friend bool
  68. operator<(thread::id __x, thread::id __y)
  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. virtual ~_Impl_base() = default;
  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() = default;
  95. thread(const thread&) = delete;
  96. thread(thread&& __t)
  97. { swap(__t); }
  98. template<typename _Callable>
  99. explicit thread(_Callable __f)
  100. { _M_start_thread(_M_make_routine<_Callable>(__f)); }
  101. template<typename _Callable, typename... _Args>
  102. thread(_Callable&& __f, _Args&&... __args)
  103. { _M_start_thread(_M_make_routine(std::bind(__f, __args...))); }
  104. ~thread()
  105. {
  106. if (joinable())
  107. std::terminate();
  108. }
  109. thread& operator=(const thread&) = delete;
  110. thread& operator=(thread&& __t)
  111. {
  112. if (joinable())
  113. std::terminate();
  114. swap(__t);
  115. return *this;
  116. }
  117. void
  118. swap(thread&& __t)
  119. { std::swap(_M_id, __t._M_id); }
  120. bool
  121. joinable() const
  122. { return !(_M_id == id()); }
  123. void
  124. join();
  125. void
  126. detach();
  127. thread::id
  128. get_id() const
  129. { return _M_id; }
  130. /** @pre thread is joinable
  131. */
  132. native_handle_type
  133. native_handle()
  134. { return _M_id._M_thread; }
  135. // Returns a value that hints at the number of hardware thread contexts.
  136. static unsigned int
  137. hardware_concurrency()
  138. { return 0; }
  139. private:
  140. void
  141. _M_start_thread(__shared_base_type);
  142. template<typename _Callable>
  143. shared_ptr<_Impl<_Callable>>
  144. _M_make_routine(_Callable&& __f)
  145. {
  146. // Create and allocate full data structure, not base.
  147. return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
  148. }
  149. };
  150. inline void
  151. swap(thread& __x, thread& __y)
  152. { __x.swap(__y); }
  153. inline void
  154. swap(thread&& __x, thread& __y)
  155. { __x.swap(__y); }
  156. inline void
  157. swap(thread& __x, thread&& __y)
  158. { __x.swap(__y); }
  159. inline bool
  160. operator!=(thread::id __x, thread::id __y)
  161. { return !(__x == __y); }
  162. inline bool
  163. operator<=(thread::id __x, thread::id __y)
  164. { return !(__y < __x); }
  165. inline bool
  166. operator>(thread::id __x, thread::id __y)
  167. { return __y < __x; }
  168. inline bool
  169. operator>=(thread::id __x, thread::id __y)
  170. { return !(__x < __y); }
  171. template<class _CharT, class _Traits>
  172. inline basic_ostream<_CharT, _Traits>&
  173. operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
  174. {
  175. if (__id == thread::id())
  176. return __out << "thread::id of a non-executing thread";
  177. else
  178. return __out << __id._M_thread;
  179. }
  180. /** @namespace std::this_thread
  181. * @brief ISO C++ 0x entities sub namespace for thread.
  182. * 30.2.2 Namespace this_thread.
  183. */
  184. namespace this_thread
  185. {
  186. /// get_id
  187. inline thread::id
  188. get_id() { return thread::id(__gthread_self()); }
  189. #ifdef _GLIBCXX_USE_SCHED_YIELD
  190. /// yield
  191. inline void
  192. yield()
  193. { __gthread_yield(); }
  194. #endif
  195. #ifdef _GLIBCXX_USE_NANOSLEEP
  196. /// sleep_until
  197. template<typename _Clock, typename _Duration>
  198. inline void
  199. sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
  200. { sleep_for(__atime - _Clock::now()); }
  201. /// sleep_for
  202. template<typename _Rep, typename _Period>
  203. inline void
  204. sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
  205. {
  206. chrono::seconds __s =
  207. chrono::duration_cast<chrono::seconds>(__rtime);
  208. chrono::nanoseconds __ns =
  209. chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
  210. __gthread_time_t __ts =
  211. {
  212. static_cast<std::time_t>(__s.count()),
  213. static_cast<long>(__ns.count())
  214. };
  215. ::nanosleep(&__ts, 0);
  216. }
  217. #endif
  218. }
  219. // @} group threads
  220. }
  221. #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
  222. #endif // __GXX_EXPERIMENTAL_CXX0X__
  223. #endif // _GLIBCXX_THREAD