diff options
| author | Magnus Auvinen <magnus.auvinen@gmail.com> | 2011-12-30 16:02:22 +0100 |
|---|---|---|
| committer | Magnus Auvinen <magnus.auvinen@gmail.com> | 2011-12-30 16:02:22 +0100 |
| commit | 8ffe5826156d07b1feb7fc58bf59a1431e01160f (patch) | |
| tree | a2a0a536538e725b68741b516e80f3e0a73999c5 /src/base | |
| parent | 6e20c32859ee4518f398a12b65586463ddeecaab (diff) | |
| download | zcatch-8ffe5826156d07b1feb7fc58bf59a1431e01160f.tar.gz zcatch-8ffe5826156d07b1feb7fc58bf59a1431e01160f.zip | |
ugly incomplete hack to put the rendering into another thread so we don't have to wait for the flip
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/tl/threading.h | 78 |
1 files changed, 78 insertions, 0 deletions
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 <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); } + +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(); + } +}; |