e43c126cdceb908f25fd6046d5df171289c3b981
[gcc.git] / libstdc++-v3 / include / std / thread
1 // <thread> -*- C++ -*-
2
3 // Copyright (C) 2008, 2009, 2010, 2011 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/thread
26 * This is a Standard C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <chrono>
39 #include <functional>
40 #include <memory>
41 #include <mutex>
42 #include <condition_variable>
43 #include <bits/functexcept.h>
44 #include <bits/functional_hash.h>
45 #include <bits/gthr.h>
46
47 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
48
49 namespace std _GLIBCXX_VISIBILITY(default)
50 {
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52
53 /**
54 * @defgroup threads Threads
55 * @ingroup concurrency
56 *
57 * Classes for thread support.
58 * @{
59 */
60
61 /// thread
62 class thread
63 {
64 public:
65 typedef __gthread_t native_handle_type;
66 struct _Impl_base;
67 typedef shared_ptr<_Impl_base> __shared_base_type;
68
69 /// thread::id
70 class id
71 {
72 native_handle_type _M_thread;
73
74 public:
75 id() : _M_thread() { }
76
77 explicit
78 id(native_handle_type __id) : _M_thread(__id) { }
79
80 private:
81 friend class thread;
82 friend class hash<thread::id>;
83
84 friend bool
85 operator==(thread::id __x, thread::id __y)
86 { return __gthread_equal(__x._M_thread, __y._M_thread); }
87
88 friend bool
89 operator<(thread::id __x, thread::id __y)
90 { return __x._M_thread < __y._M_thread; }
91
92 template<class _CharT, class _Traits>
93 friend basic_ostream<_CharT, _Traits>&
94 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
95 };
96
97 // Simple base type that the templatized, derived class containing
98 // an arbitrary functor can be converted to and called.
99 struct _Impl_base
100 {
101 __shared_base_type _M_this_ptr;
102
103 virtual ~_Impl_base();
104
105 virtual void _M_run() = 0;
106 };
107
108 template<typename _Callable>
109 struct _Impl : public _Impl_base
110 {
111 _Callable _M_func;
112
113 _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
114 { }
115
116 void
117 _M_run() { _M_func(); }
118 };
119
120 private:
121 id _M_id;
122
123 public:
124 thread() = default;
125 thread(thread&) = delete;
126 thread(const thread&) = delete;
127
128 thread(thread&& __t)
129 { swap(__t); }
130
131 template<typename _Callable, typename... _Args>
132 explicit
133 thread(_Callable&& __f, _Args&&... __args)
134 {
135 _M_start_thread(_M_make_routine(std::bind<void>(
136 std::forward<_Callable>(__f),
137 std::forward<_Args>(__args)...)));
138 }
139
140 ~thread()
141 {
142 if (joinable())
143 std::terminate();
144 }
145
146 thread& operator=(const thread&) = delete;
147
148 thread& operator=(thread&& __t)
149 {
150 if (joinable())
151 std::terminate();
152 swap(__t);
153 return *this;
154 }
155
156 void
157 swap(thread& __t)
158 { std::swap(_M_id, __t._M_id); }
159
160 bool
161 joinable() const
162 { return !(_M_id == id()); }
163
164 void
165 join();
166
167 void
168 detach();
169
170 thread::id
171 get_id() const
172 { return _M_id; }
173
174 /** @pre thread is joinable
175 */
176 native_handle_type
177 native_handle()
178 { return _M_id._M_thread; }
179
180 // Returns a value that hints at the number of hardware thread contexts.
181 static unsigned int
182 hardware_concurrency()
183 { return 0; }
184
185 private:
186 void
187 _M_start_thread(__shared_base_type);
188
189 template<typename _Callable>
190 shared_ptr<_Impl<_Callable>>
191 _M_make_routine(_Callable&& __f)
192 {
193 // Create and allocate full data structure, not base.
194 return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
195 }
196 };
197
198 inline void
199 swap(thread& __x, thread& __y)
200 { __x.swap(__y); }
201
202 inline bool
203 operator!=(thread::id __x, thread::id __y)
204 { return !(__x == __y); }
205
206 inline bool
207 operator<=(thread::id __x, thread::id __y)
208 { return !(__y < __x); }
209
210 inline bool
211 operator>(thread::id __x, thread::id __y)
212 { return __y < __x; }
213
214 inline bool
215 operator>=(thread::id __x, thread::id __y)
216 { return !(__x < __y); }
217
218 // DR 889.
219 /// std::hash specialization for thread::id.
220 template<>
221 struct hash<thread::id>
222 : public __hash_base<size_t, thread::id>
223 {
224 size_t
225 operator()(const thread::id& __id) const
226 { return std::_Hash_impl::hash(__id._M_thread); }
227 };
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 _GLIBCXX_END_NAMESPACE_VERSION
240
241 /** @namespace std::this_thread
242 * @brief ISO C++ 0x entities sub namespace for thread.
243 * 30.2.2 Namespace this_thread.
244 */
245 namespace this_thread
246 {
247 _GLIBCXX_BEGIN_NAMESPACE_VERSION
248
249 /// get_id
250 inline thread::id
251 get_id() { return thread::id(__gthread_self()); }
252
253 #ifdef _GLIBCXX_USE_SCHED_YIELD
254 /// yield
255 inline void
256 yield()
257 { __gthread_yield(); }
258 #endif
259
260 #ifdef _GLIBCXX_USE_NANOSLEEP
261 /// sleep_until
262 template<typename _Clock, typename _Duration>
263 inline void
264 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
265 { sleep_for(__atime - _Clock::now()); }
266
267 /// sleep_for
268 template<typename _Rep, typename _Period>
269 inline void
270 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
271 {
272 chrono::seconds __s =
273 chrono::duration_cast<chrono::seconds>(__rtime);
274
275 chrono::nanoseconds __ns =
276 chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
277
278 __gthread_time_t __ts =
279 {
280 static_cast<std::time_t>(__s.count()),
281 static_cast<long>(__ns.count())
282 };
283
284 ::nanosleep(&__ts, 0);
285 }
286 #endif
287
288 _GLIBCXX_END_NAMESPACE_VERSION
289 }
290
291 // @} group threads
292
293 } // namespace
294
295 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
296
297 #endif // __GXX_EXPERIMENTAL_CXX0X__
298
299 #endif // _GLIBCXX_THREAD