Simplifiy WaitableCounter. Fixes it on Mac/ARM.

Not completely sure why it didn't work before...
This commit is contained in:
Henrik Rydgård 2021-06-12 23:07:51 +02:00
parent 50d9d7ea6f
commit ac9cc26a6d
2 changed files with 14 additions and 9 deletions

View file

@ -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;

View file

@ -1,33 +1,37 @@
#pragma once
#include <functional>
#include <atomic>
#include <mutex>
#include <condition_variable>
#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<std::mutex> 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<std::mutex> lock(mutex_);
while (count_.load() != maxValue_) {
cond_.wait(lock);
if (count_ == 0) {
return;
}
cond_.wait(lock);
}
int maxValue_;
std::atomic<int> count_;
int count_;
std::mutex mutex_;
std::condition_variable cond_;
};