|
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 2.0. |
| 3 | +// |
| 4 | +// The APL v2.0: |
| 5 | +// |
| 6 | +//--------------------------------------------------------------------------- |
| 7 | +// Copyright (c) 2007-2024 Broadcom. All Rights Reserved. |
| 8 | +// |
| 9 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +// you may not use this file except in compliance with the License. |
| 11 | +// You may obtain a copy of the License at |
| 12 | +// |
| 13 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +// |
| 15 | +// Unless required by applicable law or agreed to in writing, software |
| 16 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +// See the License for the specific language governing permissions and |
| 19 | +// limitations under the License. |
| 20 | +//--------------------------------------------------------------------------- |
| 21 | +// |
| 22 | +// The MPL v2.0: |
| 23 | +// |
| 24 | +//--------------------------------------------------------------------------- |
| 25 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 26 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 27 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 28 | +// |
| 29 | +// Copyright (c) 2007-2024 Broadcom. All Rights Reserved. |
| 30 | +//--------------------------------------------------------------------------- |
| 31 | + |
| 32 | +using System; |
| 33 | +using System.Diagnostics.CodeAnalysis; |
| 34 | +using System.Threading; |
| 35 | +using System.Threading.Tasks; |
| 36 | +using System.Threading.Tasks.Sources; |
| 37 | + |
| 38 | +namespace RabbitMQ.Client.client.impl |
| 39 | +{ |
| 40 | + sealed class AsyncManualResetEvent : IValueTaskSource |
| 41 | + { |
| 42 | + private ManualResetValueTaskSourceCore<bool> _valueTaskSource; |
| 43 | + private bool _isSet; |
| 44 | + |
| 45 | + public AsyncManualResetEvent(bool initialState = false) |
| 46 | + { |
| 47 | + _isSet = initialState; |
| 48 | + _valueTaskSource.Reset(); |
| 49 | + if (initialState) |
| 50 | + { |
| 51 | + _valueTaskSource.SetResult(true); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public bool IsSet => Volatile.Read(ref _isSet); |
| 56 | + |
| 57 | + public async ValueTask WaitAsync(CancellationToken cancellationToken) |
| 58 | + { |
| 59 | + if (IsSet) |
| 60 | + { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + cancellationToken.ThrowIfCancellationRequested(); |
| 65 | + |
| 66 | + CancellationTokenRegistration tokenRegistration = |
| 67 | +#if NET6_0_OR_GREATER |
| 68 | + cancellationToken.UnsafeRegister( |
| 69 | + static state => |
| 70 | + { |
| 71 | + var (source, token) = ((ManualResetValueTaskSourceCore<bool>, CancellationToken))state!; |
| 72 | + source.SetException(new OperationCanceledException(token)); |
| 73 | + }, (_valueTaskSource, cancellationToken)); |
| 74 | +#else |
| 75 | + cancellationToken.Register( |
| 76 | + static state => |
| 77 | + { |
| 78 | + var (source, token) = ((ManualResetValueTaskSourceCore<bool>, CancellationToken))state!; |
| 79 | + source.SetException(new OperationCanceledException(token)); |
| 80 | + }, |
| 81 | + state: (_valueTaskSource, cancellationToken), useSynchronizationContext: false); |
| 82 | +#endif |
| 83 | + try |
| 84 | + { |
| 85 | + await new ValueTask(this, _valueTaskSource.Version) |
| 86 | + .ConfigureAwait(false); |
| 87 | + } |
| 88 | + finally |
| 89 | + { |
| 90 | +#if NET6_0_OR_GREATER |
| 91 | + await tokenRegistration.DisposeAsync() |
| 92 | + .ConfigureAwait(false); |
| 93 | +#else |
| 94 | + tokenRegistration.Dispose(); |
| 95 | +#endif |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public void Set() |
| 100 | + { |
| 101 | + if (IsSet) |
| 102 | + { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + Volatile.Write(ref _isSet, true); |
| 107 | + _valueTaskSource.SetResult(true); |
| 108 | + } |
| 109 | + |
| 110 | + public void Reset() |
| 111 | + { |
| 112 | + if (!IsSet) |
| 113 | + { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + Volatile.Write(ref _isSet, false); |
| 118 | + _valueTaskSource.Reset(); |
| 119 | + } |
| 120 | + |
| 121 | + void IValueTaskSource.GetResult(short token) |
| 122 | + { |
| 123 | + if (token != _valueTaskSource.Version) |
| 124 | + { |
| 125 | + ThrowIncorrectTokenException(); |
| 126 | + } |
| 127 | + |
| 128 | + _valueTaskSource.GetResult(token); |
| 129 | + } |
| 130 | + |
| 131 | + ValueTaskSourceStatus IValueTaskSource.GetStatus(short token) |
| 132 | + { |
| 133 | + if (token != _valueTaskSource.Version) |
| 134 | + { |
| 135 | + ThrowIncorrectTokenException(); |
| 136 | + } |
| 137 | + |
| 138 | + return _valueTaskSource.GetStatus(token); |
| 139 | + } |
| 140 | + |
| 141 | + void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) |
| 142 | + { |
| 143 | + if (token != _valueTaskSource.Version) |
| 144 | + { |
| 145 | + ThrowIncorrectTokenException(); |
| 146 | + } |
| 147 | + |
| 148 | + _valueTaskSource.OnCompleted(continuation, state, token, flags); |
| 149 | + } |
| 150 | + |
| 151 | + [DoesNotReturn] |
| 152 | + static void ThrowIncorrectTokenException() => |
| 153 | + throw new InvalidOperationException("ValueTask cannot be awaited multiple times."); |
| 154 | + } |
| 155 | +} |
0 commit comments