2022-04-15 22:13:50 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <condition_variable>
|
2022-04-15 23:30:57 +01:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <optional>
|
2022-04-15 22:13:50 +01:00
|
|
|
|
|
|
|
|
#include "NotifierInterface.hpp"
|
|
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
/**
|
|
|
|
|
* The ConditionVariableNotifier class is an implementation of the NotifierInterface, using an
|
|
|
|
|
* std::condition_variable.
|
|
|
|
|
*/
|
|
|
|
|
class ConditionVariableNotifier: public NotifierInterface
|
2022-04-15 22:13:50 +01:00
|
|
|
{
|
2023-08-13 15:47:51 +01:00
|
|
|
public:
|
|
|
|
|
ConditionVariableNotifier() = default;
|
|
|
|
|
~ConditionVariableNotifier() override = default;
|
|
|
|
|
|
|
|
|
|
ConditionVariableNotifier(ConditionVariableNotifier& other) = delete;
|
|
|
|
|
ConditionVariableNotifier& operator = (ConditionVariableNotifier& other) = delete;
|
|
|
|
|
|
|
|
|
|
ConditionVariableNotifier(ConditionVariableNotifier&& other) noexcept = delete;
|
|
|
|
|
ConditionVariableNotifier& operator = (ConditionVariableNotifier&& other) = delete;
|
|
|
|
|
|
|
|
|
|
void notify() override;
|
|
|
|
|
|
2022-04-15 22:13:50 +01:00
|
|
|
/**
|
2023-08-13 15:47:51 +01:00
|
|
|
* Blocks until the contained std::conditional_variable is notified.
|
2022-04-15 22:13:50 +01:00
|
|
|
*/
|
2023-08-13 15:47:51 +01:00
|
|
|
void waitForNotification(std::optional<std::chrono::milliseconds> timeout = std::nullopt);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::mutex mutex;
|
|
|
|
|
std::condition_variable conditionalVariable;
|
|
|
|
|
bool notified = false;
|
|
|
|
|
};
|