#ifndef _LIBCPP___STOP_TOKEN_STOP_STATE_H
#define _LIBCPP___STOP_TOKEN_STOP_STATE_H
#include <__assert>
#include <__config>
#include <__stop_token/atomic_unique_lock.h>
#include <__stop_token/intrusive_list_view.h>
#include <__thread/id.h>
#include <atomic>
#include <cstdint>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_THREADS)
struct __stop_callback_base : __intrusive_node_base<__stop_callback_base> {
using __callback_fn_t = void(__stop_callback_base*) noexcept;
_LIBCPP_HIDE_FROM_ABI explicit __stop_callback_base(__callback_fn_t* __callback_fn) : __callback_fn_(__callback_fn) {}
_LIBCPP_HIDE_FROM_ABI void __invoke() noexcept { __callback_fn_(this); }
__callback_fn_t* __callback_fn_;
atomic<bool> __completed_ = false;
bool* __destroyed_ = nullptr;
};
class __stop_state {
static constexpr uint32_t __stop_requested_bit = 1;
static constexpr uint32_t __callback_list_locked_bit = 1 << 1;
static constexpr uint32_t __stop_source_counter_shift = 2;
atomic<uint32_t> __state_ = 0;
atomic<uint32_t> __ref_count_ = 0;
using __state_t = uint32_t;
using __callback_list_lock = __atomic_unique_lock<__state_t, __callback_list_locked_bit>;
using __callback_list = __intrusive_list_view<__stop_callback_base>;
__callback_list __callback_list_;
__thread_id __requesting_thread_;
public:
_LIBCPP_HIDE_FROM_ABI __stop_state() noexcept = default;
_LIBCPP_HIDE_FROM_ABI void __increment_stop_source_counter() noexcept {
_LIBCPP_ASSERT_UNCATEGORIZED(
__state_.load(std::memory_order_relaxed) <= static_cast<__state_t>(~(1 << __stop_source_counter_shift)),
"stop_source's counter reaches the maximum. Incrementing the counter will overflow");
__state_.fetch_add(1 << __stop_source_counter_shift, std::memory_order_relaxed);
}
_LIBCPP_HIDE_FROM_ABI void __decrement_stop_source_counter() noexcept {
_LIBCPP_ASSERT_UNCATEGORIZED(
__state_.load(std::memory_order_relaxed) >= static_cast<__state_t>(1 << __stop_source_counter_shift),
"stop_source's counter is 0. Decrementing the counter will underflow");
__state_.fetch_sub(1 << __stop_source_counter_shift, std::memory_order_relaxed);
}
_LIBCPP_HIDE_FROM_ABI bool __stop_requested() const noexcept {
return (__state_.load(std::memory_order_acquire) & __stop_requested_bit) != 0;
}
_LIBCPP_HIDE_FROM_ABI bool __stop_possible_for_stop_token() const noexcept {
__state_t __curent_state = __state_.load(std::memory_order_acquire);
return ((__curent_state & __stop_requested_bit) != 0) || ((__curent_state >> __stop_source_counter_shift) != 0);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __request_stop() noexcept {
auto __cb_list_lock = __try_lock_for_request_stop();
if (!__cb_list_lock.__owns_lock()) {
return false;
}
__requesting_thread_ = this_thread::get_id();
while (!__callback_list_.__empty()) {
auto __cb = __callback_list_.__pop_front();
__cb_list_lock.__unlock();
bool __destroyed = false;
__cb->__destroyed_ = &__destroyed;
__cb->__invoke();
if (!__destroyed) {
__cb->__destroyed_ = nullptr;
__cb->__completed_.store(true, std::memory_order_release);
__cb->__completed_.notify_all();
}
__cb_list_lock.__lock();
}
return true;
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __add_callback(__stop_callback_base* __cb) noexcept {
const auto __give_up_trying_to_lock_condition = [__cb](__state_t __state) {
if ((__state & __stop_requested_bit) != 0) {
__cb->__invoke();
return true;
}
return (__state >> __stop_source_counter_shift) == 0;
};
__callback_list_lock __cb_list_lock(__state_, __give_up_trying_to_lock_condition);
if (!__cb_list_lock.__owns_lock()) {
return false;
}
__callback_list_.__push_front(__cb);
return true;
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __remove_callback(__stop_callback_base* __cb) noexcept {
__callback_list_lock __cb_list_lock(__state_);
bool __potentially_executing_now = __cb->__prev_ == nullptr && !__callback_list_.__is_head(__cb);
if (__potentially_executing_now) {
auto __requested_thread = __requesting_thread_;
__cb_list_lock.__unlock();
if (std::this_thread::get_id() != __requested_thread) {
__cb->__completed_.wait(false, std::memory_order_acquire);
} else {
if (__cb->__destroyed_) {
*__cb->__destroyed_ = true;
}
}
} else {
__callback_list_.__remove(__cb);
}
}
private:
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __callback_list_lock __try_lock_for_request_stop() noexcept {
const auto __lock_fail_condition = [](__state_t __state) { return (__state & __stop_requested_bit) != 0; };
const auto __after_lock_state = [](__state_t __state) {
return __state | __callback_list_locked_bit | __stop_requested_bit;
};
const auto __locked_ordering = std::memory_order_acq_rel;
return __callback_list_lock(__state_, __lock_fail_condition, __after_lock_state, __locked_ordering);
}
template <class _Tp>
friend struct __intrusive_shared_ptr_traits;
};
template <class _Tp>
struct __intrusive_shared_ptr_traits;
template <>
struct __intrusive_shared_ptr_traits<__stop_state> {
_LIBCPP_HIDE_FROM_ABI static atomic<uint32_t>& __get_atomic_ref_count(__stop_state& __state) {
return __state.__ref_count_;
}
};
#endif
_LIBCPP_END_NAMESPACE_STD
#endif