summaryrefslogtreecommitdiff
path: root/Source/Core/Common/OneShotEvent.h
blob: 312848ed9187127e0b1f9c66bc82ce8f57e4ff9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <chrono>
#include <memory>
#include <semaphore>

#include "Common/Functional.h"

namespace Common
{

// A one-time-use single-producer single-consumer thread synchronization class.
// Safe when `Set` will cause a `Wait`ing thread to immediately destruct the event itself.
class OneShotEvent
{
public:
  // One thread should call Set() exactly once.
  void Set() { m_semaphore.release(1); }

  // One thread may call Wait() once or WaitFor() until it returns true.

  void Wait() { m_semaphore.acquire(); }

  template <typename Rep, typename Period>
  bool WaitFor(const std::chrono::duration<Rep, Period>& rel_time)
  {
    return m_semaphore.try_acquire_for(rel_time);
  }

private:
  std::binary_semaphore m_semaphore{0};
};

// Invokes Set() on the given object upon destruction.
template <typename EventType>
class ScopedSetter
{
public:
  ScopedSetter() = default;
  explicit ScopedSetter(EventType* ptr) : m_ptr{ptr} {}

  // Forgets the object without invoking Set().
  void Dismiss() { m_ptr.release(); }

private:
  // Leveraging unique_ptr conveniently makes this class move-only.
  // It does no actual deletion, just calls Set().
  using NonOwningSetOnDeletePtr = std::unique_ptr<EventType, InvokerOf<&EventType::Set>>;
  NonOwningSetOnDeletePtr m_ptr;
};

}  // namespace Common