Skip to content
This repository was archived by the owner on Dec 22, 2019. It is now read-only.

Commit caaf7f7

Browse files
committed
making the queue section a single atomic operation to fix #workers overflowing max_worker_count
1 parent bf7910c commit caaf7f7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

UpdateLib/UpdateLib/Tasks/WorkerScheduler.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static WorkerScheduler Instance
3939
private readonly AtomicInteger m_currentWorkerCount;
4040
private readonly AsyncTask m_dispatcherTask;
4141
private readonly ManualResetEvent m_waitForAvailableWorker;
42+
private readonly object sync = new object();
4243

4344
#endregion
4445

@@ -65,19 +66,22 @@ private void Dispatcher()
6566
AsyncTask task = null;
6667
while (m_taskQueue.TryDequeue(out task))
6768
{
68-
if (task.IsCompleted || task.IsCancelled || task.HasErrors)
69-
continue;
69+
lock (sync)
70+
{
71+
if (task.IsCompleted || task.IsCancelled || task.HasErrors)
72+
continue;
7073

71-
SetupTask(task);
74+
SetupTask(task);
7275

73-
if (m_currentWorkerCount.Value >= MAX_WORKERS)
74-
m_waitForAvailableWorker.Reset();
76+
if (m_currentWorkerCount.Value >= MAX_WORKERS)
77+
m_waitForAvailableWorker.Reset();
7578

76-
m_waitForAvailableWorker.WaitOne();
79+
m_waitForAvailableWorker.WaitOne();
7780

78-
Logger.Debug(GetType().Name, $"Current worker count: {m_currentWorkerCount.Increment()}");
81+
Logger.Debug(GetType().Name, $"Current worker count: {m_currentWorkerCount.Increment()} | Current queue count: {m_taskQueue.Count}");
7982

80-
task.ConfigureAwait(false).Start();
83+
task.ConfigureAwait(false).Start();
84+
}
8185
}
8286
}
8387

0 commit comments

Comments
 (0)