1 // <thread> -*- C++ -*-
3 // Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * This is a Standard C++ Library header.
34 #ifndef _GLIBCXX_THREAD
35 #define _GLIBCXX_THREAD 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
48 #include <condition_variable>
49 #include <type_traits>
51 #include <bits/functexcept.h>
52 #include <bits/gthr.h>
54 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
58 class __thread_data_base;
60 typedef shared_ptr<__thread_data_base> __thread_data_ptr;
62 class __thread_data_base : public enable_shared_from_this<__thread_data_base>
65 __thread_data_base() = default;
66 virtual ~__thread_data_base() = default;
68 virtual void __run() const = 0;
70 __gthread_t _M_thread_handle;
71 __thread_data_ptr _M_this_ptr;
75 template<typename _Callable>
76 class __thread_data : public __thread_data_base
79 __thread_data(_Callable&& __f)
80 : _M_func(std::forward<_Callable>(__f))
96 typedef __gthread_t native_handle_type;
101 template<typename _Callable>
102 explicit thread(_Callable __f)
103 : _M_thread_data(__make_thread_data(__f))
104 { __start_thread(); }
106 template<typename _Callable, typename... _Args>
107 thread(_Callable&& __f, _Args&&... __args)
108 : _M_thread_data(__make_thread_data(std::bind(__f, __args...)))
109 { __start_thread(); }
114 thread(const thread&) = delete;
116 thread& operator=(const thread&) = delete;
117 thread& operator=(thread&&);
122 { std::swap(_M_thread_data, __t._M_thread_data); }
138 { return _M_thread_data->_M_thread_handle; }
141 static unsigned hardware_concurrency();
144 _M_get_thread_data() const
146 lock_guard<mutex> __l(_M_thread_data_mutex);
147 return _M_thread_data;
151 template<typename _Callable>
153 __make_thread_data(_Callable&& __f)
155 return __thread_data_ptr(
156 new __thread_data<_Callable>(std::forward<_Callable>(__f)));
160 __make_thread_data(void(*__f)())
161 { return __thread_data_ptr(new __thread_data<void(*)()>(__f)); }
163 void __start_thread();
165 __thread_data_ptr _M_thread_data;
166 mutable mutex _M_thread_data_mutex;
170 swap(thread& __x, thread& __y)
174 swap(thread&& __x, thread& __y)
178 swap(thread& __x, thread&& __y)
181 namespace this_thread
186 #ifdef _GLIBCXX_USE_SCHED_YIELD
189 { __gthread_yield(); }
192 #ifdef _GLIBCXX_USE_NANOSLEEP
193 template<typename _Clock, typename _Duration>
195 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
196 { sleep_for(__atime - _Clock::now()); }
198 template<typename _Rep, typename _Period>
200 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
202 chrono::seconds __s =
203 chrono::duration_cast<chrono::seconds>(__rtime);
205 chrono::nanoseconds __ns =
206 chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
208 __gthread_time_t __ts =
210 static_cast<std::time_t>(__s.count()),
211 static_cast<long>(__ns.count())
214 ::nanosleep(&__ts, 0);
223 id() : _M_thread_id() { }
228 friend thread::id this_thread::get_id();
231 operator==(thread::id __x, thread::id __y)
232 { return static_cast<bool>(__gthread_equal(__x._M_thread_id,
233 __y._M_thread_id)); }
236 operator<(thread::id __x, thread::id __y)
237 { return __x._M_thread_id < __y._M_thread_id; }
239 template<class _CharT, class _Traits>
240 friend basic_ostream<_CharT, _Traits>&
241 operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id);
247 __gthread_t _M_thread_id;
251 operator!=(thread::id __x, thread::id __y)
252 { return !(__x == __y); }
255 operator<=(thread::id __x, thread::id __y)
256 { return !(__y < __x); }
259 operator>(thread::id __x, thread::id __y)
260 { return __y < __x; }
263 operator>=(thread::id __x, thread::id __y)
264 { return !(__x < __y); }
266 template<class _CharT, class _Traits>
267 inline basic_ostream<_CharT, _Traits>&
268 operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
270 if(__id == thread::id())
271 return __out << "non-executing thread";
273 return __out << __id._M_thread_id;
277 thread::joinable() const
278 { return get_id() != thread::id(); }
281 thread::get_id() const
284 return thread::id(_M_thread_data->_M_thread_handle);
289 namespace this_thread
293 { return thread::id(__gthread_self()); }
297 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
299 #endif // __GXX_EXPERIMENTAL_CXX0X__
301 #endif // _GLIBCXX_THREAD