summaryrefslogtreecommitdiffstats
path: root/source/cThread.cpp
diff options
context:
space:
mode:
authorfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-10-03 20:41:19 +0200
committerfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-10-03 20:41:19 +0200
commit386d58b5862d8b76925c6523721594887606e82a (patch)
treeef073e7a843f4b75a4008d4b7383f7cdf08ceee5 /source/cThread.cpp
parentVisual Studio 2010 solution and project files (diff)
downloadcuberite-386d58b5862d8b76925c6523721594887606e82a.tar
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.gz
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.bz2
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.lz
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.xz
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.zst
cuberite-386d58b5862d8b76925c6523721594887606e82a.zip
Diffstat (limited to '')
-rw-r--r--source/cThread.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/source/cThread.cpp b/source/cThread.cpp
new file mode 100644
index 000000000..d0ada2b4e
--- /dev/null
+++ b/source/cThread.cpp
@@ -0,0 +1,75 @@
+#ifndef _WIN32
+#include <cstring>
+#include <semaphore.h>
+#include <errno.h>
+#include <pthread.h>
+#else
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+#endif
+#include "cThread.h"
+#include "cEvent.h"
+#include "cMCLogger.h"
+
+cThread::cThread( ThreadFunc a_ThreadFunction, void* a_Param )
+ : m_ThreadFunction( a_ThreadFunction )
+ , m_Param( a_Param )
+ , m_Event( new cEvent() )
+ , m_StopEvent( 0 )
+{
+}
+
+cThread::~cThread()
+{
+ delete m_Event;
+
+ if( m_StopEvent )
+ {
+ m_StopEvent->Wait();
+ delete m_StopEvent;
+ }
+}
+
+void cThread::Start( bool a_bWaitOnDelete /* = true */ )
+{
+ if( a_bWaitOnDelete )
+ m_StopEvent = new cEvent();
+
+#ifndef _WIN32
+ pthread_t SndThread;
+ if( pthread_create( &SndThread, NULL, MyThread, this) )
+ LOGERROR("ERROR: Could not create thread!");
+#else
+ HANDLE hThread = CreateThread( 0 // security
+ ,0 // stack size
+ , (LPTHREAD_START_ROUTINE) MyThread // function name
+ ,this // parameters
+ ,0 // flags
+ ,0 ); // thread id
+ CloseHandle( hThread );
+#endif
+
+ // Wait until thread has actually been created
+ m_Event->Wait();
+}
+
+#ifdef _WIN32
+unsigned long cThread::MyThread(void* a_Param )
+#else
+void *cThread::MyThread( void *a_Param )
+#endif
+{
+ cThread* self = (cThread*)a_Param;
+ cEvent* StopEvent = self->m_StopEvent;
+
+ ThreadFunc* ThreadFunction = self->m_ThreadFunction;
+ void* ThreadParam = self->m_Param;
+
+ // Set event to let other thread know this thread has been created and it's safe to delete the cThread object
+ self->m_Event->Set();
+
+ ThreadFunction( ThreadParam );
+
+ if( StopEvent ) StopEvent->Set();
+ return 0;
+}