From 29cb5058c0b05cca8ebeb40d84aba8a8d2e11075 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 16 Apr 2014 01:39:11 -0700 Subject: Use a C11-like mutex wrapper instead of CRITICAL_SECTIONs --- OpenAL32/Include/threads.h | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'OpenAL32/Include/threads.h') diff --git a/OpenAL32/Include/threads.h b/OpenAL32/Include/threads.h index 26493101..03589d15 100644 --- a/OpenAL32/Include/threads.h +++ b/OpenAL32/Include/threads.h @@ -11,4 +11,90 @@ ALuint StopThread(althread_t thread); void SetThreadName(const char *name); + +enum { + althrd_success = 0, + althrd_timeout, + althrd_error, + althrd_busy, + althrd_nomem +}; + +enum { + almtx_plain = 0, + almtx_recursive = 1, + almtx_timed = 2, + almtx_normal = 4, + almtx_errorcheck = 8 +}; + +typedef struct alxtime { + time_t sec; + long nsec; +} alxtime; + + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include + +typedef CRITICAL_SECTION almtx_t; + +inline int almtx_lock(almtx_t *mtx) +{ + if(!mtx) return althrd_error; + EnterCriticalSection(mtx); + return althrd_success; +} + +inline int almtx_unlock(almtx_t *mtx) +{ + if(!mtx) return althrd_error; + LeaveCriticalSection(mtx); + return althrd_success; +} + +inline int almtx_trylock(almtx_t *mtx) +{ + if(!mtx) return althrd_error; + if(!TryEnterCriticalSection(mtx)) + return althrd_busy; + return althrd_success; +} + +#else + +#include + + +typedef pthread_mutex_t almtx_t; + +inline int almtx_lock(almtx_t *mtx) +{ + if(!mtx) return althrd_error; + pthread_mutex_lock(mtx); + return althrd_success; +} + +inline int almtx_unlock(almtx_t *mtx) +{ + if(!mtx) return althrd_error; + pthread_mutex_unlock(mtx); + return althrd_success; +} + +inline int almtx_trylock(almtx_t *mtx) +{ + if(!mtx) return althrd_error; + if(pthread_mutex_trylock(mtx) != 0) + return althrd_busy; + return althrd_success; +} + +#endif + +int almtx_init(almtx_t *mtx, int type); +void almtx_destroy(almtx_t *mtx); +int almtx_timedlock(almtx_t *mtx, const alxtime *xt); + #endif /* AL_THREADS_H */ -- cgit v1.2.3