Path: blob/main/contrib/llvm-project/libcxx/include/__stop_token/stop_callback.h
35236 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___STOP_TOKEN_STOP_CALLBACK_H10#define _LIBCPP___STOP_TOKEN_STOP_CALLBACK_H1112#include <__concepts/constructible.h>13#include <__concepts/destructible.h>14#include <__concepts/invocable.h>15#include <__config>16#include <__stop_token/intrusive_shared_ptr.h>17#include <__stop_token/stop_state.h>18#include <__stop_token/stop_token.h>19#include <__type_traits/is_nothrow_constructible.h>20#include <__utility/forward.h>21#include <__utility/move.h>22#include <__utility/private_constructor_tag.h>2324#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)25# pragma GCC system_header26#endif2728_LIBCPP_PUSH_MACROS29#include <__undef_macros>3031_LIBCPP_BEGIN_NAMESPACE_STD3233#if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)3435template <class _Callback>36class _LIBCPP_AVAILABILITY_SYNC stop_callback : private __stop_callback_base {37static_assert(invocable<_Callback>,38"Mandates: stop_callback is instantiated with an argument for the template parameter Callback that "39"satisfies invocable.");40static_assert(destructible<_Callback>,41"Mandates: stop_callback is instantiated with an argument for the template parameter Callback that "42"satisfies destructible.");4344public:45using callback_type = _Callback;4647template <class _Cb>48requires constructible_from<_Callback, _Cb>49_LIBCPP_HIDE_FROM_ABI explicit stop_callback(const stop_token& __st,50_Cb&& __cb) noexcept(is_nothrow_constructible_v<_Callback, _Cb>)51: stop_callback(__private_constructor_tag{}, __st.__state_, std::forward<_Cb>(__cb)) {}5253template <class _Cb>54requires constructible_from<_Callback, _Cb>55_LIBCPP_HIDE_FROM_ABI explicit stop_callback(stop_token&& __st,56_Cb&& __cb) noexcept(is_nothrow_constructible_v<_Callback, _Cb>)57: stop_callback(__private_constructor_tag{}, std::move(__st.__state_), std::forward<_Cb>(__cb)) {}5859_LIBCPP_HIDE_FROM_ABI ~stop_callback() {60if (__state_) {61__state_->__remove_callback(this);62}63}6465stop_callback(const stop_callback&) = delete;66stop_callback(stop_callback&&) = delete;67stop_callback& operator=(const stop_callback&) = delete;68stop_callback& operator=(stop_callback&&) = delete;6970private:71_LIBCPP_NO_UNIQUE_ADDRESS _Callback __callback_;72__intrusive_shared_ptr<__stop_state> __state_;7374friend __stop_callback_base;7576template <class _StatePtr, class _Cb>77_LIBCPP_HIDE_FROM_ABI explicit stop_callback(__private_constructor_tag, _StatePtr&& __state, _Cb&& __cb) noexcept(78is_nothrow_constructible_v<_Callback, _Cb>)79: __stop_callback_base([](__stop_callback_base* __cb_base) noexcept {80// stop callback is supposed to only be called once81std::forward<_Callback>(static_cast<stop_callback*>(__cb_base)->__callback_)();82}),83__callback_(std::forward<_Cb>(__cb)),84__state_() {85if (__state && __state->__add_callback(this)) {86// st.stop_requested() was false and this is successfully added to the linked list87__state_ = std::forward<_StatePtr>(__state);88}89}90};9192template <class _Callback>93_LIBCPP_AVAILABILITY_SYNC stop_callback(stop_token, _Callback) -> stop_callback<_Callback>;9495#endif // _LIBCPP_STD_VER >= 209697_LIBCPP_END_NAMESPACE_STD9899_LIBCPP_POP_MACROS100101#endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)102103104