diff options
author | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-10-03 20:41:19 +0200 |
---|---|---|
committer | faketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-10-03 20:41:19 +0200 |
commit | 386d58b5862d8b76925c6523721594887606e82a (patch) | |
tree | ef073e7a843f4b75a4008d4b7383f7cdf08ceee5 /source/cCriticalSection.cpp | |
parent | Visual Studio 2010 solution and project files (diff) | |
download | cuberite-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 'source/cCriticalSection.cpp')
-rw-r--r-- | source/cCriticalSection.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/source/cCriticalSection.cpp b/source/cCriticalSection.cpp new file mode 100644 index 000000000..85f89c195 --- /dev/null +++ b/source/cCriticalSection.cpp @@ -0,0 +1,60 @@ +#include "cCriticalSection.h"
+#include "cMCLogger.h"
+
+#ifdef _WIN32
+#include <Windows.h>
+#else
+#include <pthread.h>
+#endif
+
+cCriticalSection::cCriticalSection()
+{
+#ifdef _WIN32
+ m_CriticalSectionPtr = new CRITICAL_SECTION;
+ InitializeCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
+#else
+ m_Attributes = new pthread_mutexattr_t;
+ pthread_mutexattr_init((pthread_mutexattr_t*)m_Attributes);
+ pthread_mutexattr_settype((pthread_mutexattr_t*)m_Attributes, PTHREAD_MUTEX_RECURSIVE);
+
+ m_CriticalSectionPtr = new pthread_mutex_t;
+ if( pthread_mutex_init( (pthread_mutex_t*)m_CriticalSectionPtr, (pthread_mutexattr_t*)m_Attributes ) != 0 )
+ {
+ LOG("ERROR: Could not initialize Critical Section!");
+ }
+#endif
+}
+
+cCriticalSection::~cCriticalSection()
+{
+#ifdef _WIN32
+ DeleteCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
+ delete (CRITICAL_SECTION*)m_CriticalSectionPtr;
+#else
+ if( pthread_mutex_destroy( (pthread_mutex_t*)m_CriticalSectionPtr ) != 0 )
+ {
+ LOG("ERROR: Could not destroy Critical Section!");
+ }
+ delete (pthread_mutex_t*)m_CriticalSectionPtr;
+ pthread_mutexattr_destroy( (pthread_mutexattr_t*)m_Attributes );
+ delete (pthread_mutexattr_t*)m_Attributes;
+#endif
+}
+
+void cCriticalSection::Lock()
+{
+#ifdef _WIN32
+ EnterCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
+#else
+ pthread_mutex_lock( (pthread_mutex_t*)m_CriticalSectionPtr );
+#endif
+}
+
+void cCriticalSection::Unlock()
+{
+#ifdef _WIN32
+ LeaveCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
+#else
+ pthread_mutex_unlock( (pthread_mutex_t*)m_CriticalSectionPtr );
+#endif
+}
|