summaryrefslogtreecommitdiff
path: root/src/common/polyfill_thread.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/polyfill_thread.h')
-rw-r--r--src/common/polyfill_thread.h20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/common/polyfill_thread.h b/src/common/polyfill_thread.h
index b5ef055db..41cbb9ed5 100644
--- a/src/common/polyfill_thread.h
+++ b/src/common/polyfill_thread.h
@@ -19,8 +19,8 @@
19namespace Common { 19namespace Common {
20 20
21template <typename Condvar, typename Lock, typename Pred> 21template <typename Condvar, typename Lock, typename Pred>
22void CondvarWait(Condvar& cv, Lock& lock, std::stop_token token, Pred&& pred) { 22void CondvarWait(Condvar& cv, std::unique_lock<Lock>& lk, std::stop_token token, Pred&& pred) {
23 cv.wait(lock, token, std::move(pred)); 23 cv.wait(lk, token, std::move(pred));
24} 24}
25 25
26template <typename Rep, typename Period> 26template <typename Rep, typename Period>
@@ -332,13 +332,17 @@ private:
332namespace Common { 332namespace Common {
333 333
334template <typename Condvar, typename Lock, typename Pred> 334template <typename Condvar, typename Lock, typename Pred>
335void CondvarWait(Condvar& cv, Lock& lock, std::stop_token token, Pred pred) { 335void CondvarWait(Condvar& cv, std::unique_lock<Lock>& lk, std::stop_token token, Pred pred) {
336 if (token.stop_requested()) { 336 if (token.stop_requested()) {
337 return; 337 return;
338 } 338 }
339 339
340 std::stop_callback callback(token, [&] { cv.notify_all(); }); 340 std::stop_callback callback(token, [&] {
341 cv.wait(lock, [&] { return pred() || token.stop_requested(); }); 341 { std::scoped_lock lk2{*lk.mutex()}; }
342 cv.notify_all();
343 });
344
345 cv.wait(lk, [&] { return pred() || token.stop_requested(); });
342} 346}
343 347
344template <typename Rep, typename Period> 348template <typename Rep, typename Period>
@@ -353,8 +357,10 @@ bool StoppableTimedWait(std::stop_token token, const std::chrono::duration<Rep,
353 357
354 std::stop_callback cb(token, [&] { 358 std::stop_callback cb(token, [&] {
355 // Wake up the waiting thread. 359 // Wake up the waiting thread.
356 std::unique_lock lk{m}; 360 {
357 stop_requested = true; 361 std::scoped_lock lk{m};
362 stop_requested = true;
363 }
358 cv.notify_one(); 364 cv.notify_one();
359 }); 365 });
360 366