Fixed [ThreadStatic] systems in WorkQueue

This commit is contained in:
Justin Swanson 2019-12-07 00:40:48 -06:00
parent 63afc2dc9e
commit bc436e53c7
3 changed files with 16 additions and 7 deletions

View File

@ -102,7 +102,7 @@ namespace Wabbajack.Common
public static void Status(string msg, int progress = 0)
{
WorkQueue.CurrentQueue?.Report(msg, progress);
WorkQueue.AsyncLocalCurrentQueue.Value?.Report(msg, progress);
}
/// <summary>
@ -549,7 +549,10 @@ namespace Wabbajack.Common
if (WorkQueue.WorkerThread)
while (remainingTasks > 0)
if (queue.Queue.TryTake(out var a, 500))
{
WorkQueue.AsyncLocalCurrentQueue.Value = WorkQueue.ThreadLocalCurrentQueue.Value;
await a();
}
return await Task.WhenAll(tasks);
}
@ -584,7 +587,10 @@ namespace Wabbajack.Common
if (WorkQueue.WorkerThread)
while (remainingTasks > 0)
if (queue.Queue.TryTake(out var a, 500))
{
WorkQueue.AsyncLocalCurrentQueue.Value = WorkQueue.ThreadLocalCurrentQueue.Value;
await a();
}
return await Task.WhenAll(tasks);
}

View File

@ -15,10 +15,11 @@ namespace Wabbajack.Common
internal BlockingCollection<Func<Task>>
Queue = new BlockingCollection<Func<Task>>(new ConcurrentStack<Func<Task>>());
[ThreadStatic] private static int CpuId;
private static readonly AsyncLocal<int> CpuId = new AsyncLocal<int>();
internal static bool WorkerThread => CurrentQueue != null;
[ThreadStatic] internal static WorkQueue CurrentQueue;
internal static bool WorkerThread => ThreadLocalCurrentQueue.Value != null;
internal static readonly ThreadLocal<WorkQueue> ThreadLocalCurrentQueue = new ThreadLocal<WorkQueue>();
internal static readonly AsyncLocal<WorkQueue> AsyncLocalCurrentQueue = new AsyncLocal<WorkQueue>();
private readonly Subject<CPUStatus> _Status = new Subject<CPUStatus>();
public IObservable<CPUStatus> Status => _Status;
@ -51,8 +52,9 @@ namespace Wabbajack.Common
private async Task ThreadBody(int idx)
{
CpuId = idx;
CurrentQueue = this;
CpuId.Value = idx;
ThreadLocalCurrentQueue.Value = this;
AsyncLocalCurrentQueue.Value = this;
try
{
@ -77,7 +79,7 @@ namespace Wabbajack.Common
Progress = progress,
ProgressPercent = progress / 100f,
Msg = msg,
ID = CpuId,
ID = CpuId.Value,
IsWorking = isWorking
});
}

View File

@ -86,6 +86,7 @@ namespace Wabbajack.Lib.Downloaders
long totalRead = 0;
var bufferSize = 1024 * 32;
Utils.Status($"Starting Download {a?.Name ?? Url}", 0);
var response = await client.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead);
Stream stream;