forked from Mirror/Ryujinx
eb528ae0f0
* Add workflow to perform automated checks for PRs * Downgrade Microsoft.CodeAnalysis to 4.4.0 This is a workaround to fix issues with dotnet-format. See: - https://github.com/dotnet/format/issues/1805 - https://github.com/dotnet/format/issues/1800 * Adjust editorconfig to be more compatible with Ryujinx code-style * Adjust .editorconfig line endings to match .gitattributes * Disable 'prefer switch expression' rule * Remove naming styles These are the default rules, so we don't need to override them. * Silence IDE0060 in .editorconfig * Slightly adjust .editorconfig * Add lost workflow changes * Move .editorconfig comment to the top * .editorconfig: private static readonly fields should be _lowerCamelCase * .editorconfig: Remove alignment for declarations as well * editorconfig: Add rule for local constants * Disable CA1822 for HLE services * Disable CA1822 for ViewModels Bindings won't work with static members, but this issue is silently ignored. * Run dotnet format for the whole solution * Check result code of SDL_GetDisplayBounds * Fix dotnet format style issues * Add missing trailing commas * Update Microsoft.CodeAnalysis.CSharp to 4.6.0 Skipping 4.5.0 since it breaks dotnet format * Restore old default naming rules for dotnet format * Add naming rule exception for CPU tests * checks: Include all files before excluding paths * Fix dotnet format issues * Check dotnet format version * checks: Run dotnet format with severity info again * checks: Disable naming style rules until they won't crash the process anymore * Remove unread private member * checks: Attempt to run analyzers 3 times before giving up * checks: Enable naming style rules again with the new retry logic
102 lines
2.6 KiB
C#
102 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
|
|
namespace Ryujinx.Common
|
|
{
|
|
public sealed class AsyncWorkQueue<T> : IDisposable
|
|
{
|
|
private readonly Thread _workerThread;
|
|
private readonly CancellationTokenSource _cts;
|
|
private readonly Action<T> _workerAction;
|
|
private readonly BlockingCollection<T> _queue;
|
|
|
|
public bool IsCancellationRequested => _cts.IsCancellationRequested;
|
|
|
|
public AsyncWorkQueue(Action<T> callback, string name = null) : this(callback, name, new BlockingCollection<T>())
|
|
{
|
|
}
|
|
|
|
public AsyncWorkQueue(Action<T> callback, string name, BlockingCollection<T> collection)
|
|
{
|
|
_cts = new CancellationTokenSource();
|
|
_queue = collection;
|
|
_workerAction = callback;
|
|
_workerThread = new Thread(DoWork)
|
|
{
|
|
Name = name,
|
|
IsBackground = true,
|
|
};
|
|
_workerThread.Start();
|
|
}
|
|
|
|
private void DoWork()
|
|
{
|
|
try
|
|
{
|
|
foreach (var item in _queue.GetConsumingEnumerable(_cts.Token))
|
|
{
|
|
_workerAction(item);
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
}
|
|
}
|
|
|
|
public void Cancel()
|
|
{
|
|
_cts.Cancel();
|
|
}
|
|
|
|
public void CancelAfter(int millisecondsDelay)
|
|
{
|
|
_cts.CancelAfter(millisecondsDelay);
|
|
}
|
|
|
|
public void CancelAfter(TimeSpan delay)
|
|
{
|
|
_cts.CancelAfter(delay);
|
|
}
|
|
|
|
public void Add(T workItem)
|
|
{
|
|
_queue.Add(workItem);
|
|
}
|
|
|
|
public void Add(T workItem, CancellationToken cancellationToken)
|
|
{
|
|
_queue.Add(workItem, cancellationToken);
|
|
}
|
|
|
|
public bool TryAdd(T workItem)
|
|
{
|
|
return _queue.TryAdd(workItem);
|
|
}
|
|
|
|
public bool TryAdd(T workItem, int millisecondsDelay)
|
|
{
|
|
return _queue.TryAdd(workItem, millisecondsDelay);
|
|
}
|
|
|
|
public bool TryAdd(T workItem, int millisecondsDelay, CancellationToken cancellationToken)
|
|
{
|
|
return _queue.TryAdd(workItem, millisecondsDelay, cancellationToken);
|
|
}
|
|
|
|
public bool TryAdd(T workItem, TimeSpan timeout)
|
|
{
|
|
return _queue.TryAdd(workItem, timeout);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_queue.CompleteAdding();
|
|
_cts.Cancel();
|
|
_workerThread.Join();
|
|
|
|
_queue.Dispose();
|
|
_cts.Dispose();
|
|
}
|
|
}
|
|
}
|