From c270d033fa04873ee7a8925dbb00cae5edc4555c Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 30 Mar 2019 11:59:35 +0800 Subject: *misc --- source/modules/asura-utils/threading/semaphore.cpp | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 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 12d4aab..aa5d9dd 100644 --- a/source/modules/asura-utils/threading/semaphore.cpp +++ b/source/modules/asura-utils/threading/semaphore.cpp @@ -1,6 +1,7 @@ #include "../exceptions/exception.h" #include "../type.h" +#include "mutex.h" #include "semaphore.h" namespace AsuraEngine @@ -41,10 +42,10 @@ namespace AsuraEngine mImpl->Signal(); } - void Semaphore::Wait(int timeout) + bool Semaphore::Wait(int timeout /*= ASURA_MUTEX_MAXWAIT*/) { ASSERT(mImpl); - mImpl->Wait(timeout); + return mImpl->Wait(timeout); } #if ASURA_THREAD_WIN32 @@ -52,9 +53,13 @@ namespace AsuraEngine SemaphoreWin32::SemaphoreWin32(unsigned int init_value) : SemaphoreImpl(init_value) { - mSem = CreateSemaphore(NULL, init_value, UINT_MAX, NULL); + // UINT_MAX get error. + mSem = CreateSemaphore(NULL, init_value, INT_MAX, NULL); if (!mSem) - throw Exception("Cant use win32 semaphore."); + { + int errorCode = GetLastError(); + throw Exception("Cant use win32 semaphore. Error code: %d.", errorCode); + } } SemaphoreWin32::~SemaphoreWin32() @@ -72,14 +77,22 @@ namespace AsuraEngine bool SemaphoreWin32::Wait(int timeout) { int result; - result = WaitForSingleObject(mSem, timeout < 0 ? INFINITE : timeout); + result = WaitForSingleObject(mSem, timeout); if (result == WAIT_OBJECT_0) { InterlockedDecrement(&mCount); return true; } - else + else if(result == WAIT_TIMEOUT) + { + // ³¬Ê± return false; + } + else + { + // δ֪´íÎó + throw Exception("WaitForSingleObject() failed"); + } } #endif // ASURA_THREAD_WIN32 -- cgit v1.1-26-g67d0