From a8c78dc86aa5b73ae0b5ece754c059692636f336 Mon Sep 17 00:00:00 2001 From: Nav Date: Fri, 15 Apr 2022 23:30:57 +0100 Subject: [PATCH] Added timeout to ConditionVariableNotifier::waitForNotification() function --- src/Helpers/ConditionVariableNotifier.cpp | 15 +++++++++++---- src/Helpers/ConditionVariableNotifier.hpp | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Helpers/ConditionVariableNotifier.cpp b/src/Helpers/ConditionVariableNotifier.cpp index 505dbd02..1e8cc546 100644 --- a/src/Helpers/ConditionVariableNotifier.cpp +++ b/src/Helpers/ConditionVariableNotifier.cpp @@ -8,11 +8,18 @@ namespace Bloom this->conditionalVariable.notify_all(); } - void ConditionVariableNotifier::waitForNotification() { - auto lock = std::unique_lock(this->mutex); - this->conditionalVariable.wait(lock, [this] { + void ConditionVariableNotifier::waitForNotification(std::optional timeout) { + const auto predicate = [this] { return this->notified; - }); + }; + auto lock = std::unique_lock(this->mutex); + + if (timeout.has_value()) { + this->conditionalVariable.wait_for(lock, timeout.value(), predicate); + + } else { + this->conditionalVariable.wait(lock, predicate); + } this->notified = false; } diff --git a/src/Helpers/ConditionVariableNotifier.hpp b/src/Helpers/ConditionVariableNotifier.hpp index f86fc688..174a040d 100644 --- a/src/Helpers/ConditionVariableNotifier.hpp +++ b/src/Helpers/ConditionVariableNotifier.hpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include "NotifierInterface.hpp" @@ -34,7 +36,7 @@ namespace Bloom /** * Blocks until the contained std::conditional_variable is notified. */ - void waitForNotification(); + void waitForNotification(std::optional timeout = std::nullopt); private: std::mutex mutex;