From 3fc639a235c70a65dfa7863ef2564461e4c62e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Sun, 17 May 2020 10:44:33 -0700 Subject: [PATCH] Cleanup. --- include/bx/thread.h | 10 +++++++++- src/thread.cpp | 40 +++++++++++++++++++++++++++++++++++----- tests/thread_test.cpp | 4 +++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/include/bx/thread.h b/include/bx/thread.h index 9571dd5..1ac3701 100644 --- a/include/bx/thread.h +++ b/include/bx/thread.h @@ -31,8 +31,16 @@ namespace bx /// virtual ~Thread(); + /// Create and initialize thread. /// - void init(ThreadFn _fn, void* _userData = NULL, uint32_t _stackSize = 0, const char* _name = NULL); + /// @param[in] _fn Thread function. + /// @param[in] _userData User data passed to thread function. + /// @param[in] _stackSize Stack size, if zero is passed it will use OS default thread stack + /// size. + /// @param[in] _name Thread name used by debugger. + /// @returns True if thread is created, otherwise returns false. + /// + bool init(ThreadFn _fn, void* _userData = NULL, uint32_t _stackSize = 0, const char* _name = NULL); /// void shutdown(); diff --git a/src/thread.cpp b/src/thread.cpp index 9f4bff2..e18602a 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -128,18 +128,22 @@ namespace bx } } - void Thread::init(ThreadFn _fn, void* _userData, uint32_t _stackSize, const char* _name) + bool Thread::init(ThreadFn _fn, void* _userData, uint32_t _stackSize, const char* _name) { BX_CHECK(!m_running, "Already running!"); m_fn = _fn; m_userData = _userData; m_stackSize = _stackSize; - m_running = true; ThreadInternal* ti = (ThreadInternal*)m_internal; #if BX_CRT_NONE ti->m_handle = crt0::threadCreate(&ti->threadFunc, _userData, m_stackSize, _name); + + if (NULL == ti->m_handle) + { + return false; + } #elif BX_PLATFORM_WINDOWS \ || BX_PLATFORM_XBOXONE ti->m_handle = ::CreateThread(NULL @@ -149,8 +153,18 @@ namespace bx , 0 , NULL ); + if (NULL == ti->m_handle) + { + return false; + } #elif BX_PLATFORM_WINRT ti->m_handle = CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS); + + if (NULL == ti->m_handle) + { + return false; + } + auto workItemHandler = ref new WorkItemHandler([=](IAsyncAction^) { m_exitCode = ti->threadFunc(this); @@ -166,26 +180,42 @@ namespace bx pthread_attr_t attr; result = pthread_attr_init(&attr); - BX_CHECK(0 == result, "pthread_attr_init failed! %d", result); + BX_WARN(0 == result, "pthread_attr_init failed! %d", result); + if (0 != result) + { + return false; + } if (0 != m_stackSize) { result = pthread_attr_setstacksize(&attr, m_stackSize); - BX_CHECK(0 == result, "pthread_attr_setstacksize failed! %d", result); + BX_WARN(0 == result, "pthread_attr_setstacksize failed! %d", result); + + if (0 != result) + { + return false; + } } result = pthread_create(&ti->m_handle, &attr, &ti->threadFunc, this); - BX_CHECK(0 == result, "pthread_attr_setschedparam failed! %d", result); + BX_WARN(0 == result, "pthread_attr_setschedparam failed! %d", result); + if (0 != result) + { + return false; + } #else # error "Not implemented!" #endif // BX_PLATFORM_ + m_running = true; m_sem.wait(); if (NULL != _name) { setThreadName(_name); } + + return true; } void Thread::shutdown() diff --git a/tests/thread_test.cpp b/tests/thread_test.cpp index 9556a9f..fbd906c 100644 --- a/tests/thread_test.cpp +++ b/tests/thread_test.cpp @@ -42,7 +42,9 @@ TEST_CASE("Thread", "") REQUIRE(!th.isRunning() ); - th.init(threadExit0); + bool init = th.init(threadExit0, NULL, 0, NULL); + REQUIRE(init); + REQUIRE(th.isRunning() ); th.push(NULL); th.shutdown();