]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/thread
re PR libstdc++/53872 ([C++11] ADL bug in std::thread)
[thirdparty/gcc.git] / libstdc++-v3 / include / std / thread
CommitLineData
46e113bf
CF
1// <thread> -*- C++ -*-
2
f8b54112 3// Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
46e113bf
CF
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
46e113bf
CF
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
748086b7
JJ
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/>.
46e113bf 24
f910786b 25/** @file include/thread
46e113bf
CF
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__
ab65a4c7 35# include <bits/c++0x_warning.h>
46e113bf
CF
36#else
37
38#include <chrono>
46e113bf
CF
39#include <functional>
40#include <memory>
46e113bf 41#include <bits/functexcept.h>
5c8db18a 42#include <bits/functional_hash.h>
46e113bf
CF
43#include <bits/gthr.h>
44
45#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
46
12ffa228
BK
47namespace std _GLIBCXX_VISIBILITY(default)
48{
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
53dc5044 50
5b9daa7e
BK
51 /**
52 * @defgroup threads Threads
53 * @ingroup concurrency
54 *
55 * Classes for thread support.
56 * @{
57 */
58
d7afcd2b 59 /// thread
8644ecf5 60 class thread
46e113bf
CF
61 {
62 public:
626dda69 63 typedef __gthread_t native_handle_type;
f7459b6c
BK
64 struct _Impl_base;
65 typedef shared_ptr<_Impl_base> __shared_base_type;
d7afcd2b
BK
66
67 /// thread::id
68 class id
46e113bf 69 {
626dda69 70 native_handle_type _M_thread;
d7afcd2b 71
46e113bf 72 public:
7f0d79d5 73 id() noexcept : _M_thread() { }
d7afcd2b
BK
74
75 explicit
76 id(native_handle_type __id) : _M_thread(__id) { }
77
78 private:
79 friend class thread;
5c8db18a 80 friend class hash<thread::id>;
d7afcd2b
BK
81
82 friend bool
7f0d79d5 83 operator==(thread::id __x, thread::id __y) noexcept
d7afcd2b
BK
84 { return __gthread_equal(__x._M_thread, __y._M_thread); }
85
86 friend bool
7f0d79d5 87 operator<(thread::id __x, thread::id __y) noexcept
d7afcd2b
BK
88 { return __x._M_thread < __y._M_thread; }
89
90 template<class _CharT, class _Traits>
91 friend basic_ostream<_CharT, _Traits>&
ff74fd13 92 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
d7afcd2b
BK
93 };
94
626dda69 95 // Simple base type that the templatized, derived class containing
9b3003d5 96 // an arbitrary functor can be converted to and called.
d7afcd2b
BK
97 struct _Impl_base
98 {
626dda69 99 __shared_base_type _M_this_ptr;
d7afcd2b 100
d5545744 101 inline virtual ~_Impl_base();
d7afcd2b 102
626dda69 103 virtual void _M_run() = 0;
46e113bf 104 };
46e113bf 105
d7afcd2b 106 template<typename _Callable>
626dda69 107 struct _Impl : public _Impl_base
d7afcd2b 108 {
626dda69 109 _Callable _M_func;
d7afcd2b 110
d7afcd2b
BK
111 _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
112 { }
113
626dda69 114 void
d7afcd2b
BK
115 _M_run() { _M_func(); }
116 };
117
118 private:
626dda69 119 id _M_id;
d7afcd2b
BK
120
121 public:
7f0d79d5 122 thread() noexcept = default;
b2edc921 123 thread(thread&) = delete;
d7afcd2b
BK
124 thread(const thread&) = delete;
125
7f0d79d5 126 thread(thread&& __t) noexcept
d7afcd2b
BK
127 { swap(__t); }
128
46e113bf 129 template<typename _Callable, typename... _Args>
1b3fad81 130 explicit
46e113bf 131 thread(_Callable&& __f, _Args&&... __args)
1b3fad81 132 {
4880236e 133 _M_start_thread(_M_make_routine(std::__bind_simple(
1b3fad81
JW
134 std::forward<_Callable>(__f),
135 std::forward<_Args>(__args)...)));
136 }
46e113bf 137
959d14e1 138 ~thread()
cbdab9c8
JW
139 {
140 if (joinable())
cd3b0faf 141 std::terminate();
cbdab9c8 142 }
46e113bf 143
46e113bf 144 thread& operator=(const thread&) = delete;
d7afcd2b 145
7f0d79d5 146 thread& operator=(thread&& __t) noexcept
78b580a9
JW
147 {
148 if (joinable())
cd3b0faf 149 std::terminate();
78b580a9
JW
150 swap(__t);
151 return *this;
152 }
46e113bf 153
d7afcd2b 154 void
7f0d79d5 155 swap(thread& __t) noexcept
626dda69 156 { std::swap(_M_id, __t._M_id); }
46e113bf 157
d7afcd2b 158 bool
7f0d79d5 159 joinable() const noexcept
626dda69 160 { return !(_M_id == id()); }
46e113bf 161
d7afcd2b 162 void
46e113bf
CF
163 join();
164
d7afcd2b 165 void
46e113bf
CF
166 detach();
167
959d14e1 168 thread::id
7f0d79d5 169 get_id() const noexcept
626dda69 170 { return _M_id; }
46e113bf 171
cbdab9c8
JW
172 /** @pre thread is joinable
173 */
d7afcd2b 174 native_handle_type
46e113bf 175 native_handle()
626dda69 176 { return _M_id._M_thread; }
46e113bf 177
d7afcd2b
BK
178 // Returns a value that hints at the number of hardware thread contexts.
179 static unsigned int
43653c33 180 hardware_concurrency() noexcept;
46e113bf 181
46e113bf 182 private:
626dda69
CF
183 void
184 _M_start_thread(__shared_base_type);
185
46e113bf 186 template<typename _Callable>
d7afcd2b 187 shared_ptr<_Impl<_Callable>>
626dda69 188 _M_make_routine(_Callable&& __f)
8644ecf5 189 {
d7afcd2b 190 // Create and allocate full data structure, not base.
f8b54112 191 return std::make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
46e113bf 192 }
46e113bf
CF
193 };
194
d5545744
BK
195 inline thread::_Impl_base::~_Impl_base() = default;
196
46e113bf 197 inline void
7f0d79d5 198 swap(thread& __x, thread& __y) noexcept
46e113bf
CF
199 { __x.swap(__y); }
200
d7afcd2b 201 inline bool
7f0d79d5 202 operator!=(thread::id __x, thread::id __y) noexcept
d7afcd2b
BK
203 { return !(__x == __y); }
204
205 inline bool
7f0d79d5 206 operator<=(thread::id __x, thread::id __y) noexcept
d7afcd2b
BK
207 { return !(__y < __x); }
208
209 inline bool
7f0d79d5 210 operator>(thread::id __x, thread::id __y) noexcept
d7afcd2b
BK
211 { return __y < __x; }
212
213 inline bool
7f0d79d5 214 operator>=(thread::id __x, thread::id __y) noexcept
d7afcd2b
BK
215 { return !(__x < __y); }
216
5c8db18a
PC
217 // DR 889.
218 /// std::hash specialization for thread::id.
219 template<>
220 struct hash<thread::id>
5d64ee19 221 : public __hash_base<size_t, thread::id>
5c8db18a
PC
222 {
223 size_t
72f1c34b 224 operator()(const thread::id& __id) const noexcept
e7f72940 225 { return std::_Hash_impl::hash(__id._M_thread); }
5c8db18a
PC
226 };
227
d7afcd2b
BK
228 template<class _CharT, class _Traits>
229 inline basic_ostream<_CharT, _Traits>&
ff74fd13 230 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
d7afcd2b
BK
231 {
232 if (__id == thread::id())
233 return __out << "thread::id of a non-executing thread";
234 else
235 return __out << __id._M_thread;
236 }
237
12ffa228
BK
238_GLIBCXX_END_NAMESPACE_VERSION
239
5b9daa7e
BK
240 /** @namespace std::this_thread
241 * @brief ISO C++ 0x entities sub namespace for thread.
242 * 30.2.2 Namespace this_thread.
243 */
46e113bf
CF
244 namespace this_thread
245 {
12ffa228
BK
246 _GLIBCXX_BEGIN_NAMESPACE_VERSION
247
f7459b6c 248 /// get_id
4a50cd93 249 inline thread::id
7f0d79d5 250 get_id() noexcept { return thread::id(__gthread_self()); }
46e113bf 251
959d14e1 252#ifdef _GLIBCXX_USE_SCHED_YIELD
f7459b6c 253 /// yield
959d14e1 254 inline void
7f0d79d5 255 yield() noexcept
959d14e1
CF
256 { __gthread_yield(); }
257#endif
46e113bf 258
959d14e1 259#ifdef _GLIBCXX_USE_NANOSLEEP
f7459b6c 260 /// sleep_for
46e113bf 261 template<typename _Rep, typename _Period>
959d14e1 262 inline void
46e113bf
CF
263 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
264 {
265 chrono::seconds __s =
266 chrono::duration_cast<chrono::seconds>(__rtime);
267
268 chrono::nanoseconds __ns =
269 chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
270
d7afcd2b 271 __gthread_time_t __ts =
46e113bf
CF
272 {
273 static_cast<std::time_t>(__s.count()),
274 static_cast<long>(__ns.count())
275 };
276
46e113bf 277 ::nanosleep(&__ts, 0);
46e113bf 278 }
d1129441
JW
279
280 /// sleep_until
281 template<typename _Clock, typename _Duration>
282 inline void
283 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
284 { sleep_for(__atime - _Clock::now()); }
959d14e1 285#endif
12ffa228
BK
286
287 _GLIBCXX_END_NAMESPACE_VERSION
46e113bf 288 }
5b9daa7e
BK
289
290 // @} group threads
12ffa228
BK
291
292} // namespace
46e113bf
CF
293
294#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
295
296#endif // __GXX_EXPERIMENTAL_CXX0X__
297
298#endif // _GLIBCXX_THREAD