about summary refs log tree commit diff
path: root/src/base/tl
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/tl
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/tl')
-rw-r--r--src/base/tl/threading.h74
1 files changed, 51 insertions, 23 deletions
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
 {