Add WaitHandleAsyncFactory
This commit is contained in:
124
LibreMetaverse/Threading/WaitHandleAsyncFactory.cs
Normal file
124
LibreMetaverse/Threading/WaitHandleAsyncFactory.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Stephen Cleary
|
||||
* Copyright (c) 2025, Sjofn LLC.
|
||||
* All rights reserved.
|
||||
*
|
||||
* - Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* - Neither the name of the openmetaverse.co nor the names
|
||||
* of its contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
|
||||
namespace LibreMetaverse.Threading
|
||||
{
|
||||
public static class WaitHandleAsyncFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps a <see cref="WaitHandle"/> with a <see cref="Task"/>.
|
||||
/// When the <see cref="WaitHandle"/> is signalled, the returned <see cref="Task"/> is completed.
|
||||
/// If the handle is already signalled, this method acts synchronously.
|
||||
/// </summary>
|
||||
/// <param name="handle">The <see cref="WaitHandle"/> to observe.</param>
|
||||
public static Task FromWaitHandle(WaitHandle handle)
|
||||
{
|
||||
return FromWaitHandle(handle, Timeout.InfiniteTimeSpan, CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a <see cref="WaitHandle"/> with a <see cref="Task{Boolean}"/>.
|
||||
/// If the <see cref="WaitHandle"/> is signalled, the returned task is completed with a <c>true</c> result.
|
||||
/// If the observation times out, the returned task is completed with a <c>false</c> result.
|
||||
/// If the handle is already signalled or the timeout is zero, this method acts synchronously.
|
||||
/// </summary>
|
||||
/// <param name="handle">The <see cref="WaitHandle"/> to observe.</param>
|
||||
/// <param name="timeout">The timeout after which the <see cref="WaitHandle"/> is no longer observed.</param>
|
||||
public static Task<bool> FromWaitHandle(WaitHandle handle, TimeSpan timeout)
|
||||
{
|
||||
return FromWaitHandle(handle, timeout, CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a <see cref="WaitHandle"/> with a <see cref="Task{Boolean}"/>.
|
||||
/// If the <see cref="WaitHandle"/> is signalled, the returned task is (successfully) completed.
|
||||
/// If the observation is cancelled, the returned task is cancelled.
|
||||
/// If the handle is already signalled or the cancellation token is already cancelled, this method acts synchronously.
|
||||
/// </summary>
|
||||
/// <param name="handle">The <see cref="WaitHandle"/> to observe.</param>
|
||||
/// <param name="token">The cancellation token that cancels observing the <see cref="WaitHandle"/>.</param>
|
||||
public static Task FromWaitHandle(WaitHandle handle, CancellationToken token)
|
||||
{
|
||||
return FromWaitHandle(handle, Timeout.InfiniteTimeSpan, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a <see cref="WaitHandle"/> with a <see cref="Task{Boolean}"/>.
|
||||
/// If the <see cref="WaitHandle"/> is signalled, the returned task is completed with a <c>true</c> result.
|
||||
/// If the observation times out, the returned task is completed with a <c>false</c> result.
|
||||
/// If the observation is cancelled, the returned task is cancelled.
|
||||
/// If the handle has already signalled, the timeout is zero,
|
||||
/// or the cancellation token has already cancelled, then this method acts synchronously.
|
||||
/// </summary>
|
||||
/// <param name="handle">The <see cref="WaitHandle"/> to observe.</param>
|
||||
/// <param name="timeout">The timeout after which the <see cref="WaitHandle"/> is no longer observed.</param>
|
||||
/// <param name="token">The cancellation token that cancels observing the <see cref="WaitHandle"/>.</param>
|
||||
public static Task<bool> FromWaitHandle(WaitHandle handle, TimeSpan timeout, CancellationToken token)
|
||||
{
|
||||
_ = handle ?? throw new ArgumentNullException(nameof(handle));
|
||||
|
||||
// Handle synchronous cases.
|
||||
var alreadySignalled = handle.WaitOne(0);
|
||||
if (alreadySignalled)
|
||||
return Task.FromResult(true);
|
||||
if (timeout == TimeSpan.Zero)
|
||||
return Task.FromResult(false);
|
||||
if (token.IsCancellationRequested)
|
||||
return Task.FromCanceled<bool>(new CancellationToken(true));
|
||||
|
||||
// Register all asynchronous cases.
|
||||
return DoFromWaitHandle(handle, timeout, token);
|
||||
}
|
||||
|
||||
private static async Task<bool> DoFromWaitHandle(WaitHandle handle, TimeSpan timeout, CancellationToken token)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
using (new ThreadPoolRegistration(handle, timeout, tcs))
|
||||
using (token.Register(state => ((TaskCompletionSource<bool>)state).TrySetCanceled(),
|
||||
tcs, useSynchronizationContext: false))
|
||||
return await tcs.Task.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private sealed class ThreadPoolRegistration : IDisposable
|
||||
{
|
||||
private readonly RegisteredWaitHandle _registeredWaitHandle;
|
||||
|
||||
public ThreadPoolRegistration(WaitHandle handle, TimeSpan timeout, TaskCompletionSource<bool> tcs)
|
||||
{
|
||||
_registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(handle,
|
||||
(state, timedOut) => ((TaskCompletionSource<bool>)state).TrySetResult(!timedOut), tcs,
|
||||
timeout, executeOnlyOnce: true);
|
||||
}
|
||||
|
||||
void IDisposable.Dispose() => _registeredWaitHandle.Unregister(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user