//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
namespace slskd
{
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
///
/// Enables arbitrary await-able things.
///
public interface IWaiter : IDisposable
{
///
/// Gets the default timeout duration.
///
int DefaultTimeout { get; }
///
/// Cancels the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
void Cancel(WaitKey key);
///
/// Cancels all waits.
///
void CancelAll();
///
/// Completes the oldest wait matching the specified with the specified .
///
/// The wait result type.
/// The unique WaitKey for the wait.
/// The wait result.
void Complete(WaitKey key, T result);
///
/// Completes the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
void Complete(WaitKey key);
///
/// Returns a value indicating whether the specified is being waited upon.
///
/// The unique WaitKey to check.
/// A value indicating whether the specified key is being waited upon.
bool IsWaitingFor(WaitKey key);
///
/// Throws the specified on the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
/// The Exception to throw.
void Throw(WaitKey key, Exception exception);
///
/// Causes the oldest wait matching the specified to time out.
///
/// The unique WaitKey for the wait.
void Timeout(WaitKey key);
///
/// Adds a new wait for the specified and with the specified .
///
/// The wait result type.
/// A unique WaitKey for the wait.
/// The wait timeout.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task Wait(WaitKey key, int? timeout = null, CancellationToken? cancellationToken = null);
///
/// Adds a new wait for the specified and with the specified .
///
/// A unique WaitKey for the wait.
/// The wait timeout.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task Wait(WaitKey key, int? timeout = null, CancellationToken? cancellationToken = null);
///
/// Adds a new wait for the specified which does not time out.
///
/// The wait result type.
/// A unique WaitKey for the wait.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task WaitIndefinitely(WaitKey key, CancellationToken? cancellationToken = null);
///
/// Adds a new wait for the specified which does not time out.
///
/// A unique WaitKey for the wait.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task WaitIndefinitely(WaitKey key, CancellationToken? cancellationToken = null);
}
///
/// Enables arbitrary await-able things.
///
public sealed class Waiter : IWaiter
{
private const int DefaultTimeoutValue = 5000;
///
/// Initializes a new instance of the class with the default timeout.
///
public Waiter()
: this(DefaultTimeoutValue)
{
}
///
/// Initializes a new instance of the class with the specified .
///
/// The default timeout duration for message waits.
public Waiter(int defaultTimeout)
{
DefaultTimeout = defaultTimeout;
}
///
/// Gets the default timeout duration, in milliseconds.
///
public int DefaultTimeout { get; private set; }
private bool Disposed { get; set; }
private ConcurrentDictionary Locks { get; } = new ConcurrentDictionary();
private ConcurrentDictionary> Waits { get; } = new ConcurrentDictionary>();
///
/// Cancels the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
public void Cancel(WaitKey key)
{
Disposition(key, wait =>
wait.TaskCompletionSource.TrySetCanceled());
}
///
/// Cancels all waits.
///
public void CancelAll()
{
var keys = Waits.Keys.ToList();
foreach (var key in keys)
{
Cancel(key);
}
}
///
/// Completes the oldest wait matching the specified with the specified .
///
/// The wait result type.
/// The unique WaitKey for the wait.
/// The wait result.
public void Complete(WaitKey key, T result)
{
Disposition(key, wait =>
((TaskCompletionSource)wait.TaskCompletionSource).TrySetResult(result));
}
///
/// Completes the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
public void Complete(WaitKey key)
{
Complete