Files
parallel-gx1/Source/Core/Common/Thread.cpp
T
LibretroAdmin 2dd80c5c2e VideoCommon: shader compiler workers below the emulation threads
The shader compiler's worker threads run at a lower priority than the
threads that emulate, on Windows (below normal) and Linux (nice 5): on
a machine with fewer cores than busy threads, what compiles in the
background waits, not the frame being emulated. Common gets
LowerCurrentThreadPriority for it.
2026-11-06 23:45:19 +01:00

111 lines
2.2 KiB
C++

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Common/Thread.h"
#ifdef _WIN32
#include <windows.h>
#include <processthreadsapi.h>
#else
#include <pthread.h>
#include <unistd.h>
#endif
#ifdef __linux__
#include <sys/resource.h>
#include <sys/syscall.h>
#endif
#ifdef __APPLE__
#include <mach/mach.h>
#elif defined BSD4_4 || defined __FreeBSD__ || defined __OpenBSD__
#include <pthread_np.h>
#elif defined __NetBSD__
#include <sched.h>
#elif defined __HAIKU__
#include <OS.h>
#endif
#include <rthreads/rthreads.h>
#include "Common/CommonTypes.h"
namespace Common
{
int CurrentThreadId()
{
#ifdef _WIN32
return GetCurrentThreadId();
#elif defined __APPLE__
return mach_thread_self();
#else
return 0;
#endif
}
void SetCurrentThreadName(const char* name)
{
sthread_setname(name);
}
void LowerCurrentThreadPriority()
{
#ifdef _WIN32
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
#elif defined(__linux__)
/* Linux keeps a nice value per thread: this one only */
setpriority(PRIO_PROCESS, (id_t)syscall(SYS_gettid), 5);
#endif
}
#ifndef _WIN32
/* A thread's stack does not move: asked once per thread, as glibc's
* pthread_getattr_np allocates. */
static thread_local void* s_stack_addr;
static thread_local size_t s_stack_size;
std::tuple<void*, size_t> GetCurrentThreadStack()
{
void* stack_addr;
size_t stack_size;
if (s_stack_size)
return std::make_tuple(s_stack_addr, s_stack_size);
pthread_t self = pthread_self();
#ifdef __APPLE__
stack_size = pthread_get_stacksize_np(self);
stack_addr = reinterpret_cast<u8*>(pthread_get_stackaddr_np(self)) - stack_size;
#elif defined __OpenBSD__
stack_t stack;
pthread_stackseg_np(self, &stack);
stack_addr = reinterpret_cast<u8*>(stack.ss_sp) - stack.ss_size;
stack_size = stack.ss_size;
#else
pthread_attr_t attr;
#ifdef __FreeBSD__
pthread_attr_init(&attr);
pthread_attr_get_np(self, &attr);
#else
// Linux and NetBSD
pthread_getattr_np(self, &attr);
#endif
pthread_attr_getstack(&attr, &stack_addr, &stack_size);
pthread_attr_destroy(&attr);
#endif
s_stack_addr = stack_addr;
s_stack_size = stack_size;
return std::make_tuple(stack_addr, stack_size);
}
#endif
} // namespace Common