|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 NVIDIA Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 with LLVM Exceptions |
| 5 | + * (the "License"); you may not use this file except in compliance with |
| 6 | + * the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://llvm.org/LICENSE.txt |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include "stdexec/execution.hpp" |
| 19 | + |
| 20 | +namespace repeat_receiver_lifetime_test |
| 21 | +{ |
| 22 | + namespace ex = STDEXEC; |
| 23 | + |
| 24 | + template <class Completion> |
| 25 | + struct invalidate_on_destroy_sender |
| 26 | + { |
| 27 | + using sender_concept = ex::sender_tag; |
| 28 | + using completion_signatures = ex::completion_signatures<typename Completion::signature>; |
| 29 | + |
| 30 | + template <class Receiver> |
| 31 | + struct operation |
| 32 | + { |
| 33 | + operation(Receiver rcvr, Completion completion, bool *invalidated) noexcept |
| 34 | + : rcvr_(static_cast<Receiver &&>(rcvr)) |
| 35 | + , completion_(static_cast<Completion &&>(completion)) |
| 36 | + , invalidated_(invalidated) |
| 37 | + {} |
| 38 | + |
| 39 | + ~operation() |
| 40 | + { |
| 41 | + if constexpr (requires { rcvr_.__self_->__rcvr_.__state_; }) |
| 42 | + { |
| 43 | + if (started_) |
| 44 | + { |
| 45 | + rcvr_.__self_->__rcvr_.__state_ = nullptr; |
| 46 | + *invalidated_ = true; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + void start() & noexcept |
| 52 | + { |
| 53 | + started_ = true; |
| 54 | + completion_(static_cast<Receiver &&>(rcvr_)); |
| 55 | + } |
| 56 | + |
| 57 | + Receiver rcvr_; |
| 58 | + Completion completion_; |
| 59 | + bool *invalidated_; |
| 60 | + bool started_ = false; |
| 61 | + }; |
| 62 | + |
| 63 | + template <ex::receiver_of<completion_signatures> Receiver> |
| 64 | + auto connect(Receiver rcvr) const -> operation<Receiver> |
| 65 | + { |
| 66 | + return {static_cast<Receiver &&>(rcvr), completion_, invalidated_}; |
| 67 | + } |
| 68 | + |
| 69 | + Completion completion_; |
| 70 | + bool *invalidated_; |
| 71 | + }; |
| 72 | + |
| 73 | + template <class Completion> |
| 74 | + STDEXEC_HOST_DEVICE_DEDUCTION_GUIDE |
| 75 | + invalidate_on_destroy_sender(Completion, bool *) -> invalidate_on_destroy_sender<Completion>; |
| 76 | +} // namespace repeat_receiver_lifetime_test |
0 commit comments