| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <chrono> |
| 7 |
#include <memory> |
| 8 |
#include <semaphore> |
| 9 |
|
| 10 |
#include "Common/Functional.h" |
| 11 |
|
| 12 |
namespace Common |
| 13 |
{ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class OneShotEvent |
| 18 |
{ |
| 19 |
public: |
| 20 |
|
| 21 |
void Set() { m_semaphore.release(1); } |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
void Wait() { m_semaphore.acquire(); } |
| 26 |
|
| 27 |
template <typename Rep, typename Period> |
| 28 |
bool WaitFor(const std::chrono::duration<Rep, Period>& rel_time) |
| 29 |
{ |
| 30 |
return m_semaphore.try_acquire_for(rel_time); |
| 31 |
} |
| 32 |
|
| 33 |
private: |
| 34 |
std::binary_semaphore m_semaphore{0}; |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
template <typename EventType> |
| 39 |
class ScopedSetter |
| 40 |
{ |
| 41 |
public: |
| 42 |
ScopedSetter() = default; |
| 43 |
explicit ScopedSetter(EventType* ptr) : m_ptr{ptr} {} |
| 44 |
|
| 45 |
|
| 46 |
void Dismiss() { m_ptr.release(); } |
| 47 |
|
| 48 |
private: |
| 49 |
|
| 50 |
|
| 51 |
using NonOwningSetOnDeletePtr = std::unique_ptr<EventType, InvokerOf<&EventType::Set>>; |
| 52 |
NonOwningSetOnDeletePtr m_ptr; |
| 53 |
}; |
| 54 |
|
| 55 |
} |