From c16c4ee4d9f84745cf2169bce129fa985b01fc9d Mon Sep 17 00:00:00 2001 From: oy Date: Sun, 10 Jun 2012 12:14:41 +0200 Subject: [PATCH] fixed semaphore on macosx --- src/base/system.c | 26 ++++++++++++++------------ src/base/system.h | 26 ++++++++++++++------------ src/base/tl/threading.h | 24 +++++++++++++++--------- src/engine/client/backend_sdl.h | 10 ++++++++++ src/engine/client/client.h | 2 -- src/engine/client/graphics.cpp | 14 ++++++++++++++ 6 files changed, 67 insertions(+), 35 deletions(-) diff --git a/src/base/system.c b/src/base/system.c index d357a7c29..dc4deda2d 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -505,18 +505,20 @@ 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) { *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 +#if !defined(CONF_PLATFORM_MACOSX) + #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) { *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 #endif diff --git a/src/base/system.h b/src/base/system.h index b3588dbf1..032cf7857 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -403,19 +403,21 @@ 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 +#if !defined(CONF_PLATFORM_MACOSX) + #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); + void semaphore_init(SEMAPHORE *sem); + void semaphore_wait(SEMAPHORE *sem); + void semaphore_signal(SEMAPHORE *sem); + void semaphore_destroy(SEMAPHORE *sem); +#endif /* Group: Timer */ #ifdef __GNUC__ diff --git a/src/base/tl/threading.h b/src/base/tl/threading.h index dbf788cdc..5caf8588b 100644 --- a/src/base/tl/threading.h +++ b/src/base/tl/threading.h @@ -58,15 +58,21 @@ #error missing atomic implementation for this compiler #endif -class semaphore -{ - SEMAPHORE sem; -public: - semaphore() { semaphore_init(&sem); } - ~semaphore() { semaphore_destroy(&sem); } - void wait() { semaphore_wait(&sem); } - void signal() { semaphore_signal(&sem); } -}; +#if defined(CONF_PLATFORM_MACOSX) + /* + use semaphore provided by SDL on macosx + */ +#else + class semaphore + { + SEMAPHORE sem; + public: + semaphore() { semaphore_init(&sem); } + ~semaphore() { semaphore_destroy(&sem); } + void wait() { semaphore_wait(&sem); } + void signal() { semaphore_signal(&sem); } + }; +#endif class lock { diff --git a/src/engine/client/backend_sdl.h b/src/engine/client/backend_sdl.h index 305453f24..2ef04a1f8 100644 --- a/src/engine/client/backend_sdl.h +++ b/src/engine/client/backend_sdl.h @@ -30,6 +30,16 @@ #include + class semaphore + { + SDL_sem *sem; + public: + semaphore() { sem = SDL_CreateSemaphore(0); } + ~semaphore() { SDL_DestroySemaphore(sem); } + void wait() { SDL_SemWait(sem); } + void signal() { SDL_SemPost(sem); } + }; + struct SGLContext { AGLContext m_Context; diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 16fd8ab2c..07718a017 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -177,8 +177,6 @@ class CClient : public IClient, public CDemoPlayer::IListner class CHostLookup m_VersionServeraddr; } m_VersionInfo; - semaphore m_GfxRenderSemaphore; - semaphore m_GfxStateSemaphore; volatile int m_GfxState; static void GraphicsThreadProxy(void *pThis) { ((CClient*)pThis)->GraphicsThread(); } void GraphicsThread(); diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index bf432356d..8816e1eda 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -22,6 +22,20 @@ #include "graphics.h" +#if defined(CONF_PLATFORM_MACOSX) + + class semaphore + { + SDL_sem *sem; + public: + semaphore() { sem = SDL_CreateSemaphore(0); } + ~semaphore() { SDL_DestroySemaphore(sem); } + void wait() { SDL_SemWait(sem); } + void signal() { SDL_SemPost(sem); } + }; +#endif + + static CVideoMode g_aFakeModes[] = { {320,240,8,8,8}, {400,300,8,8,8}, {640,480,8,8,8}, {720,400,8,8,8}, {768,576,8,8,8}, {800,600,8,8,8},