From 8ffe5826156d07b1feb7fc58bf59a1431e01160f Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Fri, 30 Dec 2011 16:02:22 +0100 Subject: ugly incomplete hack to put the rendering into another thread so we don't have to wait for the flip --- src/base/tl/threading.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/base/tl/threading.h (limited to 'src/base') diff --git a/src/base/tl/threading.h b/src/base/tl/threading.h new file mode 100644 index 00000000..66751ec4 --- /dev/null +++ b/src/base/tl/threading.h @@ -0,0 +1,78 @@ + +#pragma once + +#include "../system.h" + +inline unsigned atomic_inc(volatile unsigned *pValue) +{ + return __sync_fetch_and_add(pValue, 1); +} + +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(); +} + +#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); } + +class semaphore +{ + SEMAPHORE sem; +public: + semaphore() { semaphore_init(&sem); } + ~semaphore() { semaphore_destroy(&sem); } + void wait() { semaphore_wait(&sem); } + void signal() { semaphore_signal(&sem); } +}; + +class lock +{ + friend class scope_lock; + + LOCK var; + + void take() { lock_wait(var); } + void release() { lock_release(var); } + +public: + lock() + { + var = lock_create(); + } + + ~lock() + { + lock_destroy(var); + } +}; + +class scope_lock +{ + lock *var; +public: + scope_lock(lock *l) + { + var = l; + var->take(); + } + + ~scope_lock() + { + var->release(); + } +}; -- cgit 1.4.1 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/system.c | 15 ++++++++++ src/base/system.h | 17 ++++++++++++ src/base/tl/threading.h | 74 ++++++++++++++++++++++++++++++++++--------------- 3 files changed, 83 insertions(+), 23 deletions(-) (limited to 'src/base') 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 + 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 -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