summaryrefslogtreecommitdiffstats
path: root/source/OSSupport
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2013-08-15 10:56:52 +0200
committerMattes D <github@xoft.cz>2013-08-15 10:56:52 +0200
commit1a52e89177a907b5d23d3e72f78d30a333da4bfe (patch)
tree8f2783d06d423361b02457c77f90d6ec874a5191 /source/OSSupport
parentMerge pull request #91 from tigerw/master (diff)
parentAdded simple deadlock detection code. (diff)
downloadcuberite-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')
-rw-r--r--source/OSSupport/Event.cpp104
-rw-r--r--source/OSSupport/Event.h10
-rw-r--r--source/OSSupport/IsThread.cpp44
-rw-r--r--source/OSSupport/IsThread.h6
4 files changed, 116 insertions, 48 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
}
diff --git a/source/OSSupport/Event.h b/source/OSSupport/Event.h
index 71f418c0c..803d73b7e 100644
--- a/source/OSSupport/Event.h
+++ b/source/OSSupport/Event.h
@@ -19,12 +19,22 @@
class cEvent
{
public:
+ enum eWaitResult
+ {
+ wrSignalled,
+ wrTimeout,
+ wrError,
+ } ;
+
cEvent(void);
~cEvent();
void Wait(void);
void Set (void);
+ /// Waits for the semaphore with a timeout
+ eWaitResult Wait(int a_TimeoutMilliSec);
+
private:
#ifdef _WIN32
diff --git a/source/OSSupport/IsThread.cpp b/source/OSSupport/IsThread.cpp
index 257c5c876..45e329a68 100644
--- a/source/OSSupport/IsThread.cpp
+++ b/source/OSSupport/IsThread.cpp
@@ -116,36 +116,38 @@ bool cIsThread::Start(void)
+void cIsThread::Stop(void)
+{
+ if (m_Handle == NULL)
+ {
+ return;
+ }
+ m_ShouldTerminate = true;
+ Wait();
+}
+
+
+
+
+
bool cIsThread::Wait(void)
{
- #ifdef _WIN32
+ if (m_Handle == NULL)
+ {
+ return true;
+ }
+ LOGD("Waiting for thread %s to finish", m_ThreadName.c_str());
- if (m_Handle == NULL)
- {
- return true;
- }
- // Cannot log, logger may already be stopped:
- // LOG("Waiting for thread \"%s\" to terminate.", m_ThreadName.c_str());
+ #ifdef _WIN32
int res = WaitForSingleObject(m_Handle, INFINITE);
m_Handle = NULL;
- // Cannot log, logger may already be stopped:
- // LOG("Thread \"%s\" %s terminated, GLE = %d", m_ThreadName.c_str(), (res == WAIT_OBJECT_0) ? "" : "not", GetLastError());
+ LOGD("Thread %s finished", m_ThreadName.c_str());
return (res == WAIT_OBJECT_0);
-
#else // _WIN32
-
- if (!m_HasStarted)
- {
- return true;
- }
- // Cannot log, logger may already be stopped:
- // LOG("Waiting for thread \"%s\" to terminate.", m_ThreadName.c_str());
int res = pthread_join(m_Handle, NULL);
- m_HasStarted = false;
- // Cannot log, logger may already be stopped:
- // LOG("Thread \"%s\" %s terminated, errno = %d", m_ThreadName.c_str(), (res == 0) ? "" : "not", errno);
+ m_Handle = NULL;
+ LOGD("Thread %s finished", m_ThreadName.c_str());
return (res == 0);
-
#endif // else _WIN32
}
diff --git a/source/OSSupport/IsThread.h b/source/OSSupport/IsThread.h
index 2a4451a4a..9b7f0b73e 100644
--- a/source/OSSupport/IsThread.h
+++ b/source/OSSupport/IsThread.h
@@ -39,6 +39,9 @@ public:
/// Starts the thread; returns without waiting for the actual start
bool Start(void);
+ /// Signals the thread to terminate and waits until it's finished
+ void Stop(void);
+
/// Waits for the thread to finish. Doesn't signalize the ShouldTerminate flag
bool Wait(void);
@@ -54,7 +57,9 @@ private:
static DWORD_PTR __stdcall thrExecute(LPVOID a_Param)
{
+ HWND IdentificationWnd = CreateWindow("STATIC", ((cIsThread *)a_Param)->m_ThreadName.c_str(), 0, 0, 0, 0, WS_OVERLAPPED, NULL, NULL, NULL, NULL);
((cIsThread *)a_Param)->Execute();
+ DestroyWindow(IdentificationWnd);
return 0;
}
@@ -70,7 +75,6 @@ private:
}
#endif // else _WIN32
-
} ;