From c98f9a6b34ac46e4545cc6e6dc2c7ade9e86f44c Mon Sep 17 00:00:00 2001 From: Death Killer <884052+deathkiller@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:00:31 +0100 Subject: [PATCH] Fixed build --- Sources/nCine/Threading/PosixThread.cpp | 58 ++++++++++++++++++----- Sources/nCine/Threading/Thread.h | 54 ++++----------------- Sources/nCine/Threading/WindowsThread.cpp | 43 +++++++++++++++-- 3 files changed, 93 insertions(+), 62 deletions(-) diff --git a/Sources/nCine/Threading/PosixThread.cpp b/Sources/nCine/Threading/PosixThread.cpp index 515b0aa9..51b58eff 100644 --- a/Sources/nCine/Threading/PosixThread.cpp +++ b/Sources/nCine/Threading/PosixThread.cpp @@ -6,9 +6,11 @@ #include "../../Common.h" -#include // for sysconf() -#include // for sched_yield() +#include #include +#include +#include // for sched_yield() +#include // for sysconf() #include #if defined(DEATH_TARGET_APPLE) @@ -75,6 +77,14 @@ namespace nCine #endif + struct Thread::SharedBlock + { + std::atomic_int32_t _refCount; + pthread_t _handle; + ThreadFuncDelegate _threadFunc; + void* _threadArg; + }; + Thread::Thread() : _sharedBlock(nullptr) { @@ -96,6 +106,30 @@ namespace nCine Detach(); } + Thread::Thread(const Thread& other) + { + // Copy constructor + _sharedBlock = other._sharedBlock; + + if (_sharedBlock != nullptr) { + ++_sharedBlock->_refCount; + } + } + + Thread& Thread::operator=(const Thread& other) + { + Detach(); + + // Copy assignment + _sharedBlock = other._sharedBlock; + + if (_sharedBlock != nullptr) { + ++_sharedBlock->_refCount; + } + + return *this; + } + std::uint32_t Thread::GetProcessorCount() { #if defined(DEATH_TARGET_SWITCH) @@ -259,27 +293,28 @@ namespace nCine return (pthread_cancel(_sharedBlock->_handle) == 0); } +#endif -# if !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) +#if !defined(DEATH_TARGET_ANDROID) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) ThreadAffinityMask Thread::GetAffinityMask() const { ThreadAffinityMask affinityMask; if (_sharedBlock == nullptr || _sharedBlock->_handle == 0) { - LOGW("Cannot get the affinity for a thread that has not been created yet"); + LOGW("Can't get the affinity for a thread that has not been created yet"); return affinityMask; } -# if defined(DEATH_TARGET_APPLE) +# if defined(DEATH_TARGET_APPLE) thread_affinity_policy_data_t threadAffinityPolicy; thread_port_t threadPort = pthread_mach_thread_np(_sharedBlock->_handle); mach_msg_type_number_t policyCount = THREAD_AFFINITY_POLICY_COUNT; boolean_t getDefault = FALSE; thread_policy_get(threadPort, THREAD_AFFINITY_POLICY, reinterpret_cast(&threadAffinityPolicy), &policyCount, &getDefault); affinityMask.affinityTag_ = threadAffinityPolicy.affinity_tag; -# else +# else pthread_getaffinity_np(_sharedBlock->_handle, sizeof(cpu_set_t), &affinityMask.cpuSet_); -# endif +# endif return affinityMask; } @@ -287,19 +322,18 @@ namespace nCine void Thread::SetAffinityMask(ThreadAffinityMask affinityMask) { if (_sharedBlock == nullptr || _sharedBlock->_handle == 0) { - LOGW("Cannot set the affinity mask for a not yet created thread"); + LOGW("Can't set the affinity mask for a not yet created thread"); return; } -# if defined(DEATH_TARGET_APPLE) +# if defined(DEATH_TARGET_APPLE) thread_affinity_policy_data_t threadAffinityPolicy = { affinityMask.affinityTag_ }; thread_port_t threadPort = pthread_mach_thread_np(_sharedBlock->_handle); thread_policy_set(threadPort, THREAD_AFFINITY_POLICY, reinterpret_cast(&threadAffinityPolicy), THREAD_AFFINITY_POLICY_COUNT); -# else +# else pthread_setaffinity_np(_sharedBlock->_handle, sizeof(cpu_set_t), &affinityMask.cpuSet_); -# endif - } # endif + } #endif void* Thread::WrapperFunction(void* arg) diff --git a/Sources/nCine/Threading/Thread.h b/Sources/nCine/Threading/Thread.h index aeb88414..7dbff411 100644 --- a/Sources/nCine/Threading/Thread.h +++ b/Sources/nCine/Threading/Thread.h @@ -1,15 +1,8 @@ #pragma once -#include "Atomic.h" - #include -#if defined(DEATH_TARGET_WINDOWS) -# include -# include -#else -# include -#endif +#if defined(WITH_THREADS) #if defined(DEATH_TARGET_APPLE) # include @@ -70,29 +63,8 @@ namespace nCine ~Thread(); - Thread(const Thread& other) - { - // Copy constructor - _sharedBlock = other._sharedBlock; - - if (_sharedBlock != nullptr) { - _sharedBlock->_refCount.fetchAdd(1); - } - } - - Thread& operator=(const Thread& other) - { - Detach(); - - // Copy assignment - _sharedBlock = other._sharedBlock; - - if (_sharedBlock != nullptr) { - _sharedBlock->_refCount.fetchAdd(1); - } - - return *this; - } + Thread(const Thread& other); + Thread& operator=(const Thread& other); Thread(Thread&& other) noexcept { @@ -145,27 +117,17 @@ namespace nCine #if !defined(DEATH_TARGET_ANDROID) /** @brief Tries to cancel or terminate the thread (depending on operating system) */ bool Abort(); +#endif -# if !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) +#if !defined(DEATH_TARGET_ANDROID) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) /** @brief Gets the thread affinity mask */ ThreadAffinityMask GetAffinityMask() const; /** @brief Sets the thread affinity mask*/ void SetAffinityMask(ThreadAffinityMask affinityMask); -# endif #endif private: - struct SharedBlock - { - Atomic32 _refCount; -#if defined(DEATH_TARGET_WINDOWS) - HANDLE _handle; -#else - pthread_t _handle; -#endif - ThreadFuncDelegate _threadFunc; - void* _threadArg; - }; + struct SharedBlock; Thread(SharedBlock* sharedBlock); @@ -181,4 +143,6 @@ namespace nCine static void* WrapperFunction(void* arg); #endif }; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/Sources/nCine/Threading/WindowsThread.cpp b/Sources/nCine/Threading/WindowsThread.cpp index b058a977..6c15c8ca 100644 --- a/Sources/nCine/Threading/WindowsThread.cpp +++ b/Sources/nCine/Threading/WindowsThread.cpp @@ -7,7 +7,10 @@ #include "../../Common.h" #include "../tracy.h" +#include #include +#include +#include #include @@ -82,6 +85,14 @@ namespace nCine return ((affinityMask_ >> cpuNum) & 1LL) != 0; } + struct Thread::SharedBlock + { + std::atomic_int32_t _refCount; + HANDLE _handle; + ThreadFuncDelegate _threadFunc; + void* _threadArg; + }; + Thread::Thread() : _sharedBlock(nullptr) { @@ -103,6 +114,30 @@ namespace nCine Detach(); } + Thread::Thread(const Thread& other) + { + // Copy constructor + _sharedBlock = other._sharedBlock; + + if (_sharedBlock != nullptr) { + ++_sharedBlock->_refCount; + } + } + + Thread& Thread::operator=(const Thread& other) + { + Detach(); + + // Copy assignment + _sharedBlock = other._sharedBlock; + + if (_sharedBlock != nullptr) { + ++_sharedBlock->_refCount; + } + + return *this; + } + std::uint32_t Thread::GetProcessorCount() { SYSTEM_INFO si; @@ -141,9 +176,7 @@ namespace nCine return; } - // This returns the value before decrementing - int32_t refCount = _sharedBlock->_refCount.fetchSub(1); - if (refCount == 1) { + if (--_sharedBlock->_refCount == 0) { ::CloseHandle(_sharedBlock->_handle); delete _sharedBlock; } @@ -212,7 +245,7 @@ namespace nCine affinityMask.affinityMask_ = ::SetThreadAffinityMask(_sharedBlock->_handle, ~0); ::SetThreadAffinityMask(_sharedBlock->_handle, affinityMask.affinityMask_); } else { - LOGW("Cannot get the affinity for a thread that has not been created yet"); + LOGW("Can't get the affinity for a thread that has not been created yet"); } return affinityMask; @@ -223,7 +256,7 @@ namespace nCine if (_sharedBlock != nullptr) ::SetThreadAffinityMask(_sharedBlock->_handle, affinityMask.affinityMask_); else { - LOGW("Cannot set the affinity mask for a not yet created thread"); + LOGW("Can't set the affinity mask for a not yet created thread"); } }