2022-04-15 22:13:50 +01:00
|
|
|
#include "ConditionVariableNotifier.hpp"
|
|
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
void ConditionVariableNotifier::notify() {
|
|
|
|
|
const auto lock = std::unique_lock(this->mutex);
|
|
|
|
|
this->notified = true;
|
|
|
|
|
this->conditionalVariable.notify_all();
|
|
|
|
|
}
|
2022-04-15 23:30:57 +01:00
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
void ConditionVariableNotifier::waitForNotification(std::optional<std::chrono::milliseconds> timeout) {
|
|
|
|
|
const auto predicate = [this] {
|
|
|
|
|
return this->notified;
|
|
|
|
|
};
|
|
|
|
|
auto lock = std::unique_lock(this->mutex);
|
2022-04-15 23:30:57 +01:00
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
if (timeout.has_value()) {
|
|
|
|
|
this->conditionalVariable.wait_for(lock, timeout.value(), predicate);
|
2022-04-15 22:13:50 +01:00
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
} else {
|
|
|
|
|
this->conditionalVariable.wait(lock, predicate);
|
2022-04-15 22:13:50 +01:00
|
|
|
}
|
2023-08-13 15:47:51 +01:00
|
|
|
|
|
|
|
|
this->notified = false;
|
2022-04-15 22:13:50 +01:00
|
|
|
}
|