about summary refs log tree commit diff
path: root/src/base
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2012-01-01 15:56:28 +0100
committerMagnus Auvinen <magnus.auvinen@gmail.com>2012-01-01 15:56:28 +0100
commite59b24d8db8a7a9ec21c654302d89e01903f4d96 (patch)
tree26b38372e28f3c440f73c00ba60325a6c910726a /src/base
parentc31c82a5840640b0c7f4b07eb90e681b88b0a330 (diff)
downloadzcatch-e59b24d8db8a7a9ec21c654302d89e01903f4d96.tar.gz
zcatch-e59b24d8db8a7a9ec21c654302d89e01903f4d96.zip
fixed atomics and semaphore for windows. can't test it correctly however due to that I only have a virtual box machine
Diffstat (limited to 'src/base')
-rw-r--r--src/base/system.c15
-rw-r--r--src/base/system.h17
-rw-r--r--src/base/tl/threading.h74
3 files changed, 83 insertions, 23 deletions
diff --git a/src/base/system.c b/src/base/system.c
index 4ea6d413..2026199b 100644
--- a/src/base/system.c
+++ b/src/base/system.c
@@ -495,6 +495,21 @@ void lock_release(LOCK lock)
 #endif
 }
 
+#if defined(CONF_FAMILY_UNIX)
+void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); }
+void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); }
+void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); }
+void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); }
+#elif defined(CONF_FAMILY_WINDOWS)
+void semaphore_init(SEMAPHORE *sem) { (HANDLE)(*sem) = CreateSemaphore(0, 0, 10000, 0); }
+void semaphore_wait(SEMAPHORE *sem) { WaitForSingleObject((HANDLE)*sem, 0L); }
+void semaphore_signal(SEMAPHORE *sem) { ReleaseSemaphore((HANDLE)*sem, 1, NULL); }
+void semaphore_destroy(SEMAPHORE *sem) { CloseHandle((HANDLE)*sem); }
+#else
+	#error not implemented on this platform
+#endif
+
+
 /* -----  time ----- */
 int64 time_get()
 {
diff --git a/src/base/system.h b/src/base/system.h
index aaa5b43f..ca93279f 100644
--- a/src/base/system.h
+++ b/src/base/system.h
@@ -388,6 +388,23 @@ int lock_try(LOCK lock);
 void lock_wait(LOCK lock);
 void lock_release(LOCK lock);
 
+
+/* Group: Semaphores */
+
+#if defined(CONF_FAMILY_UNIX)
+	#include <semaphore.h>
+	typedef sem_t SEMAPHORE;
+#elif defined(CONF_FAMILY_WINDOWS)
+	typedef void* SEMAPHORE;
+#else
+	#error missing sempahore implementation
+#endif
+
+void semaphore_init(SEMAPHORE *sem);
+void semaphore_wait(SEMAPHORE *sem);
+void semaphore_signal(SEMAPHORE *sem);
+void semaphore_destroy(SEMAPHORE *sem);
+
 /* Group: Timer */
 #ifdef __GNUC__
 /* if compiled with -pedantic-errors it will complain about long
diff --git a/src/base/tl/threading.h b/src/base/tl/threading.h
index 66751ec4..dbf788cd 100644
--- a/src/base/tl/threading.h
+++ b/src/base/tl/threading.h
@@ -3,32 +3,60 @@
 
 #include "../system.h"
 
-inline unsigned atomic_inc(volatile unsigned *pValue)
-{
-	return __sync_fetch_and_add(pValue, 1);
-}
+/*
+	atomic_inc - should return the value after increment
+	atomic_dec - should return the value after decrement
+	atomic_compswap - should return the value before the eventual swap
+	
+*/
 
-inline unsigned atomic_dec(volatile unsigned *pValue)
-{
-	return __sync_fetch_and_add(pValue, -1);
-}
+#if defined(__GNUC__)
 
-inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value)
-{
-	return __sync_val_compare_and_swap(pValue, comperand, value);
-}
+	inline unsigned atomic_inc(volatile unsigned *pValue)
+	{
+		return __sync_fetch_and_add(pValue, 1);
+	}
 
-inline void sync_barrier()
-{
-	__sync_synchronize();
-}
-
-#include <semaphore.h>
-typedef sem_t SEMAPHORE;
-inline void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); }
-inline void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); }
-inline void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); }
-inline void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); }
+	inline unsigned atomic_dec(volatile unsigned *pValue)
+	{
+		return __sync_fetch_and_add(pValue, -1);
+	}
+
+	inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value)
+	{
+		return __sync_val_compare_and_swap(pValue, comperand, value);
+	}
+
+	inline void sync_barrier()
+	{
+		__sync_synchronize();
+	}
+
+#elif defined(_MSC_VER)
+	#include <intrin.h>
+
+	inline unsigned atomic_inc(volatile unsigned *pValue)
+	{
+		return _InterlockedIncrement((volatile long *)pValue);
+	}
+	
+	inline unsigned atomic_dec(volatile unsigned *pValue)
+	{
+		return _InterlockedDecrement((volatile long *)pValue);
+	}
+
+	inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value)
+	{
+		return _InterlockedCompareExchange((volatile long *)pValue, (long)value, (long)comperand);
+	}
+
+	inline void sync_barrier()
+	{
+		_ReadWriteBarrier();
+	}
+#else
+	#error missing atomic implementation for this compiler
+#endif
 
 class semaphore
 {