1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
// <thread> -*- C++ -*-
// Copyright (C) 2008-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/thread
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_THREAD
#define _GLIBCXX_THREAD 1
#ifdef _GLIBCXX_SYSHDR
#pragma GCC system_header
#endif
#include <bits/requires_hosted.h> // concurrency
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
#if __cplusplus > 201703L
# include <compare> // std::strong_ordering
# include <stop_token> // std::stop_source, std::stop_token, std::nostopstate
#endif
#include <bits/std_thread.h> // std::thread, get_id, yield
#include <bits/this_thread_sleep.h> // std::this_thread::sleep_for, sleep_until
#define __glibcxx_want_jthread
#define __glibcxx_want_formatters
#include <bits/version.h>
#if __cpp_lib_formatters
# include <format>
#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* @defgroup threads Threads
* @ingroup concurrency
* @since C++11
*
* Classes for thread support.
* @{
*/
// std::thread is defined in <bits/std_thread.h>
/// @relates std::thread::id @{
#if __cpp_lib_three_way_comparison
inline strong_ordering
operator<=>(thread::id __x, thread::id __y) noexcept
{ return __x._M_thread <=> __y._M_thread; }
#else
inline bool
operator!=(thread::id __x, thread::id __y) noexcept
{ return !(__x == __y); }
inline bool
operator<(thread::id __x, thread::id __y) noexcept
{
// Pthreads doesn't define any way to do this, so we just have to
// assume native_handle_type is LessThanComparable.
return __x._M_thread < __y._M_thread;
}
inline bool
operator<=(thread::id __x, thread::id __y) noexcept
{ return !(__y < __x); }
inline bool
operator>(thread::id __x, thread::id __y) noexcept
{ return __y < __x; }
inline bool
operator>=(thread::id __x, thread::id __y) noexcept
{ return !(__x < __y); }
#endif // __cpp_lib_three_way_comparison
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
{
// Convert non-void pointers to const void* for formatted output.
using __output_type
= __conditional_t<is_pointer<thread::native_handle_type>::value,
const void*,
thread::native_handle_type>;
if (__id == thread::id())
return __out << "thread::id of a non-executing thread";
else
return __out << static_cast<__output_type>(__id._M_thread);
}
/// @}
#ifdef __cpp_lib_jthread // C++ >= 20
/// @cond undocumented
#ifndef __STRICT_ANSI__
template<typename _Callable, typename... _Args>
constexpr bool __pmf_expects_stop_token = false;
template<typename _Callable, typename _Obj, typename... _Args>
constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
= __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
is_invocable<_Callable, _Obj, stop_token, _Args...>>::value;
#endif
/// @endcond
/** A thread with cancellation and automatic joining.
*
* Unlike `std::thread`, destroying a joinable `std::jthread` will not
* terminate the process. Instead, it will try to request its thread to
* stop, then will join it.
*
* A `std::jthread` has a `std::stop_source` member which will be passed
* as the first argument to the callable that runs in the new thread
* (as long as the callable will accept that argument). That can then
* be used to send a stop request that the new thread can test for.
*
* @headerfile thread
* @since C++20
*/
class jthread
{
public:
using id = thread::id;
using native_handle_type = thread::native_handle_type;
jthread() noexcept
: _M_stop_source{nostopstate}
{ }
template<typename _Callable, typename... _Args,
typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
jthread>>>
explicit
jthread(_Callable&& __f, _Args&&... __args)
: _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
std::forward<_Args>(__args)...)}
{ }
jthread(const jthread&) = delete;
jthread(jthread&&) noexcept = default;
~jthread()
{
if (joinable())
{
request_stop();
join();
}
}
jthread&
operator=(const jthread&) = delete;
jthread&
operator=(jthread&& __other) noexcept
{
std::jthread(std::move(__other)).swap(*this);
return *this;
}
void
swap(jthread& __other) noexcept
{
std::swap(_M_stop_source, __other._M_stop_source);
std::swap(_M_thread, __other._M_thread);
}
[[nodiscard]] bool
joinable() const noexcept
{
return _M_thread.joinable();
}
void
join()
{
_M_thread.join();
}
void
detach()
{
_M_thread.detach();
}
[[nodiscard]] id
get_id() const noexcept
{
return _M_thread.get_id();
}
[[nodiscard]] native_handle_type
native_handle()
{
return _M_thread.native_handle();
}
[[nodiscard]] static unsigned
hardware_concurrency() noexcept
{
return thread::hardware_concurrency();
}
[[nodiscard]] stop_source
get_stop_source() noexcept
{
return _M_stop_source;
}
[[nodiscard]] stop_token
get_stop_token() const noexcept
{
return _M_stop_source.get_token();
}
bool request_stop() noexcept
{
return _M_stop_source.request_stop();
}
friend void swap(jthread& __lhs, jthread& __rhs) noexcept
{
__lhs.swap(__rhs);
}
private:
template<typename _Callable, typename... _Args>
static thread
_S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
{
#ifndef __STRICT_ANSI__
if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
else
#endif
if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
decay_t<_Args>...>)
return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
std::forward<_Args>(__args)...};
else
{
static_assert(is_invocable_v<decay_t<_Callable>,
decay_t<_Args>...>,
"std::jthread arguments must be invocable after"
" conversion to rvalues");
return thread{std::forward<_Callable>(__f),
std::forward<_Args>(__args)...};
}
}
#ifndef __STRICT_ANSI__
template<typename _Callable, typename _Obj, typename... _Args>
static thread
_S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj,
_Args&&... __args)
{
return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
std::forward<_Args>(__args)...};
}
#endif
stop_source _M_stop_source;
thread _M_thread;
};
#endif // __cpp_lib_jthread
#ifdef __cpp_lib_formatters // C++ >= 23
template<typename _CharT>
requires is_pointer_v<thread::native_handle_type>
|| is_integral_v<thread::native_handle_type>
class formatter<thread::id, _CharT>
{
public:
constexpr typename basic_format_parse_context<_CharT>::iterator
parse(basic_format_parse_context<_CharT>& __pc)
{
__format::_Spec<_CharT> __spec{};
const auto __last = __pc.end();
auto __first = __pc.begin();
auto __finalize = [this, &__spec] {
_M_spec = __spec;
};
auto __finished = [&] {
if (__first == __last || *__first == '}')
{
__finalize();
return true;
}
return false;
};
if (__finished())
return __first;
__first = __spec._M_parse_fill_and_align(__first, __last);
if (__finished())
return __first;
__first = __spec._M_parse_width(__first, __last, __pc);
if (__finished())
return __first;
__throw_format_error("format error: invalid format-spec for "
"std::thread::id");
}
template<typename _Out>
typename basic_format_context<_Out, _CharT>::iterator
format(thread::id __id, basic_format_context<_Out, _CharT>& __fc) const
{
basic_string_view<_CharT> __sv;
if constexpr (is_same_v<_CharT, char>)
__sv = "{}thread::id of a non-executing thread";
else
__sv = L"{}thread::id of a non-executing thread";
basic_string<_CharT> __str;
if (__id == thread::id())
__sv.remove_prefix(2);
else
{
using _FmtStr = __format::_Runtime_format_string<_CharT>;
// Convert non-void pointers to const void* for formatted output.
using __output_type
= __conditional_t<is_pointer_v<thread::native_handle_type>,
const void*,
thread::native_handle_type>;
auto __o = static_cast<__output_type>(__id._M_thread);
__sv = __str = std::format(_FmtStr(__sv.substr(0, 2)), __o);
}
return __format::__write_padded_as_spec(__sv, __sv.size(),
__fc, _M_spec,
__format::_Align_right);
}
private:
__format::_Spec<_CharT> _M_spec;
};
#endif // __cpp_lib_formatters
/// @} group threads
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
#endif // C++11
#endif // _GLIBCXX_THREAD
|