|
libstdc++
|
00001 // <thread> -*- C++ -*- 00002 00003 // Copyright (C) 2008-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/thread 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _GLIBCXX_THREAD 00030 #define _GLIBCXX_THREAD 1 00031 00032 #pragma GCC system_header 00033 00034 #if __cplusplus < 201103L 00035 # include <bits/c++0x_warning.h> 00036 #else 00037 00038 #include <chrono> 00039 #include <functional> 00040 #include <memory> 00041 #include <bits/functexcept.h> 00042 #include <bits/functional_hash.h> 00043 #include <bits/gthr.h> 00044 00045 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) 00046 00047 namespace std _GLIBCXX_VISIBILITY(default) 00048 { 00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00050 00051 /** 00052 * @defgroup threads Threads 00053 * @ingroup concurrency 00054 * 00055 * Classes for thread support. 00056 * @{ 00057 */ 00058 00059 /// thread 00060 class thread 00061 { 00062 public: 00063 typedef __gthread_t native_handle_type; 00064 struct _Impl_base; 00065 typedef shared_ptr<_Impl_base> __shared_base_type; 00066 00067 /// thread::id 00068 class id 00069 { 00070 native_handle_type _M_thread; 00071 00072 public: 00073 id() noexcept : _M_thread() { } 00074 00075 explicit 00076 id(native_handle_type __id) : _M_thread(__id) { } 00077 00078 private: 00079 friend class thread; 00080 friend class hash<thread::id>; 00081 00082 friend bool 00083 operator==(thread::id __x, thread::id __y) noexcept 00084 { return __gthread_equal(__x._M_thread, __y._M_thread); } 00085 00086 friend bool 00087 operator<(thread::id __x, thread::id __y) noexcept 00088 { return __x._M_thread < __y._M_thread; } 00089 00090 template<class _CharT, class _Traits> 00091 friend basic_ostream<_CharT, _Traits>& 00092 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id); 00093 }; 00094 00095 // Simple base type that the templatized, derived class containing 00096 // an arbitrary functor can be converted to and called. 00097 struct _Impl_base 00098 { 00099 __shared_base_type _M_this_ptr; 00100 00101 inline virtual ~_Impl_base(); 00102 00103 virtual void _M_run() = 0; 00104 }; 00105 00106 template<typename _Callable> 00107 struct _Impl : public _Impl_base 00108 { 00109 _Callable _M_func; 00110 00111 _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f)) 00112 { } 00113 00114 void 00115 _M_run() { _M_func(); } 00116 }; 00117 00118 private: 00119 id _M_id; 00120 00121 public: 00122 thread() noexcept = default; 00123 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00124 // 2097. packaged_task constructors should be constrained 00125 thread(thread&) = delete; 00126 thread(const thread&) = delete; 00127 thread(const thread&&) = delete; 00128 00129 thread(thread&& __t) noexcept 00130 { swap(__t); } 00131 00132 template<typename _Callable, typename... _Args> 00133 explicit 00134 thread(_Callable&& __f, _Args&&... __args) 00135 { 00136 #ifdef GTHR_ACTIVE_PROXY 00137 // Create a reference to pthread_create, not just the gthr weak symbol 00138 _M_start_thread(_M_make_routine(std::__bind_simple( 00139 std::forward<_Callable>(__f), 00140 std::forward<_Args>(__args)...)), 00141 reinterpret_cast<void(*)()>(&pthread_create)); 00142 #else 00143 _M_start_thread(_M_make_routine(std::__bind_simple( 00144 std::forward<_Callable>(__f), 00145 std::forward<_Args>(__args)...))); 00146 #endif 00147 } 00148 00149 ~thread() 00150 { 00151 if (joinable()) 00152 std::terminate(); 00153 } 00154 00155 thread& operator=(const thread&) = delete; 00156 00157 thread& operator=(thread&& __t) noexcept 00158 { 00159 if (joinable()) 00160 std::terminate(); 00161 swap(__t); 00162 return *this; 00163 } 00164 00165 void 00166 swap(thread& __t) noexcept 00167 { std::swap(_M_id, __t._M_id); } 00168 00169 bool 00170 joinable() const noexcept 00171 { return !(_M_id == id()); } 00172 00173 void 00174 join(); 00175 00176 void 00177 detach(); 00178 00179 thread::id 00180 get_id() const noexcept 00181 { return _M_id; } 00182 00183 /** @pre thread is joinable 00184 */ 00185 native_handle_type 00186 native_handle() 00187 { return _M_id._M_thread; } 00188 00189 // Returns a value that hints at the number of hardware thread contexts. 00190 static unsigned int 00191 hardware_concurrency() noexcept; 00192 00193 private: 00194 void 00195 _M_start_thread(__shared_base_type, void (*)()); 00196 00197 void 00198 _M_start_thread(__shared_base_type); 00199 00200 template<typename _Callable> 00201 shared_ptr<_Impl<_Callable>> 00202 _M_make_routine(_Callable&& __f) 00203 { 00204 // Create and allocate full data structure, not base. 00205 return std::make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f)); 00206 } 00207 }; 00208 00209 inline thread::_Impl_base::~_Impl_base() = default; 00210 00211 inline void 00212 swap(thread& __x, thread& __y) noexcept 00213 { __x.swap(__y); } 00214 00215 inline bool 00216 operator!=(thread::id __x, thread::id __y) noexcept 00217 { return !(__x == __y); } 00218 00219 inline bool 00220 operator<=(thread::id __x, thread::id __y) noexcept 00221 { return !(__y < __x); } 00222 00223 inline bool 00224 operator>(thread::id __x, thread::id __y) noexcept 00225 { return __y < __x; } 00226 00227 inline bool 00228 operator>=(thread::id __x, thread::id __y) noexcept 00229 { return !(__x < __y); } 00230 00231 // DR 889. 00232 /// std::hash specialization for thread::id. 00233 template<> 00234 struct hash<thread::id> 00235 : public __hash_base<size_t, thread::id> 00236 { 00237 size_t 00238 operator()(const thread::id& __id) const noexcept 00239 { return std::_Hash_impl::hash(__id._M_thread); } 00240 }; 00241 00242 template<class _CharT, class _Traits> 00243 inline basic_ostream<_CharT, _Traits>& 00244 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id) 00245 { 00246 if (__id == thread::id()) 00247 return __out << "thread::id of a non-executing thread"; 00248 else 00249 return __out << __id._M_thread; 00250 } 00251 00252 _GLIBCXX_END_NAMESPACE_VERSION 00253 00254 /** @namespace std::this_thread 00255 * @brief ISO C++ 2011 entities sub-namespace for thread. 00256 * 30.3.2 Namespace this_thread. 00257 */ 00258 namespace this_thread 00259 { 00260 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00261 00262 /// get_id 00263 inline thread::id 00264 get_id() noexcept { return thread::id(__gthread_self()); } 00265 00266 /// yield 00267 inline void 00268 yield() noexcept 00269 { 00270 #ifdef _GLIBCXX_USE_SCHED_YIELD 00271 __gthread_yield(); 00272 #endif 00273 } 00274 00275 void 00276 __sleep_for(chrono::seconds, chrono::nanoseconds); 00277 00278 /// sleep_for 00279 template<typename _Rep, typename _Period> 00280 inline void 00281 sleep_for(const chrono::duration<_Rep, _Period>& __rtime) 00282 { 00283 if (__rtime <= __rtime.zero()) 00284 return; 00285 auto __s = chrono::duration_cast<chrono::seconds>(__rtime); 00286 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s); 00287 #ifdef _GLIBCXX_USE_NANOSLEEP 00288 __gthread_time_t __ts = 00289 { 00290 static_cast<std::time_t>(__s.count()), 00291 static_cast<long>(__ns.count()) 00292 }; 00293 ::nanosleep(&__ts, 0); 00294 #else 00295 __sleep_for(__s, __ns); 00296 #endif 00297 } 00298 00299 /// sleep_until 00300 template<typename _Clock, typename _Duration> 00301 inline void 00302 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime) 00303 { 00304 auto __now = _Clock::now(); 00305 if (__now < __atime) 00306 sleep_for(__atime - __now); 00307 } 00308 00309 _GLIBCXX_END_NAMESPACE_VERSION 00310 } 00311 00312 // @} group threads 00313 00314 } // namespace 00315 00316 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 00317 00318 #endif // C++11 00319 00320 #endif // _GLIBCXX_THREAD