diff --git a/Common/Thread/ParallelLoop.cpp b/Common/Thread/ParallelLoop.cpp index 20c4f2fe3a..22d6fdb148 100644 --- a/Common/Thread/ParallelLoop.cpp +++ b/Common/Thread/ParallelLoop.cpp @@ -53,6 +53,7 @@ WaitableCounter *ParallelRangeLoopWaitable(ThreadManager *threadMan, const std:: // Remember that stragglers are done on the current thread // so we don't round up. numTasks = (int)(totalFrac / delta); + printf("numTasks: %d\n", numTasks); WaitableCounter *waitableCounter = new WaitableCounter(numTasks); int64_t counter = (int64_t)lower << fractionalBits; diff --git a/Common/Thread/ParallelLoop.h b/Common/Thread/ParallelLoop.h index 5a0bc22578..f6e50aff7c 100644 --- a/Common/Thread/ParallelLoop.h +++ b/Common/Thread/ParallelLoop.h @@ -1,33 +1,37 @@ #pragma once #include -#include #include #include #include "Common/Thread/ThreadManager.h" -// Kind of like a semaphore I guess. +// Same as the latch from C++21, just counting upwards for no particular reason. struct WaitableCounter : public Waitable { public: - WaitableCounter(int maxValue) : maxValue_(maxValue) {} + WaitableCounter(int count) : count_(count) {} void Count() { - if (count_.fetch_add(1) == maxValue_ - 1) { + std::unique_lock lock(mutex_); + if (count_ == 0) { + return; + } + count_--; + if (count_ == 0) { // We were the last one to increment - cond_.notify_one(); + cond_.notify_all(); } } void Wait() override { std::unique_lock lock(mutex_); - while (count_.load() != maxValue_) { - cond_.wait(lock); + if (count_ == 0) { + return; } + cond_.wait(lock); } - int maxValue_; - std::atomic count_; + int count_; std::mutex mutex_; std::condition_variable cond_; };