summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Semaphore.h
blob: 0c48d2fb4d767a5b9718603219f47fc29d54439d (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
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#ifdef _WIN32

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>

namespace Common
{
class Semaphore
{
public:
  Semaphore(int initial_count, int maximum_count)
  {
    m_handle = CreateSemaphoreA(nullptr, initial_count, maximum_count, nullptr);
  }

  ~Semaphore() { CloseHandle(m_handle); }
  void Wait() { WaitForSingleObject(m_handle, INFINITE); }
  void Post() { ReleaseSemaphore(m_handle, 1, nullptr); }

private:
  HANDLE m_handle;
};
}  // namespace Common

#else  // _WIN32

#include <semaphore.h>

namespace Common
{
class Semaphore
{
public:
  Semaphore(int initial_count, int maximum_count) { sem_init(&m_handle, 0, initial_count); }
  ~Semaphore() { sem_destroy(&m_handle); }
  void Wait() { sem_wait(&m_handle); }
  void Post() { sem_post(&m_handle); }

private:
  sem_t m_handle;
};
}  // namespace Common

#endif  // _WIN32