2022-10-22 21:34:35 +04:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Channels;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-10-22 22:58:15 +04:00
|
|
|
using NLog.LayoutRenderers.Wrappers;
|
2022-10-22 21:34:35 +04:00
|
|
|
|
|
|
|
|
namespace Ragon.Core;
|
|
|
|
|
|
2022-12-16 00:05:46 +04:00
|
|
|
public class Executor: TaskScheduler
|
2022-10-22 21:34:35 +04:00
|
|
|
{
|
|
|
|
|
private ChannelReader<Task> _reader;
|
|
|
|
|
private ChannelWriter<Task> _writer;
|
2022-10-22 22:58:15 +04:00
|
|
|
private Queue<Task> _pendingTasks;
|
|
|
|
|
|
2022-12-16 00:05:46 +04:00
|
|
|
public void Run(Task task)
|
|
|
|
|
{
|
|
|
|
|
task.Start(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Executor()
|
2022-10-22 21:34:35 +04:00
|
|
|
{
|
2022-10-22 22:58:15 +04:00
|
|
|
var channel = Channel.CreateUnbounded<Task>();
|
|
|
|
|
_reader = channel.Reader;
|
|
|
|
|
_writer = channel.Writer;
|
2022-12-16 00:05:46 +04:00
|
|
|
|
|
|
|
|
_pendingTasks = new Queue<Task>();
|
2022-10-22 21:34:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<Task>? GetScheduledTasks()
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void QueueTask(Task task)
|
|
|
|
|
{
|
|
|
|
|
_writer.TryWrite(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 00:05:46 +04:00
|
|
|
public void Execute()
|
2022-10-22 21:34:35 +04:00
|
|
|
{
|
|
|
|
|
while (_reader.TryRead(out var task))
|
|
|
|
|
{
|
|
|
|
|
TryExecuteTask(task);
|
|
|
|
|
|
2022-10-23 18:45:37 +04:00
|
|
|
if (task.Status == TaskStatus.Running)
|
2022-10-22 22:58:15 +04:00
|
|
|
_pendingTasks.Enqueue(task);
|
2022-10-22 21:34:35 +04:00
|
|
|
}
|
2022-10-22 22:58:15 +04:00
|
|
|
|
|
|
|
|
while (_pendingTasks.TryDequeue(out var task))
|
|
|
|
|
_writer.TryWrite(task);
|
2022-10-22 21:34:35 +04:00
|
|
|
}
|
|
|
|
|
}
|