Skip to content

Commit

Permalink
Fixed build
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Nov 30, 2024
1 parent 4584bdc commit c98f9a6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 62 deletions.
58 changes: 46 additions & 12 deletions Sources/nCine/Threading/PosixThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

#include "../../Common.h"

#include <unistd.h> // for sysconf()
#include <sched.h> // for sched_yield()
#include <atomic>
#include <cstring>
#include <pthread.h>
#include <sched.h> // for sched_yield()
#include <unistd.h> // for sysconf()
#include <utility>

#if defined(DEATH_TARGET_APPLE)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -259,47 +293,47 @@ 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<thread_policy_t>(&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;
}

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<thread_policy_t>(&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)
Expand Down
54 changes: 9 additions & 45 deletions Sources/nCine/Threading/Thread.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
#pragma once

#include "Atomic.h"

#include <CommonWindows.h>

#if defined(DEATH_TARGET_WINDOWS)
# include <process.h>
# include <processthreadsapi.h>
#else
# include <pthread.h>
#endif
#if defined(WITH_THREADS)

#if defined(DEATH_TARGET_APPLE)
# include <mach/mach_init.h>
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -181,4 +143,6 @@ namespace nCine
static void* WrapperFunction(void* arg);
#endif
};
}
}

#endif
43 changes: 38 additions & 5 deletions Sources/nCine/Threading/WindowsThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "../../Common.h"
#include "../tracy.h"

#include <atomic>
#include <utility>
#include <process.h>
#include <processthreadsapi.h>

#include <Utf8.h>

Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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");
}
}

Expand Down

0 comments on commit c98f9a6

Please sign in to comment.