summaryrefslogtreecommitdiff
path: root/source/modules/asura-utils/threading/semaphore.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-03-30 11:59:35 +0800
committerchai <chaifix@163.com>2019-03-30 11:59:35 +0800
commitc270d033fa04873ee7a8925dbb00cae5edc4555c (patch)
treeee27a45c5b946b08dd7a726a925028f6ca3eabf1 /source/modules/asura-utils/threading/semaphore.cpp
parent771df5c31cd5653467fd6e76c1a3e002ca39582c (diff)
*misc
Diffstat (limited to 'source/modules/asura-utils/threading/semaphore.cpp')
-rw-r--r--source/modules/asura-utils/threading/semaphore.cpp25
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