Skip to content

Commit

Permalink
Threading: Add IsCallingThread() to ThreadHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Dec 6, 2024
1 parent 5c4d95f commit 042a2d7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/common/threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "log.h"

#include <memory>
#include <utility>

#if !defined(_WIN32) && !defined(__APPLE__)
#ifndef _GNU_SOURCE
Expand Down Expand Up @@ -164,8 +165,9 @@ Threading::ThreadHandle Threading::ThreadHandle::GetForCallingThread()
{
ThreadHandle ret;
#ifdef _WIN32
ret.m_native_id = GetCurrentThreadId();
ret.m_native_handle =
(void*)OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_LIMITED_INFORMATION, FALSE, GetCurrentThreadId());
(void*)OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_LIMITED_INFORMATION, FALSE, ret.m_native_id);
#else
ret.m_native_handle = (void*)pthread_self();
#ifdef __linux__
Expand All @@ -181,7 +183,9 @@ Threading::ThreadHandle& Threading::ThreadHandle::operator=(ThreadHandle&& handl
if (m_native_handle)
CloseHandle((HANDLE)m_native_handle);
m_native_handle = handle.m_native_handle;
m_native_id = handle.m_native_id;
handle.m_native_handle = nullptr;
handle.m_native_id = 0;
#else
m_native_handle = handle.m_native_handle;
handle.m_native_handle = nullptr;
Expand All @@ -207,6 +211,12 @@ Threading::ThreadHandle& Threading::ThreadHandle::operator=(const ThreadHandle&
THREAD_QUERY_INFORMATION | THREAD_SET_LIMITED_INFORMATION, FALSE, 0))
{
m_native_handle = (void*)new_handle;
m_native_id = handle.m_native_id;
}
else
{
m_native_handle = nullptr;
m_native_id = 0;
}
#else
m_native_handle = handle.m_native_handle;
Expand Down Expand Up @@ -275,6 +285,15 @@ bool Threading::ThreadHandle::SetAffinity(u64 processor_mask) const
#endif
}

bool Threading::ThreadHandle::IsCallingThread() const
{
#ifdef _WIN32
return (GetCurrentThreadId() == m_native_id);
#else
return pthread_equal(pthread_self(), (pthread_t)m_native_handle);
#endif
}

#ifdef __APPLE__

bool Threading::ThreadHandle::SetTimeConstraints(bool enabled, u64 period, u64 typical_time, u64 maximum_time)
Expand Down Expand Up @@ -317,9 +336,9 @@ bool Threading::ThreadHandle::SetTimeConstraints(bool enabled, u64 period, u64 t

Threading::Thread::Thread() = default;

Threading::Thread::Thread(Thread&& thread) : ThreadHandle(thread), m_stack_size(thread.m_stack_size)
Threading::Thread::Thread(Thread&& thread) : ThreadHandle(thread)
{
thread.m_stack_size = 0;
m_stack_size = std::exchange(thread.m_stack_size, 0);
}

Threading::Thread::Thread(EntryPoint func) : ThreadHandle()
Expand Down
9 changes: 8 additions & 1 deletion src/common/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class ThreadHandle
/// Obviously, only works up to 64 processors.
bool SetAffinity(u64 processor_mask) const;

/// Returns true if the calling thread matches this handle.
bool IsCallingThread() const;

#ifdef __APPLE__
/// Only available on MacOS, sets a period/maximum time for the scheduler.
bool SetTimeConstraints(bool enabled, u64 period, u64 typical_time, u64 maximum_time);
Expand All @@ -62,8 +65,9 @@ class ThreadHandle
void* m_native_handle = nullptr;

// We need the thread ID for affinity adjustments on Linux.
#if defined(__linux__)
#if defined(_WIN32) || defined(__linux__)
unsigned int m_native_id = 0;
u32 m_stack_size = 0;
#endif
};

Expand Down Expand Up @@ -104,7 +108,10 @@ class Thread : public ThreadHandle
static void* ThreadProc(void* param);
#endif

#if !defined(_WIN32) && !defined(__linux__)
// Stored in ThreadHandle to save 8 bytes.
u32 m_stack_size = 0;
#endif
};

/// A semaphore that requires a system call to wake/sleep.
Expand Down

0 comments on commit 042a2d7

Please sign in to comment.