29#ifndef _GLIBCXX_STOP_TOKEN
30#define _GLIBCXX_STOP_TOKEN
34#if __cplusplus > 201703L
41#define __cpp_lib_jthread 201911L
43namespace std _GLIBCXX_VISIBILITY(default)
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
65 operator=(
const stop_token&)
noexcept =
default;
72 stop_possible()
const noexcept
74 return static_cast<bool>(_M_state) && _M_state->_M_stop_possible();
79 stop_requested()
const noexcept
81 return static_cast<bool>(_M_state) && _M_state->_M_stop_requested();
86 { _M_state.swap(__rhs._M_state); }
91 {
return __a._M_state == __b._M_state; }
95 { __lhs.swap(__rhs); }
99 template<
typename _Callback>
105#if defined __i386__ || defined __x86_64__
106 __builtin_ia32_pause();
111#ifndef __cpp_lib_semaphore
112 struct binary_semaphore
114 explicit binary_semaphore(
int __d) : _M_counter(__d > 0) { }
116 void release() { _M_counter.fetch_add(1, memory_order::release); }
121 while (!_M_counter.compare_exchange_weak(__old, 0,
122 memory_order::acquire,
123 memory_order::relaxed))
136 using __cb_type = void(_Stop_cb*)
noexcept;
137 __cb_type* _M_callback;
138 _Stop_cb* _M_prev =
nullptr;
139 _Stop_cb* _M_next =
nullptr;
140 bool* _M_destroyed =
nullptr;
141 binary_semaphore _M_done{0};
143 [[__gnu__::__nonnull__]]
145 _Stop_cb(__cb_type* __cb)
149 void _M_run()
noexcept { _M_callback(
this); }
154 using value_type = uint32_t;
155 static constexpr value_type _S_stop_requested_bit = 1;
156 static constexpr value_type _S_locked_bit = 2;
157 static constexpr value_type _S_ssrc_counter_inc = 4;
161 _Stop_cb* _M_head =
nullptr;
164 _Stop_state_t() =
default;
167 _M_stop_possible()
noexcept
171 return _M_value.load(memory_order::acquire) & ~_S_locked_bit;
175 _M_stop_requested()
noexcept
177 return _M_value.load(memory_order::acquire) & _S_stop_requested_bit;
181 _M_add_owner()
noexcept
183 _M_owners.fetch_add(1, memory_order::relaxed);
187 _M_release_ownership()
noexcept
189 if (_M_owners.fetch_sub(1, memory_order::acq_rel) == 1)
194 _M_add_ssrc()
noexcept
196 _M_value.fetch_add(_S_ssrc_counter_inc, memory_order::relaxed);
200 _M_sub_ssrc()
noexcept
202 _M_value.fetch_sub(_S_ssrc_counter_inc, memory_order::release);
211 auto __old = _M_value.load(memory_order::relaxed);
212 while (!_M_try_lock(__old, memory_order::relaxed))
220 _M_value.fetch_sub(_S_locked_bit, memory_order::release);
224 _M_request_stop()
noexcept
227 auto __old = _M_value.load(memory_order::acquire);
230 if (__old & _S_stop_requested_bit)
233 while (!_M_try_lock_and_stop(__old));
240 _Stop_cb* __cb = _M_head;
241 _M_head = _M_head->_M_next;
244 _M_head->_M_prev =
nullptr;
253 bool __destroyed =
false;
254 __cb->_M_destroyed = &__destroyed;
261 __cb->_M_destroyed =
nullptr;
264 if (!__gnu_cxx::__is_single_threaded())
265 __cb->_M_done.release();
279 [[__gnu__::__nonnull__]]
281 _M_register_callback(_Stop_cb* __cb)
noexcept
283 auto __old = _M_value.load(memory_order::acquire);
286 if (__old & _S_stop_requested_bit)
292 if (__old < _S_ssrc_counter_inc)
298 while (!_M_try_lock(__old));
300 __cb->_M_next = _M_head;
303 _M_head->_M_prev = __cb;
311 [[__gnu__::__nonnull__]]
313 _M_remove_callback(_Stop_cb* __cb)
319 _M_head = _M_head->_M_next;
321 _M_head->_M_prev =
nullptr;
325 else if (__cb->_M_prev)
327 __cb->_M_prev->_M_next = __cb->_M_next;
329 __cb->_M_next->_M_prev = __cb->_M_prev;
345 __cb->_M_done.acquire();
350 if (__cb->_M_destroyed)
351 *__cb->_M_destroyed =
true;
359 _M_try_lock(value_type& __curval,
360 memory_order __failure = memory_order::acquire)
noexcept
362 return _M_do_try_lock(__curval, 0, memory_order::acquire, __failure);
372 _M_try_lock_and_stop(value_type& __curval)
noexcept
374 return _M_do_try_lock(__curval, _S_stop_requested_bit,
375 memory_order::acq_rel, memory_order::acquire);
379 _M_do_try_lock(value_type& __curval, value_type __newbits,
382 if (__curval & _S_locked_bit)
385 __curval = _M_value.load(__failure);
388 __newbits |= _S_locked_bit;
389 return _M_value.compare_exchange_weak(__curval, __curval | __newbits,
390 __success, __failure);
394 struct _Stop_state_ref
396 _Stop_state_ref() =
default;
400 : _M_ptr(
new _Stop_state_t())
403 _Stop_state_ref(
const _Stop_state_ref& __other) noexcept
404 : _M_ptr(__other._M_ptr)
407 _M_ptr->_M_add_owner();
410 _Stop_state_ref(_Stop_state_ref&& __other) noexcept
411 : _M_ptr(__other._M_ptr)
413 __other._M_ptr =
nullptr;
417 operator=(
const _Stop_state_ref& __other)
noexcept
419 if (
auto __ptr = __other._M_ptr; __ptr != _M_ptr)
422 __ptr->_M_add_owner();
424 _M_ptr->_M_release_ownership();
431 operator=(_Stop_state_ref&& __other)
noexcept
433 _Stop_state_ref(
std::move(__other)).swap(*
this);
440 _M_ptr->_M_release_ownership();
444 swap(_Stop_state_ref& __other)
noexcept
447 explicit operator bool()
const noexcept {
return _M_ptr !=
nullptr; }
449 _Stop_state_t* operator->()
const noexcept {
return _M_ptr; }
451#if __cpp_impl_three_way_comparison >= 201907L
453 operator==(
const _Stop_state_ref&,
const _Stop_state_ref&) =
default;
456 operator==(
const _Stop_state_ref& __lhs,
const _Stop_state_ref& __rhs)
458 {
return __lhs._M_ptr == __rhs._M_ptr; }
461 operator!=(
const _Stop_state_ref& __lhs,
const _Stop_state_ref& __rhs)
463 {
return __lhs._M_ptr != __rhs._M_ptr; }
467 _Stop_state_t* _M_ptr =
nullptr;
470 _Stop_state_ref _M_state;
473 stop_token(
const _Stop_state_ref& __state) noexcept
489 : _M_state(__other._M_state)
492 _M_state->_M_add_ssrc();
500 if (_M_state != __other._M_state)
503 _M_state = __other._M_state;
505 _M_state->_M_add_ssrc();
516 _M_state->_M_sub_ssrc();
521 stop_possible()
const noexcept
523 return static_cast<bool>(_M_state);
528 stop_requested()
const noexcept
530 return static_cast<bool>(_M_state) && _M_state->_M_stop_requested();
534 request_stop()
const noexcept
537 return _M_state->_M_request_stop();
543 get_token()
const noexcept
551 _M_state.swap(__other._M_state);
558 return __a._M_state == __b._M_state;
568 stop_token::_Stop_state_ref _M_state;
572 template<
typename _Callback>
575 static_assert(is_nothrow_destructible_v<_Callback>);
576 static_assert(is_invocable_v<_Callback>);
579 using callback_type = _Callback;
581 template<
typename _Cb,
585 noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
586 : _M_cb(std::forward<_Cb>(__cb))
588 if (
auto __state = __token._M_state)
590 if (__state->_M_register_callback(&_M_cb))
591 _M_state.swap(__state);
595 template<
typename _Cb,
599 noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
600 : _M_cb(std::forward<_Cb>(__cb))
602 if (
auto& __state = __token._M_state)
604 if (__state->_M_register_callback(&_M_cb))
605 _M_state.swap(__state);
613 _M_state->_M_remove_callback(&_M_cb);
623 struct _Cb_impl : stop_token::_Stop_cb
625 template<
typename _Cb>
628 : _Stop_cb(&_S_execute),
629 _M_cb(std::forward<_Cb>(__cb))
634 [[__gnu__::__nonnull__]]
636 _S_execute(_Stop_cb* __that)
noexcept
638 _Callback& __cb =
static_cast<_Cb_impl*
>(__that)->_M_cb;
639 std::forward<_Callback>(__cb)();
644 stop_token::_Stop_state_ref _M_state;
647 template<
typename _Callback>
650_GLIBCXX_END_NAMESPACE_VERSION
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
memory_order
Enumeration for memory_order.
ISO C++ entities toplevel namespace is std.
void yield() noexcept
Allow the implementation to schedule a different thread.
thread::id get_id() noexcept
The unique identifier of the current thread.
Explicit specialization for int.
Tag type indicating a stop_source should have no shared-stop-state.
Allow testing whether a stop request has been made on a stop_source.
A type that allows a stop request to be made.
A wrapper for callbacks to be run when a stop request is made.