Fix race condition in LimitedWaitable between Notify and Wait

This commit is contained in:
Henrik Rydgård 2022-04-08 12:29:30 +02:00
parent bde54ccdc0
commit adfce57d9e
2 changed files with 3 additions and 3 deletions

View file

@ -12,8 +12,8 @@ public:
} }
void Wait() override { void Wait() override {
std::unique_lock<std::mutex> lock;
if (!triggered_) { if (!triggered_) {
std::unique_lock<std::mutex> lock;
cond_.wait(lock, [&] { return triggered_.load(); }); cond_.wait(lock, [&] { return triggered_.load(); });
} }
} }

View file

@ -12,18 +12,18 @@ public:
} }
void Wait() override { void Wait() override {
std::unique_lock<std::mutex> lock(mutex_);
if (!triggered_) { if (!triggered_) {
std::unique_lock<std::mutex> lock(mutex_);
cond_.wait(lock, [&] { return triggered_.load(); }); cond_.wait(lock, [&] { return triggered_.load(); });
} }
} }
bool WaitFor(double budget) { bool WaitFor(double budget) {
uint32_t us = budget > 0 ? (uint32_t)(budget * 1000000.0) : 0; uint32_t us = budget > 0 ? (uint32_t)(budget * 1000000.0) : 0;
std::unique_lock<std::mutex> lock(mutex_);
if (!triggered_) { if (!triggered_) {
if (us == 0) if (us == 0)
return false; return false;
std::unique_lock<std::mutex> lock(mutex_);
cond_.wait_for(lock, std::chrono::microseconds(us), [&] { return triggered_.load(); }); cond_.wait_for(lock, std::chrono::microseconds(us), [&] { return triggered_.load(); });
} }
return triggered_; return triggered_;