|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * Copyright (c) 2025 Robert Leahy. All rights reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 with LLVM Exceptions (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://llvm.org/LICENSE.txt |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include "../stdexec/execution.hpp" |
| 22 | + |
| 23 | +namespace exec { |
| 24 | + namespace __unless_stop_requested { |
| 25 | + using namespace stdexec; |
| 26 | + |
| 27 | + template <typename _Env> |
| 28 | + inline constexpr bool __unstoppable_env = unstoppable_token<stop_token_of_t<_Env>>; |
| 29 | + |
| 30 | + template <typename _Receiver> |
| 31 | + inline constexpr bool __unstoppable_receiver = __unstoppable_env<env_of_t<_Receiver>>; |
| 32 | + |
| 33 | + template <class _Sender, class _Env> |
| 34 | + using __completions = transform_completion_signatures< |
| 35 | + __completion_signatures_of_t<_Sender, _Env>, |
| 36 | + std::conditional_t< |
| 37 | + __unstoppable_env<_Env>, |
| 38 | + completion_signatures<>, |
| 39 | + completion_signatures<set_stopped_t()>>>; |
| 40 | + |
| 41 | + struct __connect_fn { |
| 42 | + template <class _Sender, class _Receiver> |
| 43 | + requires __unstoppable_receiver<_Receiver> |
| 44 | + constexpr connect_result_t<__child_of<_Sender>, _Receiver> |
| 45 | + operator()(_Sender&& __sndr, _Receiver __rcvr) const noexcept( |
| 46 | + noexcept(stdexec::connect(__declval<__child_of<_Sender>>(), (_Receiver&&) __rcvr))) { |
| 47 | + return __sexpr_apply((_Sender&&) __sndr, [&](auto, const auto&, auto&& __child) { |
| 48 | + return stdexec::connect((decltype(__child)&&) __child, (_Receiver&&) __rcvr); |
| 49 | + }); |
| 50 | + } |
| 51 | + template <class _Sender, class _Receiver> |
| 52 | + constexpr __op_state<_Sender, _Receiver> operator()(_Sender&& __sndr, _Receiver __rcvr) const |
| 53 | + noexcept(__nothrow_constructible_from<__op_state<_Sender, _Receiver>, _Sender, _Receiver>) { |
| 54 | + return __op_state<_Sender, _Receiver>{(_Sender&&) __sndr, (_Receiver&&) __rcvr}; |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + struct unless_stop_requested_t : sender_adaptor_closure<unless_stop_requested_t> { |
| 59 | + constexpr auto operator()() const noexcept { |
| 60 | + return *this; |
| 61 | + } |
| 62 | + template <sender _Sender> |
| 63 | + constexpr __well_formed_sender auto operator()(_Sender&& __sndr) const { |
| 64 | + auto __domain = __get_early_domain(__sndr); |
| 65 | + return stdexec::transform_sender( |
| 66 | + __domain, __make_sexpr<unless_stop_requested_t>(__(), static_cast<_Sender&&>(__sndr))); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + struct __unless_stop_requested_impl : __sexpr_defaults { |
| 71 | + static constexpr auto get_completion_signatures = |
| 72 | + []<class _Self, class _Env>(_Self&&, _Env&&) noexcept |
| 73 | + -> __completions<__child_of<_Self>, _Env> { |
| 74 | + static_assert(sender_expr_for<_Self, unless_stop_requested_t>); |
| 75 | + return {}; |
| 76 | + }; |
| 77 | + |
| 78 | + static constexpr auto start = []<class _State, class _Receiver, class _Operation>( |
| 79 | + _State&, |
| 80 | + _Receiver& __rcvr, |
| 81 | + _Operation& __child_op) noexcept -> void { |
| 82 | + static_assert(!__unstoppable_receiver<_Receiver>); |
| 83 | + if (get_stop_token(stdexec::get_env(__rcvr)).stop_requested()) { |
| 84 | + stdexec::set_stopped((_Receiver&&) __rcvr); |
| 85 | + return; |
| 86 | + } |
| 87 | + stdexec::start(__child_op); |
| 88 | + }; |
| 89 | + |
| 90 | + static constexpr __connect_fn connect{}; |
| 91 | + }; |
| 92 | + } // namespace __unless_stop_requested |
| 93 | + |
| 94 | + using __unless_stop_requested::unless_stop_requested_t; |
| 95 | + inline constexpr __unless_stop_requested::unless_stop_requested_t unless_stop_requested{}; |
| 96 | +} // namespace exec |
| 97 | + |
| 98 | +namespace stdexec { |
| 99 | + template <> |
| 100 | + struct __sexpr_impl<::exec::unless_stop_requested_t> |
| 101 | + : ::exec::__unless_stop_requested::__unless_stop_requested_impl { }; |
| 102 | +} // namespace stdexec |
0 commit comments