29 #ifndef _GLIBCXX_STOP_TOKEN
30 #define _GLIBCXX_STOP_TOKEN
32 #if __cplusplus > 201703L
39 #define __cpp_lib_jthread 201911L
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 struct nostopstate_t {
explicit nostopstate_t() =
default; };
47 inline constexpr nostopstate_t nostopstate{};
55 stop_token() noexcept = default;
57 stop_token(const stop_token&) noexcept = default;
58 stop_token(stop_token&&) noexcept = default;
60 ~stop_token() = default;
63 operator=(const stop_token&) noexcept = default;
66 operator=(stop_token&&) noexcept = default;
70 stop_possible() const noexcept
72 return static_cast<bool>(_M_state) && _M_state->_M_stop_possible();
77 stop_requested() const noexcept
79 return static_cast<bool>(_M_state) && _M_state->_M_stop_requested();
83 swap(stop_token& __rhs) noexcept
84 { _M_state.swap(__rhs._M_state); }
88 operator==(
const stop_token& __a,
const stop_token& __b)
89 {
return __a._M_state == __b._M_state; }
92 swap(stop_token& __lhs, stop_token& __rhs) noexcept
93 { __lhs.swap(__rhs); }
96 friend class stop_source;
97 template<
typename _Callback>
98 friend class stop_callback;
103 #if defined __i386__ || defined __x86_64__
104 __builtin_ia32_pause();
109 #ifndef __cpp_lib_semaphore
111 struct binary_semaphore
113 explicit binary_semaphore(
int __d) : _M_counter(__d > 0) { }
120 while (!_M_counter.compare_exchange_weak(__old, 0,
121 memory_order::acquire,
122 memory_order::relaxed))
129 atomic<int> _M_counter;
135 using __cb_type = void(_Stop_cb*) noexcept;
136 __cb_type* _M_callback;
137 _Stop_cb* _M_prev =
nullptr;
138 _Stop_cb* _M_next =
nullptr;
139 bool* _M_destroyed =
nullptr;
140 binary_semaphore _M_done{0};
142 [[__gnu__::__nonnull__]]
144 _Stop_cb(__cb_type* __cb)
148 void _M_run() noexcept { _M_callback(
this); }
153 using value_type = uint32_t;
154 static constexpr value_type _S_stop_requested_bit = 1;
155 static constexpr value_type _S_locked_bit = 2;
156 static constexpr value_type _S_ssrc_counter_inc = 4;
160 _Stop_cb* _M_head =
nullptr;
163 _Stop_state_t() =
default;
166 _M_stop_possible() noexcept
170 return _M_value.load(memory_order::acquire) & ~_S_locked_bit;
174 _M_stop_requested() noexcept
176 return _M_value.load(memory_order::acquire) & _S_stop_requested_bit;
180 _M_add_owner() noexcept
182 _M_owners.fetch_add(1, memory_order::relaxed);
186 _M_release_ownership() noexcept
193 _M_add_ssrc() noexcept
195 _M_value.fetch_add(_S_ssrc_counter_inc, memory_order::relaxed);
199 _M_sub_ssrc() noexcept
210 auto __old = _M_value.load(memory_order::relaxed);
211 while (!_M_try_lock(__old, memory_order::relaxed))
223 _M_request_stop() noexcept
226 auto __old = _M_value.load(memory_order::acquire);
229 if (__old & _S_stop_requested_bit)
232 while (!_M_try_lock_and_stop(__old));
239 _Stop_cb* __cb = _M_head;
240 _M_head = _M_head->_M_next;
243 _M_head->_M_prev =
nullptr;
252 bool __destroyed =
false;
253 __cb->_M_destroyed = &__destroyed;
260 __cb->_M_destroyed =
nullptr;
263 if (!__gnu_cxx::__is_single_threaded())
264 __cb->_M_done.release();
278 [[__gnu__::__nonnull__]]
280 _M_register_callback(_Stop_cb* __cb) noexcept
282 auto __old = _M_value.load(memory_order::acquire);
285 if (__old & _S_stop_requested_bit)
291 if (__old < _S_ssrc_counter_inc)
297 while (!_M_try_lock(__old));
299 __cb->_M_next = _M_head;
302 _M_head->_M_prev = __cb;
310 [[__gnu__::__nonnull__]]
312 _M_remove_callback(_Stop_cb* __cb)
318 _M_head = _M_head->_M_next;
320 _M_head->_M_prev =
nullptr;
324 else if (__cb->_M_prev)
326 __cb->_M_prev->_M_next = __cb->_M_next;
328 __cb->_M_next->_M_prev = __cb->_M_prev;
344 __cb->_M_done.acquire();
349 if (__cb->_M_destroyed)
350 *__cb->_M_destroyed =
true;
358 _M_try_lock(value_type& __curval,
359 memory_order __failure = memory_order::acquire) noexcept
361 return _M_do_try_lock(__curval, 0, memory_order::acquire, __failure);
371 _M_try_lock_and_stop(value_type& __curval) noexcept
373 return _M_do_try_lock(__curval, _S_stop_requested_bit,
374 memory_order::acq_rel, memory_order::acquire);
378 _M_do_try_lock(value_type& __curval, value_type __newbits,
381 if (__curval & _S_locked_bit)
384 __curval = _M_value.load(__failure);
387 __newbits |= _S_locked_bit;
388 return _M_value.compare_exchange_weak(__curval, __curval | __newbits,
389 __success, __failure);
393 struct _Stop_state_ref
395 _Stop_state_ref() =
default;
398 _Stop_state_ref(
const stop_source&)
399 : _M_ptr(new _Stop_state_t())
402 _Stop_state_ref(
const _Stop_state_ref& __other) noexcept
403 : _M_ptr(__other._M_ptr)
406 _M_ptr->_M_add_owner();
409 _Stop_state_ref(_Stop_state_ref&& __other) noexcept
410 : _M_ptr(__other._M_ptr)
412 __other._M_ptr =
nullptr;
416 operator=(
const _Stop_state_ref& __other) noexcept
418 if (
auto __ptr = __other._M_ptr; __ptr != _M_ptr)
421 __ptr->_M_add_owner();
423 _M_ptr->_M_release_ownership();
430 operator=(_Stop_state_ref&& __other) noexcept
432 _Stop_state_ref(
std::move(__other)).swap(*
this);
439 _M_ptr->_M_release_ownership();
443 swap(_Stop_state_ref& __other) noexcept
446 explicit operator bool() const noexcept {
return _M_ptr !=
nullptr; }
448 _Stop_state_t*
operator->() const noexcept {
return _M_ptr; }
450 #if __cpp_impl_three_way_comparison >= 201907L
452 operator==(
const _Stop_state_ref&,
const _Stop_state_ref&) =
default;
455 operator==(
const _Stop_state_ref& __lhs,
const _Stop_state_ref& __rhs)
457 {
return __lhs._M_ptr == __rhs._M_ptr; }
460 operator!=(
const _Stop_state_ref& __lhs,
const _Stop_state_ref& __rhs)
462 {
return __lhs._M_ptr != __rhs._M_ptr; }
466 _Stop_state_t* _M_ptr =
nullptr;
469 _Stop_state_ref _M_state;
472 stop_token(
const _Stop_state_ref& __state) noexcept
481 stop_source() : _M_state(*this)
484 explicit stop_source(std::nostopstate_t) noexcept
487 stop_source(
const stop_source& __other) noexcept
488 : _M_state(__other._M_state)
491 _M_state->_M_add_ssrc();
494 stop_source(stop_source&&) noexcept = default;
497 operator=(const stop_source& __other) noexcept
499 if (_M_state != __other._M_state)
502 _M_state = __other._M_state;
504 _M_state->_M_add_ssrc();
510 operator=(stop_source&&) noexcept = default;
515 _M_state->_M_sub_ssrc();
520 stop_possible() const noexcept
522 return static_cast<bool>(_M_state);
527 stop_requested() const noexcept
529 return static_cast<bool>(_M_state) && _M_state->_M_stop_requested();
533 request_stop() const noexcept
536 return _M_state->_M_request_stop();
542 get_token() const noexcept
544 return stop_token{_M_state};
548 swap(stop_source& __other) noexcept
550 _M_state.swap(__other._M_state);
555 operator==(
const stop_source& __a,
const stop_source& __b) noexcept
557 return __a._M_state == __b._M_state;
561 swap(stop_source& __lhs, stop_source& __rhs) noexcept
567 stop_token::_Stop_state_ref _M_state;
571 template<
typename _Callback>
572 class [[nodiscard]] stop_callback
574 static_assert(is_nothrow_destructible_v<_Callback>);
575 static_assert(is_invocable_v<_Callback>);
578 using callback_type = _Callback;
580 template<
typename _Cb,
581 enable_if_t<is_constructible_v<_Callback, _Cb>,
int> = 0>
583 stop_callback(
const stop_token& __token, _Cb&& __cb)
584 noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
587 if (
auto __state = __token._M_state)
589 if (__state->_M_register_callback(&_M_cb))
590 _M_state.swap(__state);
594 template<
typename _Cb,
595 enable_if_t<is_constructible_v<_Callback, _Cb>,
int> = 0>
597 stop_callback(stop_token&& __token, _Cb&& __cb)
598 noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
601 if (
auto& __state = __token._M_state)
603 if (__state->_M_register_callback(&_M_cb))
604 _M_state.swap(__state);
612 _M_state->_M_remove_callback(&_M_cb);
616 stop_callback(
const stop_callback&) =
delete;
617 stop_callback&
operator=(
const stop_callback&) =
delete;
618 stop_callback(stop_callback&&) =
delete;
619 stop_callback&
operator=(stop_callback&&) =
delete;
622 struct _Cb_impl : stop_token::_Stop_cb
624 template<
typename _Cb>
627 : _Stop_cb(&_S_execute),
633 [[__gnu__::__nonnull__]]
635 _S_execute(_Stop_cb* __that) noexcept
637 _Callback& __cb =
static_cast<_Cb_impl*
>(__that)->_M_cb;
638 std::forward<_Callback>(__cb)();
643 stop_token::_Stop_state_ref _M_state;
646 template<
typename _Callback>
647 stop_callback(stop_token, _Callback) -> stop_callback<_Callback>;
649 _GLIBCXX_END_NAMESPACE_VERSION
auto_ptr & operator=(auto_ptr &__a)
auto_ptr assignment operator.
element_type * operator->() const
Smart pointer dereferencing.
element_type * release()
Bypassing the smart pointer.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
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
this_thread::yield
thread::id get_id() noexcept
this_thread::get_id
Generic atomic type, primary class template.