]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/thread
thread (this_thread::get_id): Inline.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / thread
1 // <thread> -*- C++ -*-
2
3 // Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
20
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.
29
30 /** @file thread
31 * This is a Standard C++ Library header.
32 */
33
34 #ifndef _GLIBCXX_THREAD
35 #define _GLIBCXX_THREAD 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #else
42
43 #include <chrono>
44 #include <functional>
45 #include <memory>
46 #include <mutex>
47 #include <condition_variable>
48 #include <cstddef>
49 #include <bits/functexcept.h>
50 #include <bits/gthr.h>
51
52 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
53
54 namespace std
55 {
56 /// thread
57 class thread
58 {
59 public:
60 typedef __gthread_t native_handle_type;
61
62 /// thread::id
63 class id
64 {
65 native_handle_type _M_thread;
66
67 public:
68 id() : _M_thread() { }
69
70 explicit
71 id(native_handle_type __id) : _M_thread(__id) { }
72
73 private:
74 friend class thread;
75
76 friend bool
77 operator==(thread::id __x, thread::id __y)
78 { return __gthread_equal(__x._M_thread, __y._M_thread); }
79
80 friend bool
81 operator<(thread::id __x, thread::id __y)
82 { return __x._M_thread < __y._M_thread; }
83
84 template<class _CharT, class _Traits>
85 friend basic_ostream<_CharT, _Traits>&
86 operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id);
87 };
88
89 struct _Impl_base;
90 typedef shared_ptr<_Impl_base> __shared_base_type;
91
92 struct _Impl_base
93 {
94 id _M_id;
95 __shared_base_type _M_this_ptr;
96
97 _Impl_base() = default;
98
99 virtual ~_Impl_base() = default;
100
101 virtual void
102 _M_run() = 0;
103 };
104
105 template<typename _Callable>
106 class _Impl : public _Impl_base
107 {
108 _Callable _M_func;
109
110 public:
111 _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
112 { }
113
114 void
115 _M_run() { _M_func(); }
116 };
117
118 private:
119 // NB: Store the base type here.
120 __shared_base_type _M_data;
121
122 public:
123 thread() = default;
124 thread(const thread&) = delete;
125
126 thread(thread&& __t)
127 { swap(__t); }
128
129 template<typename _Callable>
130 explicit thread(_Callable __f)
131 : _M_data(_M_make_shared_data<_Callable>(__f))
132 { _M_start_thread(); }
133
134 template<typename _Callable, typename... _Args>
135 thread(_Callable&& __f, _Args&&... __args)
136 : _M_data(_M_make_shared_data(std::bind(__f, __args...)))
137 { _M_start_thread(); }
138
139 ~thread()
140 {
141 if (joinable())
142 detach();
143 }
144
145 thread& operator=(const thread&) = delete;
146
147 thread& operator=(thread&& __t)
148 {
149 if (joinable())
150 detach();
151 swap(__t);
152 return *this;
153 }
154
155 void
156 swap(thread&& __t)
157 { std::swap(_M_data, __t._M_data); }
158
159 bool
160 joinable() const
161 { return _M_data; }
162
163 void
164 join();
165
166 void
167 detach();
168
169 thread::id
170 get_id() const
171 {
172 if (_M_data)
173 return thread::id(_M_data->_M_id._M_thread);
174 else
175 return thread::id();
176 }
177
178 /** @pre thread is joinable
179 */
180 native_handle_type
181 native_handle()
182 { return _M_data->_M_id._M_thread; }
183
184 // Returns a value that hints at the number of hardware thread contexts.
185 static unsigned int
186 hardware_concurrency()
187 { return 0; }
188
189 private:
190 template<typename _Callable>
191 shared_ptr<_Impl<_Callable>>
192 _M_make_shared_data(_Callable&& __f)
193 {
194 // Create and allocate full data structure, not base.
195 return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
196 }
197
198 void _M_start_thread();
199 };
200
201 inline void
202 swap(thread& __x, thread& __y)
203 { __x.swap(__y); }
204
205 inline void
206 swap(thread&& __x, thread& __y)
207 { __x.swap(__y); }
208
209 inline void
210 swap(thread& __x, thread&& __y)
211 { __x.swap(__y); }
212
213 inline bool
214 operator!=(thread::id __x, thread::id __y)
215 { return !(__x == __y); }
216
217 inline bool
218 operator<=(thread::id __x, thread::id __y)
219 { return !(__y < __x); }
220
221 inline bool
222 operator>(thread::id __x, thread::id __y)
223 { return __y < __x; }
224
225 inline bool
226 operator>=(thread::id __x, thread::id __y)
227 { return !(__x < __y); }
228
229 template<class _CharT, class _Traits>
230 inline basic_ostream<_CharT, _Traits>&
231 operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
232 {
233 if (__id == thread::id())
234 return __out << "thread::id of a non-executing thread";
235 else
236 return __out << __id._M_thread;
237 }
238
239 // 30.2.2 Namespace this_thread.
240 namespace this_thread
241 {
242 inline thread::id
243 get_id() { return thread::id(__gthread_self()); }
244
245 #ifdef _GLIBCXX_USE_SCHED_YIELD
246 inline void
247 yield()
248 { __gthread_yield(); }
249 #endif
250
251 #ifdef _GLIBCXX_USE_NANOSLEEP
252 template<typename _Clock, typename _Duration>
253 inline void
254 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
255 { sleep_for(__atime - _Clock::now()); }
256
257 template<typename _Rep, typename _Period>
258 inline void
259 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
260 {
261 chrono::seconds __s =
262 chrono::duration_cast<chrono::seconds>(__rtime);
263
264 chrono::nanoseconds __ns =
265 chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
266
267 __gthread_time_t __ts =
268 {
269 static_cast<std::time_t>(__s.count()),
270 static_cast<long>(__ns.count())
271 };
272
273 ::nanosleep(&__ts, 0);
274 }
275 #endif
276 }
277 }
278
279 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
280
281 #endif // __GXX_EXPERIMENTAL_CXX0X__
282
283 #endif // _GLIBCXX_THREAD