From fdd954e7e757b4601eb6d2636e12623aef63c9b4 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 13 Aug 2016 22:08:58 +1000 Subject: [PATCH] Common: Add a Semaphore wrapper class --- Source/Core/Common/Common.vcxproj | 1 + Source/Core/Common/Common.vcxproj.filters | 1 + Source/Core/Common/Semaphore.h | 50 +++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 Source/Core/Common/Semaphore.h diff --git a/Source/Core/Common/Common.vcxproj b/Source/Core/Common/Common.vcxproj index fd47fe52ff..b6cb26cf94 100644 --- a/Source/Core/Common/Common.vcxproj +++ b/Source/Core/Common/Common.vcxproj @@ -125,6 +125,7 @@ + diff --git a/Source/Core/Common/Common.vcxproj.filters b/Source/Core/Common/Common.vcxproj.filters index 27410191d8..8b4876f638 100644 --- a/Source/Core/Common/Common.vcxproj.filters +++ b/Source/Core/Common/Common.vcxproj.filters @@ -225,6 +225,7 @@ + diff --git a/Source/Core/Common/Semaphore.h b/Source/Core/Common/Semaphore.h new file mode 100644 index 0000000000..1d1810a5f0 --- /dev/null +++ b/Source/Core/Common/Semaphore.h @@ -0,0 +1,50 @@ +// 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 + +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 + +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