summaryrefslogtreecommitdiffstats
path: root/source/cSemaphore.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/cSemaphore.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/cSemaphore.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/source/cSemaphore.cpp b/source/cSemaphore.cpp
new file mode 100644
index 000000000..96c542df6
--- /dev/null
+++ b/source/cSemaphore.cpp
@@ -0,0 +1,96 @@
+#include "cSemaphore.h"
+#include "cMCLogger.h"
+
+#ifdef _WIN32
+#include <Windows.h>
+#else
+#include <semaphore.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#endif
+
+cSemaphore::cSemaphore( unsigned int a_MaxCount, unsigned int a_InitialCount /* = 0 */ )
+#ifndef _WIN32
+ : m_bNamed( false )
+#endif
+{
+#ifndef _WIN32
+ (void)a_MaxCount;
+ m_Handle = new sem_t;
+ if( sem_init( (sem_t*)m_Handle, 0, 0 ) )
+ {
+ LOG("WARNING cSemaphore: Could not create unnamed semaphore, fallback to named.");
+ delete (sem_t*)m_Handle; // named semaphores return their own address
+ m_bNamed = true;
+
+ char c_Str[32];
+ sprintf( c_Str, "cSemaphore%p", this );
+ m_Handle = sem_open( c_Str, O_CREAT, 777, a_InitialCount );
+ if( m_Handle == SEM_FAILED )
+ {
+ LOG("ERROR: Could not create Semaphore. (%i)", errno );
+ }
+ else
+ {
+ if( sem_unlink( c_Str ) != 0 )
+ {
+ LOG("ERROR: Could not unlink cSemaphore. (%i)", errno);
+ }
+ }
+ }
+#else
+ m_Handle = CreateSemaphore(
+ NULL, // security attribute
+ a_InitialCount, // initial count
+ a_MaxCount, // maximum count
+ 0 // name (optional)
+ );
+#endif
+}
+
+cSemaphore::~cSemaphore()
+{
+#ifdef _WIN32
+ CloseHandle( m_Handle );
+#else
+ if( m_bNamed )
+ {
+ if( sem_close( (sem_t*)m_Handle ) != 0 )
+ {
+ LOG("ERROR: Could not close cSemaphore. (%i)", errno);
+ }
+ }
+ else
+ {
+ sem_destroy( (sem_t*)m_Handle );
+ delete (sem_t*)m_Handle;
+ }
+ m_Handle = 0;
+
+#endif
+}
+
+void cSemaphore::Wait()
+{
+#ifndef _WIN32
+ if( sem_wait( (sem_t*)m_Handle ) != 0)
+ {
+ LOG("ERROR: Could not wait for cSemaphore. (%i)", errno);
+ }
+#else
+ WaitForSingleObject( m_Handle, INFINITE);
+#endif
+}
+
+void cSemaphore::Signal()
+{
+#ifndef _WIN32
+ if( sem_post( (sem_t*)m_Handle ) != 0 )
+ {
+ LOG("ERROR: Could not signal cSemaphore. (%i)", errno);
+ }
+#else
+ ReleaseSemaphore( m_Handle, 1, NULL );
+#endif
+}