From cbd6d16338cb38937abedf67aa18a161f71e6382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 11 Mar 2018 18:30:43 -0700 Subject: [PATCH] Cleanup. --- include/bx/config.h | 1 - src/mutex.cpp | 40 +++++++++++++++++++++------- src/semaphore.cpp | 35 ++++++++++++++++++++----- src/thread.cpp | 64 ++++++++++++++++++++++++++++++++++++--------- 4 files changed, 112 insertions(+), 28 deletions(-) diff --git a/include/bx/config.h b/include/bx/config.h index 740db63..787859d 100644 --- a/include/bx/config.h +++ b/include/bx/config.h @@ -15,7 +15,6 @@ #ifndef BX_CONFIG_SUPPORTS_THREADING # define BX_CONFIG_SUPPORTS_THREADING !(0 \ || BX_PLATFORM_EMSCRIPTEN \ - || BX_CRT_NONE \ ) #endif // BX_CONFIG_SUPPORTS_THREADING diff --git a/src/mutex.cpp b/src/mutex.cpp index e144985..4eceb64 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -8,7 +8,8 @@ #if BX_CONFIG_SUPPORTS_THREADING -#if BX_PLATFORM_ANDROID \ +#if BX_CRT_NONE +#elif BX_PLATFORM_ANDROID \ || BX_PLATFORM_LINUX \ || BX_PLATFORM_IOS \ || BX_PLATFORM_OSX \ @@ -24,7 +25,27 @@ namespace bx { -#if BX_PLATFORM_WINDOWS \ +#if BX_CRT_NONE + Mutex::Mutex() + { + BX_STATIC_ASSERT(sizeof(pthread_mutex_t) <= sizeof(m_internal) ); + } + + Mutex::~Mutex() + { + } + + void Mutex::lock() + { + } + + void Mutex::unlock() + { + } + +#else + +# if BX_PLATFORM_WINDOWS \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT typedef CRITICAL_SECTION pthread_mutex_t; @@ -49,11 +70,11 @@ namespace bx inline int pthread_mutex_init(pthread_mutex_t* _mutex, pthread_mutexattr_t* /*_attr*/) { -#if BX_PLATFORM_WINRT +# if BX_PLATFORM_WINRT InitializeCriticalSectionEx(_mutex, 4000, 0); // docs recommend 4000 spincount as sane default -#else +# else InitializeCriticalSection(_mutex); -#endif // BX_PLATFORM_ +# endif // BX_PLATFORM_ return 0; } @@ -62,7 +83,7 @@ namespace bx DeleteCriticalSection(_mutex); return 0; } -#endif // BX_PLATFORM_ +# endif // BX_PLATFORM_ Mutex::Mutex() { @@ -70,13 +91,13 @@ namespace bx pthread_mutexattr_t attr; -#if BX_PLATFORM_WINDOWS \ +# if BX_PLATFORM_WINDOWS \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT -#else +# else pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); -#endif // BX_PLATFORM_ +# endif // BX_PLATFORM_ pthread_mutex_t* handle = (pthread_mutex_t*)m_internal; pthread_mutex_init(handle, &attr); @@ -99,6 +120,7 @@ namespace bx pthread_mutex_t* handle = (pthread_mutex_t*)m_internal; pthread_mutex_unlock(handle); } +#endif // BX_CRT_NONE } // namespace bx diff --git a/src/semaphore.cpp b/src/semaphore.cpp index 756f41e..3663fe7 100644 --- a/src/semaphore.cpp +++ b/src/semaphore.cpp @@ -8,8 +8,9 @@ #if BX_CONFIG_SUPPORTS_THREADING -#if BX_PLATFORM_OSX \ -|| BX_PLATFORM_IOS +#if BX_CRT_NONE +#elif BX_PLATFORM_OSX \ + || BX_PLATFORM_IOS # include #elif BX_PLATFORM_POSIX # include @@ -30,8 +31,10 @@ namespace bx { struct SemaphoreInternal { -#if BX_PLATFORM_OSX \ -|| BX_PLATFORM_IOS +#if BX_CRT_NONE + +#elif BX_PLATFORM_OSX \ + || BX_PLATFORM_IOS dispatch_semaphore_t m_handle; #elif BX_PLATFORM_POSIX pthread_mutex_t m_mutex; @@ -44,8 +47,28 @@ namespace bx #endif // BX_PLATFORM_ }; -#if BX_PLATFORM_OSX \ -|| BX_PLATFORM_IOS +#if BX_CRT_NONE + Semaphore::Semaphore() + { + BX_STATIC_ASSERT(sizeof(SemaphoreInternal) <= sizeof(m_internal) ); + } + + Semaphore::~Semaphore() + { + } + + void Semaphore::post(uint32_t _count) + { + BX_UNUSED(_count); + } + + bool Semaphore::wait(int32_t _msecs) + { + BX_UNUSED(_msecs); + return false; + } +#elif BX_PLATFORM_OSX \ + || BX_PLATFORM_IOS Semaphore::Semaphore() { diff --git a/src/thread.cpp b/src/thread.cpp index 8607fe4..08dde43 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -6,7 +6,10 @@ #include "bx_p.h" #include -#if BX_PLATFORM_ANDROID \ +#if BX_CONFIG_SUPPORTS_THREADING + +#if BX_CRT_NONE +#elif BX_PLATFORM_ANDROID \ || BX_PLATFORM_LINUX \ || BX_PLATFORM_IOS \ || BX_PLATFORM_OSX \ @@ -32,8 +35,6 @@ using namespace Windows::System::Threading; # endif // BX_PLATFORM_WINRT #endif // BX_PLATFORM_ -#if BX_CONFIG_SUPPORTS_THREADING - namespace bx { static AllocatorI* getAllocator() @@ -44,7 +45,9 @@ namespace bx struct ThreadInternal { -#if BX_PLATFORM_WINDOWS \ +#if BX_CRT_NONE + static void* threadFunc(void* _arg); +#elif BX_PLATFORM_WINDOWS \ || BX_PLATFORM_WINRT \ || BX_PLATFORM_XBOXONE static DWORD WINAPI threadFunc(LPVOID _arg); @@ -90,7 +93,9 @@ namespace bx BX_STATIC_ASSERT(sizeof(ThreadInternal) <= sizeof(m_internal) ); ThreadInternal* ti = (ThreadInternal*)m_internal; -#if BX_PLATFORM_WINDOWS \ +#if BX_CRT_NONE + BX_UNUSED(ti); +#elif BX_PLATFORM_WINDOWS \ || BX_PLATFORM_WINRT \ || BX_PLATFORM_XBOXONE ti->m_handle = INVALID_HANDLE_VALUE; @@ -118,7 +123,9 @@ namespace bx m_running = true; ThreadInternal* ti = (ThreadInternal*)m_internal; -#if BX_PLATFORM_WINDOWS \ +#if BX_CRT_NONE + BX_UNUSED(ti); +#elif BX_PLATFORM_WINDOWS \ || BX_PLATFORM_XBOXONE ti->m_handle = ::CreateThread(NULL , m_stackSize @@ -170,7 +177,9 @@ namespace bx { BX_CHECK(m_running, "Not running!"); ThreadInternal* ti = (ThreadInternal*)m_internal; -#if BX_PLATFORM_WINDOWS +#if BX_CRT_NONE + BX_UNUSED(ti); +#elif BX_PLATFORM_WINDOWS WaitForSingleObject(ti->m_handle, INFINITE); GetExitCodeThread(ti->m_handle, (DWORD*)&m_exitCode); CloseHandle(ti->m_handle); @@ -207,18 +216,21 @@ namespace bx { ThreadInternal* ti = (ThreadInternal*)m_internal; BX_UNUSED(ti); -#if BX_PLATFORM_OSX || BX_PLATFORM_IOS +#if BX_CRT_NONE + BX_UNUSED(_name); +#elif BX_PLATFORM_OSX \ + || BX_PLATFORM_IOS pthread_setname_np(_name); #elif (BX_CRT_GLIBC >= 21200) && ! BX_PLATFORM_HURD pthread_setname_np(ti->m_handle, _name); #elif BX_PLATFORM_LINUX prctl(PR_SET_NAME,_name, 0, 0, 0); #elif BX_PLATFORM_BSD -# ifdef __NetBSD__ +# if defined(__NetBSD__) pthread_setname_np(ti->m_handle, "%s", (void*)_name); # else pthread_set_name_np(ti->m_handle, _name); -# endif // __NetBSD__ +# endif // defined(__NetBSD__) #elif BX_PLATFORM_WINDOWS && BX_COMPILER_MSVC # pragma pack(push, 8) struct ThreadName @@ -276,14 +288,42 @@ namespace bx struct TlsDataInternal { -#if BX_PLATFORM_WINDOWS +#if BX_CRT_NONE +#elif BX_PLATFORM_WINDOWS uint32_t m_id; #elif !(BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT) pthread_key_t m_id; #endif // BX_PLATFORM_* }; -#if BX_PLATFORM_WINDOWS +#if BX_CRT_NONE + TlsData::TlsData() + { + BX_STATIC_ASSERT(sizeof(TlsDataInternal) <= sizeof(m_internal) ); + + TlsDataInternal* ti = (TlsDataInternal*)m_internal; + BX_UNUSED(ti); + } + + TlsData::~TlsData() + { + TlsDataInternal* ti = (TlsDataInternal*)m_internal; + BX_UNUSED(ti); + } + + void* TlsData::get() const + { + return NULL; + } + + void TlsData::set(void* _ptr) + { + BX_UNUSED(_ptr); + + TlsDataInternal* ti = (TlsDataInternal*)m_internal; + BX_UNUSED(ti); + } +#elif BX_PLATFORM_WINDOWS TlsData::TlsData() { BX_STATIC_ASSERT(sizeof(TlsDataInternal) <= sizeof(m_internal) );