2019-07-22 22:17:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-11-05 04:14:31 +00:00
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
using System.Reactive.Subjects;
|
2019-07-22 22:17:46 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
2019-11-20 23:39:03 +00:00
|
|
|
|
public class WorkQueue : IDisposable
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
2019-11-17 04:16:42 +00:00
|
|
|
|
internal BlockingCollection<Action>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
Queue = new BlockingCollection<Action>(new ConcurrentStack<Action>());
|
2019-07-22 22:17:46 +00:00
|
|
|
|
|
2019-09-14 04:35:42 +00:00
|
|
|
|
[ThreadStatic] private static int CpuId;
|
2019-07-22 22:17:46 +00:00
|
|
|
|
|
2019-11-17 06:02:09 +00:00
|
|
|
|
internal static bool WorkerThread => CurrentQueue != null;
|
|
|
|
|
[ThreadStatic] internal static WorkQueue CurrentQueue;
|
2019-09-14 04:35:42 +00:00
|
|
|
|
|
2019-11-17 06:02:09 +00:00
|
|
|
|
private static readonly Subject<CPUStatus> _Status = new Subject<CPUStatus>();
|
2019-11-17 04:16:42 +00:00
|
|
|
|
public IObservable<CPUStatus> Status => _Status;
|
2019-11-17 06:02:09 +00:00
|
|
|
|
|
2019-09-14 04:35:42 +00:00
|
|
|
|
public static List<Thread> Threads { get; private set; }
|
2019-08-10 15:21:50 +00:00
|
|
|
|
|
2019-11-17 23:48:32 +00:00
|
|
|
|
public WorkQueue(int threadCount = 0)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
2019-11-17 23:48:32 +00:00
|
|
|
|
StartThreads(threadCount == 0 ? Environment.ProcessorCount : threadCount);
|
2019-07-22 22:17:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 23:48:32 +00:00
|
|
|
|
private void StartThreads(int threadCount)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
2019-11-20 23:39:03 +00:00
|
|
|
|
ThreadCount = threadCount;
|
2019-11-17 23:48:32 +00:00
|
|
|
|
Threads = Enumerable.Range(0, threadCount)
|
2019-09-14 04:35:42 +00:00
|
|
|
|
.Select(idx =>
|
|
|
|
|
{
|
|
|
|
|
var thread = new Thread(() => ThreadBody(idx));
|
|
|
|
|
thread.Priority = ThreadPriority.BelowNormal;
|
|
|
|
|
thread.IsBackground = true;
|
|
|
|
|
thread.Name = string.Format("Wabbajack_Worker_{0}", idx);
|
|
|
|
|
thread.Start();
|
|
|
|
|
return thread;
|
|
|
|
|
}).ToList();
|
2019-07-22 22:17:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 23:39:03 +00:00
|
|
|
|
public int ThreadCount { get; private set; }
|
|
|
|
|
|
2019-11-17 04:16:42 +00:00
|
|
|
|
private void ThreadBody(int idx)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
|
|
|
|
CpuId = idx;
|
2019-11-17 06:02:09 +00:00
|
|
|
|
CurrentQueue = this;
|
2019-07-22 22:17:46 +00:00
|
|
|
|
|
2019-09-14 04:35:42 +00:00
|
|
|
|
while (true)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
|
|
|
|
Report("Waiting", 0);
|
|
|
|
|
var f = Queue.Take();
|
|
|
|
|
f();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-14 04:35:42 +00:00
|
|
|
|
|
2019-11-17 04:16:42 +00:00
|
|
|
|
public void Report(string msg, int progress)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
2019-11-17 04:16:42 +00:00
|
|
|
|
_Status.OnNext(
|
|
|
|
|
new CPUStatus
|
|
|
|
|
{
|
|
|
|
|
Progress = progress,
|
|
|
|
|
Msg = msg,
|
|
|
|
|
ID = CpuId
|
|
|
|
|
});
|
2019-07-22 22:17:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 04:16:42 +00:00
|
|
|
|
public void QueueTask(Action a)
|
2019-07-22 22:17:46 +00:00
|
|
|
|
{
|
|
|
|
|
Queue.Add(a);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 04:16:42 +00:00
|
|
|
|
public void Shutdown()
|
2019-08-02 23:04:04 +00:00
|
|
|
|
{
|
2019-11-17 04:16:42 +00:00
|
|
|
|
Threads.Do(th => th.Abort());
|
2019-08-02 23:04:04 +00:00
|
|
|
|
}
|
2019-11-20 23:39:03 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Shutdown();
|
|
|
|
|
Queue?.Dispose();
|
|
|
|
|
}
|
2019-07-22 22:17:46 +00:00
|
|
|
|
}
|
2019-11-05 04:14:31 +00:00
|
|
|
|
|
|
|
|
|
public class CPUStatus
|
|
|
|
|
{
|
|
|
|
|
public int Progress { get; internal set; }
|
|
|
|
|
public string Msg { get; internal set; }
|
|
|
|
|
public int ID { get; internal set; }
|
|
|
|
|
}
|
2019-11-20 23:39:03 +00:00
|
|
|
|
}
|