Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed|libcore: Waiting for task pool completion
When using a TaskPool in any non-pooled-thread, the thread may block to wait for the completion of the pool tasks. However, in a pooled thread, blocking is not allowed because that would stop the thread pool from proceeding with its work. Instead, pooled threads should yield to other tasks instead of waiting idly.
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent af09345 commit 0886a63
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions doomsday/libs/core/src/concurrency/taskpool.cpp
Expand Up @@ -252,18 +252,29 @@ void TaskPool::start(TaskFunction taskFunction, Priority priority)

void TaskPool::waitForDone()
{
if (App::inMainThread())
bool allowSleep = true;

if (const iThread *cur = current_Thread())
{
if (!cmp_String(name_Thread(cur), "PooledThread"))
{
// Pooled threads cannot sleep or otherwise the thread pool would likely
// block, if too many / all workers are sleeping.
allowSleep = false;
}
}

if (allowSleep)
{
// Main thread can sleep while waiting for tasks.
// This thread will block here until tasks are complete.
d->waitForEmpty();
}
else
{
// Non-main threads should not block -- this is likely a worker, so we'll yield to
// other tasks while waiting.
// Allow the thread pool to execute other tasks.
while (!isDone())
{
yield(250_ms);
yield(100_ms);
}
}
}
Expand Down

0 comments on commit 0886a63

Please sign in to comment.