Skip to content

Commit 87ca22c

Browse files
aganeatstellar
authored andcommitted
[Support] Attempt to fix deadlock in ThreadGroup
This is an attempt to fix the situation described by https://reviews.llvm.org/D104207#2826290 and PR41508. See sequence of operations leading to the bug in https://reviews.llvm.org/D104207#3004689 We ensure that the Latch is completely "free" before decrementing the number of TaskGroupInstances. Differential revision: https://reviews.llvm.org/D109914 (cherry picked from commit 7b25fa8)
1 parent d904698 commit 87ca22c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

llvm/include/llvm/Support/Parallel.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ class Latch {
4040

4141
public:
4242
explicit Latch(uint32_t Count = 0) : Count(Count) {}
43-
~Latch() { sync(); }
43+
~Latch() {
44+
// Ensure at least that sync() was called.
45+
assert(Count == 0);
46+
}
4447

4548
void inc() {
4649
std::lock_guard<std::mutex> lock(Mutex);

llvm/lib/Support/Parallel.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ static std::atomic<int> TaskGroupInstances;
151151
// lock, only allow the first TaskGroup to run tasks parallelly. In the scenario
152152
// of nested parallel_for_each(), only the outermost one runs parallelly.
153153
TaskGroup::TaskGroup() : Parallel(TaskGroupInstances++ == 0) {}
154-
TaskGroup::~TaskGroup() { --TaskGroupInstances; }
154+
TaskGroup::~TaskGroup() {
155+
// We must ensure that all the workloads have finished before decrementing the
156+
// instances count.
157+
L.sync();
158+
--TaskGroupInstances;
159+
}
155160

156161
void TaskGroup::spawn(std::function<void()> F) {
157162
if (Parallel) {

0 commit comments

Comments
 (0)