Skip to content
This repository has been archived by the owner on Jan 16, 2020. It is now read-only.

Commit

Permalink
ConsoleHost: Fix scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojaner committed Mar 18, 2019
1 parent 5770076 commit 5aecbcd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 47 deletions.
66 changes: 31 additions & 35 deletions Rocket.Console/Scheduling/AsyncThreadPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,60 @@ namespace Rocket.Console.Scheduling
{
public class AsyncThreadPool
{
private readonly SimpleTaskScheduler scheduler;

private readonly SimpleTaskScheduler taskScheduler;
public EventWaitHandle EventWaitHandle { get; }
public AsyncThreadPool(SimpleTaskScheduler scheduler)
{
this.scheduler = scheduler;
taskScheduler = scheduler;
EventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
}

private readonly EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
private Thread singleCallsThread;
private Thread continousCallsThreads;
private Thread taskThread;
private bool _run;

public void Start()
{
singleCallsThread = new Thread(SingleThreadLoop);
singleCallsThread.Start();
_run = true;
taskThread = new Thread(ContinousThreadLoop);
taskThread.Start();
}

continousCallsThreads = new Thread(ContinousThreadLoop);
continousCallsThreads.Start();
public void Stop()
{
_run = false;
taskThread = null;
}

private void ContinousThreadLoop()
{
while (true)
while (_run)
{
var cpy = scheduler.Tasks.ToList(); // we need a copy because the task list may be modified at runtime
var cpy = taskScheduler.Tasks.Where(c => !c.IsFinished && !c.IsCancelled).ToList(); // we need a copy because the task list may be modified at runtime

foreach (ITask task in cpy.Where(c => !c.IsFinished && !c.IsCancelled))
foreach (ITask task in cpy)
{
if (task.Period == null)
if (task.ExecutionTarget != ExecutionTargetContext.EveryAsyncFrame
&& task.ExecutionTarget != ExecutionTargetContext.EveryFrame
&& task.ExecutionTarget != ExecutionTargetContext.EveryPhysicsUpdate)
if (task.Period == null || (task.Period != null && (task.ExecutionTarget != ExecutionTargetContext.Async && task.ExecutionTarget != ExecutionTargetContext.Sync)))
if (task.ExecutionTarget != ExecutionTargetContext.EveryAsyncFrame && task.ExecutionTarget != ExecutionTargetContext.EveryFrame)
continue;

scheduler.RunTask(task);
taskScheduler.RunTask(task);
}

Thread.Sleep(20);
}
}

private void SingleThreadLoop()
{
while (true)
{
waitHandle.WaitOne();
var cpy = scheduler.Tasks.ToList(); // we need a copy because the task list may be modified at runtime

foreach (ITask task in cpy.Where(c => !c.IsFinished && !c.IsCancelled))
foreach (ITask task in cpy)
{
if (task.ExecutionTarget != ExecutionTargetContext.NextAsyncFrame &&
task.ExecutionTarget != ExecutionTargetContext.NextFrame &&
task.ExecutionTarget != ExecutionTargetContext.NextPhysicsUpdate &&
task.ExecutionTarget != ExecutionTargetContext.Async)
if (task.ExecutionTarget != ExecutionTargetContext.NextAsyncFrame && task.ExecutionTarget != ExecutionTargetContext.NextFrame &&
task.ExecutionTarget != ExecutionTargetContext.Async && task.ExecutionTarget != ExecutionTargetContext.Sync)
continue;

scheduler.RunTask(task);
taskScheduler.RunTask(task);
}

if (cpy.Count == 0)
{
EventWaitHandle.WaitOne();
}

Thread.Sleep(20);
}
}
}
Expand Down
33 changes: 21 additions & 12 deletions Rocket.Console/Scheduling/SimpleTaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Rocket.Console.Scheduling
{
public class SimpleTaskScheduler : ITaskScheduler
public class SimpleTaskScheduler : ITaskScheduler, IDisposable
{
protected IDependencyContainer Container { get; set; }
protected List<SimpleTask> InternalTasks { get; set; }

private volatile int taskIds;
private readonly AsyncThreadPool asyncThreadPool;

public SimpleTaskScheduler(IDependencyContainer container)
{
Container = container;
MainThread = Thread.CurrentThread;

(new AsyncThreadPool(this)).Start();
asyncThreadPool = new AsyncThreadPool(this);
asyncThreadPool.Start();
InternalTasks = new List<SimpleTask>();
}

Expand Down Expand Up @@ -98,12 +99,15 @@ public virtual bool CancelTask(ITask task)

protected virtual void TriggerEvent(SimpleTask task, EventCallback cb = null)
{
TaskScheduleEvent e = new TaskScheduleEvent(task);
asyncThreadPool.EventWaitHandle.Set();

if (!(task.Owner is IEventEmitter owner)) return;
TaskScheduleEvent e = new TaskScheduleEvent(task);
if (!(task.Owner is IEventEmitter owner))
{
return;
}

IEventBus eventBus = Container.Resolve<IEventBus>();

if (eventBus == null)
{
InternalTasks.Add(task);
Expand All @@ -124,7 +128,7 @@ protected virtual void TriggerEvent(SimpleTask task, EventCallback cb = null)

protected internal virtual void RunTask(ITask t)
{
var task = (SimpleTask) t;
var task = (SimpleTask)t;
if (!task.IsReferenceAlive)
{
InternalTasks.Remove(task);
Expand All @@ -139,20 +143,20 @@ protected internal virtual void RunTask(ITask t)

if (task.EndTime != null && task.EndTime < DateTime.Now)
{
((SimpleTask)task).EndTime = DateTime.Now;
task.EndTime = DateTime.Now;
RemoveTask(task);
return;
}

if (task.Period != null
&& ((SimpleTask)task).LastRunTime != null
&& DateTime.Now - ((SimpleTask)task).LastRunTime < task.Period)
&& task.LastRunTime != null
&& DateTime.Now - task.LastRunTime < task.Period)
return;

try
{
task.Action();
((SimpleTask)task).LastRunTime = DateTime.Now;
task.LastRunTime = DateTime.Now;
}
catch (Exception e)
{
Expand All @@ -165,7 +169,7 @@ protected internal virtual void RunTask(ITask t)
|| task.ExecutionTarget == ExecutionTargetContext.NextAsyncFrame
|| task.ExecutionTarget == ExecutionTargetContext.Sync)
{
((SimpleTask)task).EndTime = DateTime.Now;
task.EndTime = DateTime.Now;
RemoveTask(task);
}
}
Expand All @@ -174,5 +178,10 @@ protected virtual void RemoveTask(ITask task)
{
InternalTasks.Remove((SimpleTask)task);
}

public void Dispose()
{
asyncThreadPool.Stop();
}
}
}

0 comments on commit 5aecbcd

Please sign in to comment.