From e59b24d8db8a7a9ec21c654302d89e01903f4d96 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sun, 1 Jan 2012 15:56:28 +0100 Subject: fixed atomics and semaphore for windows. can't test it correctly however due to that I only have a virtual box machine --- src/base/tl/threading.h | 74 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 23 deletions(-) (limited to 'src/base/tl') 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 -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 + + 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 { -- cgit 1.4.1