From 084623519e95f0ab0cf4bc328b5fa736d679c5bd Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 31 Jul 2019 21:35:12 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E5=90=8D=E7=A7=B0=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/modules/asura-utils/threading/semaphore.cpp | 154 ++++++++++----------- 1 file changed, 76 insertions(+), 78 deletions(-) (limited to 'source/modules/asura-utils/threading/semaphore.cpp') diff --git a/source/modules/asura-utils/threading/semaphore.cpp b/source/modules/asura-utils/threading/semaphore.cpp index 7e5ccf5..f9ffb35 100644 --- a/source/modules/asura-utils/threading/semaphore.cpp +++ b/source/modules/asura-utils/threading/semaphore.cpp @@ -4,98 +4,96 @@ #include "mutex.h" #include "semaphore.h" -namespace AsuraEngine -{ - namespace Threading - { +namespace_begin(AsuraEngine) +namespace_begin(Threading) #define try_create_semaphore(impl) \ - if (!m_Impl) \ - { \ - try \ - { \ - m_Impl = new impl(init_count); \ - } \ - catch (Exception& e) \ - { \ - m_Impl = nullptr; \ - } \ - } +if (!m_Impl) \ +{ \ + try \ + { \ + m_Impl = new impl(init_count); \ + } \ + catch (Exception& e) \ + { \ + m_Impl = nullptr; \ + } \ +} - Semaphore::Semaphore(unsigned int init_count) - : m_Impl(nullptr) - { +Semaphore::Semaphore(unsigned int init_count) + : m_Impl(nullptr) +{ #ifdef ASURA_THREAD_WIN32 - try_create_semaphore(SemaphoreWin32); + try_create_semaphore(SemaphoreWin32); #endif - //ASSERT(m_Impl); - } + //ASSERT(m_Impl); +} - Semaphore::~Semaphore() - { - if (m_Impl) delete m_Impl; - } +Semaphore::~Semaphore() +{ + if (m_Impl) delete m_Impl; +} - void Semaphore::Signal() - { - ASSERT(m_Impl); - m_Impl->Signal(); - } +void Semaphore::Signal() +{ + ASSERT(m_Impl); + m_Impl->Signal(); +} - bool Semaphore::Wait(int timeout /*= ASURA_MUTEX_MAXWAIT*/) - { - ASSERT(m_Impl); - return m_Impl->Wait(timeout); - } +bool Semaphore::Wait(int timeout /*= ASURA_MUTEX_MAXWAIT*/) +{ + ASSERT(m_Impl); + return m_Impl->Wait(timeout); +} #if ASURA_THREAD_WIN32 - SemaphoreWin32::SemaphoreWin32(unsigned int init_value) - : SemaphoreImpl(init_value) - { - // UINT_MAX get error. - m_Sem = CreateSemaphore(NULL, init_value, INT_MAX, NULL); - if (!m_Sem) - { - int errorCode = GetLastError(); - throw Exception("Cant use win32 semaphore. Error code: %d.", errorCode); - } - } +SemaphoreWin32::SemaphoreWin32(unsigned int init_value) + : SemaphoreImpl(init_value) +{ + // UINT_MAX get error. + m_Sem = CreateSemaphore(NULL, init_value, INT_MAX, NULL); + if (!m_Sem) + { + int errorCode = GetLastError(); + throw Exception("Cant use win32 semaphore. Error code: %d.", errorCode); + } +} - SemaphoreWin32::~SemaphoreWin32() - { - CloseHandle(m_Sem); - } +SemaphoreWin32::~SemaphoreWin32() +{ + CloseHandle(m_Sem); +} - void SemaphoreWin32::Signal() - { - InterlockedIncrement(&m_Count); - if (ReleaseSemaphore(m_Sem, 1, NULL) == FALSE) - InterlockedDecrement(&m_Count); - } +void SemaphoreWin32::Signal() +{ + InterlockedIncrement(&m_Count); + if (ReleaseSemaphore(m_Sem, 1, NULL) == FALSE) + InterlockedDecrement(&m_Count); +} - bool SemaphoreWin32::Wait(int timeout) - { - int result; - result = WaitForSingleObject(m_Sem, timeout); - if (result == WAIT_OBJECT_0) - { - InterlockedDecrement(&m_Count); - return true; - } - else if(result == WAIT_TIMEOUT) - { - // ³¬Ê± - return false; - } - else - { - // δ֪´íÎó - throw Exception("WaitForSingleObject() failed"); - } - } +bool SemaphoreWin32::Wait(int timeout) +{ + int result; + result = WaitForSingleObject(m_Sem, timeout); + if (result == WAIT_OBJECT_0) + { + InterlockedDecrement(&m_Count); + return true; + } + else if(result == WAIT_TIMEOUT) + { + // ³¬Ê± + return false; + } + else + { + // δ֪´íÎó + throw Exception("WaitForSingleObject() failed"); + } +} #endif // ASURA_THREAD_WIN32 - } -} \ No newline at end of file +namespace_end +namespace_end \ No newline at end of file -- cgit v1.1-26-g67d0