From e84ced5ab1eb7665a71163c846107e9100372e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 14 Nov 2017 16:53:22 -0800 Subject: [PATCH] OSX/iOS: Fixed semaphore timed wait. --- src/bx_p.h | 2 +- src/semaphore.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++----- tests/os_test.cpp | 2 -- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/bx_p.h b/src/bx_p.h index df37a2c..81580e1 100644 --- a/src/bx_p.h +++ b/src/bx_p.h @@ -7,7 +7,7 @@ #define BX_P_H_HEADER_GUARD #ifndef BX_CONFIG_DEBUG -# define BX_CONFIG_DEBUG 0 +# define BX_CONFIG_DEBUG 1 #endif // BX_CONFIG_DEBUG #if BX_CONFIG_DEBUG diff --git a/src/semaphore.cpp b/src/semaphore.cpp index a5bab58..bd2aca8 100644 --- a/src/semaphore.cpp +++ b/src/semaphore.cpp @@ -8,7 +8,10 @@ #if BX_CONFIG_SUPPORTS_THREADING -#if BX_PLATFORM_POSIX +#if BX_PLATFORM_OSX \ +|| BX_PLATFORM_IOS +# include +#elif BX_PLATFORM_POSIX # include # include # include @@ -26,8 +29,6 @@ #ifndef BX_CONFIG_SEMAPHORE_PTHREAD # define BX_CONFIG_SEMAPHORE_PTHREAD (0 \ || BX_PLATFORM_LINUX \ - || BX_PLATFORM_OSX \ - || BX_PLATFORM_IOS \ ) #endif // BX_CONFIG_SEMAPHORE_PTHREAD @@ -35,7 +36,10 @@ namespace bx { struct SemaphoreInternal { -#if BX_PLATFORM_POSIX +#if BX_PLATFORM_OSX \ +|| BX_PLATFORM_IOS + dispatch_semaphore_t m_handle; +#elif BX_PLATFORM_POSIX # if BX_CONFIG_SEMAPHORE_PTHREAD pthread_mutex_t m_mutex; pthread_cond_t m_cond; @@ -137,7 +141,7 @@ namespace bx # if BX_PLATFORM_OSX BX_UNUSED(_msecs); - BX_CHECK(-1 == _msecs, "OSX doesn't support pthread_cond_timedwait at this moment."); +// BX_CHECK(-1 == _msecs, "OSX doesn't support pthread_cond_timedwait at this moment."); while (0 == result && 0 >= si->m_count) { @@ -190,6 +194,45 @@ namespace bx return ok; } +# elif BX_PLATFORM_OSX \ + || BX_PLATFORM_IOS + + Semaphore::Semaphore() + { + BX_STATIC_ASSERT(sizeof(SemaphoreInternal) <= sizeof(m_internal) ); + + SemaphoreInternal* si = (SemaphoreInternal*)m_internal; + si->m_handle = dispatch_semaphore_create(0); + BX_CHECK(NULL != si->m_handle, "dispatch_semaphore_create failed."); + } + + Semaphore::~Semaphore() + { + SemaphoreInternal* si = (SemaphoreInternal*)m_internal; + dispatch_release(si->m_handle); + } + + void Semaphore::post(uint32_t _count) + { + SemaphoreInternal* si = (SemaphoreInternal*)m_internal; + + for (uint32_t ii = 0; ii < _count; ++ii) + { + dispatch_semaphore_signal(si->m_handle); + } + } + + bool Semaphore::wait(int32_t _msecs) + { + SemaphoreInternal* si = (SemaphoreInternal*)m_internal; + + dispatch_time_t dt = 0 > _msecs + ? DISPATCH_TIME_FOREVER + : dispatch_time(DISPATCH_TIME_NOW, _msecs*1000000) + ; + return !dispatch_semaphore_wait(si->m_handle, dt); + } + # else Semaphore::Semaphore() diff --git a/tests/os_test.cpp b/tests/os_test.cpp index ed60e3e..1589416 100644 --- a/tests/os_test.cpp +++ b/tests/os_test.cpp @@ -14,7 +14,6 @@ TEST_CASE("getProcessMemoryUsed", "") // DBG("bx::getProcessMemoryUsed %d", bx::getProcessMemoryUsed() ); } -#if !BX_PLATFORM_OSX TEST_CASE("semaphore_timeout", "") { bx::Semaphore sem; @@ -27,4 +26,3 @@ TEST_CASE("semaphore_timeout", "") printf("%f\n", ms); REQUIRE(!ok); } -#endif // !BX_PLATFORM_OSX