diff options
Diffstat (limited to 'source/modules/asura-utils/threading/semaphore.cpp')
-rw-r--r-- | source/modules/asura-utils/threading/semaphore.cpp | 25 |
1 files changed, 19 insertions, 6 deletions
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 |