diff options
author | Mattes D <github@xoft.cz> | 2013-08-15 10:56:52 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2013-08-15 10:56:52 +0200 |
commit | 1a52e89177a907b5d23d3e72f78d30a333da4bfe (patch) | |
tree | 8f2783d06d423361b02457c77f90d6ec874a5191 /source/OSSupport/Event.cpp | |
parent | Merge pull request #91 from tigerw/master (diff) | |
parent | Added simple deadlock detection code. (diff) | |
download | cuberite-1a52e89177a907b5d23d3e72f78d30a333da4bfe.tar cuberite-1a52e89177a907b5d23d3e72f78d30a333da4bfe.tar.gz cuberite-1a52e89177a907b5d23d3e72f78d30a333da4bfe.tar.bz2 cuberite-1a52e89177a907b5d23d3e72f78d30a333da4bfe.tar.lz cuberite-1a52e89177a907b5d23d3e72f78d30a333da4bfe.tar.xz cuberite-1a52e89177a907b5d23d3e72f78d30a333da4bfe.tar.zst cuberite-1a52e89177a907b5d23d3e72f78d30a333da4bfe.zip |
Diffstat (limited to 'source/OSSupport/Event.cpp')
-rw-r--r-- | source/OSSupport/Event.cpp | 104 |
1 files changed, 78 insertions, 26 deletions
diff --git a/source/OSSupport/Event.cpp b/source/OSSupport/Event.cpp index 13b5c1d3f..f3c9053b1 100644 --- a/source/OSSupport/Event.cpp +++ b/source/OSSupport/Event.cpp @@ -15,7 +15,7 @@ cEvent::cEvent(void) { #ifdef _WIN32 - m_Event = CreateEvent( 0, FALSE, FALSE, 0 ); + m_Event = CreateEvent(NULL, FALSE, FALSE, NULL); if (m_Event == NULL) { LOGERROR("cEvent: cannot create event, GLE = %d. Aborting server.", GetLastError()); @@ -78,19 +78,19 @@ cEvent::~cEvent() void cEvent::Wait(void) { -#ifdef _WIN32 - DWORD res = WaitForSingleObject(m_Event, INFINITE); - if (res != WAIT_OBJECT_0) - { - LOGWARN("cEvent: waiting for the event failed: %d, GLE = %d. Continuing, but server may be unstable.", res, GetLastError()); - } -#else - int res = sem_wait(m_Event); - if (res != 0 ) - { - LOGWARN("cEvent: waiting for the event failed: %i, errno = %i. Continuing, but server may be unstable.", res, errno); - } -#endif + #ifdef _WIN32 + DWORD res = WaitForSingleObject(m_Event, INFINITE); + if (res != WAIT_OBJECT_0) + { + LOGWARN("cEvent: waiting for the event failed: %d, GLE = %d. Continuing, but server may be unstable.", res, GetLastError()); + } + #else + int res = sem_wait(m_Event); + if (res != 0 ) + { + LOGWARN("cEvent: waiting for the event failed: %i, errno = %i. Continuing, but server may be unstable.", res, errno); + } + #endif } @@ -99,18 +99,70 @@ void cEvent::Wait(void) void cEvent::Set(void) { -#ifdef _WIN32 - if (!SetEvent(m_Event)) - { - LOGWARN("cEvent: Could not set cEvent: GLE = %d", GetLastError()); - } -#else - int res = sem_post(m_Event); - if (res != 0) - { - LOGWARN("cEvent: Could not set cEvent: %i, errno = %d", res, errno); - } -#endif + #ifdef _WIN32 + if (!SetEvent(m_Event)) + { + LOGWARN("cEvent: Could not set cEvent: GLE = %d", GetLastError()); + } + #else + int res = sem_post(m_Event); + if (res != 0) + { + LOGWARN("cEvent: Could not set cEvent: %i, errno = %d", res, errno); + } + #endif +} + + + + + +cEvent::eWaitResult cEvent::Wait(int a_TimeoutMilliSec) +{ + #ifdef _WIN32 + DWORD res = WaitForSingleObject(m_Event, (DWORD)a_TimeoutMilliSec); + switch (res) + { + case WAIT_OBJECT_0: + { + // The semaphore was signalled + return wrSignalled; + } + case WAIT_TIMEOUT: + { + // The timeout was hit + return wrTimeout; + } + default: + { + LOGWARNING("cEvent: timed-waiting for the event failed: %d, GLE = %d. Continuing, but server may be unstable.", res, GetLastError()); + return wrError; + } + } + #else + timespec timeout; + timeout.tv_sec = a_TimeoutMilliSec / 1000; + timeout.tv_nsec = (a_TimeoutMilliSec % 1000) * 1000000; + int res = sem_timedwait(m_Event, &timeout); + switch (res) + { + case 0: + { + // The semaphore was signalled + return wrSignalled; + } + case ETIMEDOUT: + { + // The timeout was hit + return wrTimeout; + } + default: + { + LOGWARNING("cEvent: timed-waiting for the event failed: %i, errno = %i. Continuing, but server may be unstable.", res, errno); + return wrError; + } + } + #endif } |