1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-01-31 14:52:11 +01:00

[util] Implement thread helpers on non-Windows platforms

This commit is contained in:
Joshua Ashton 2022-04-18 00:29:07 +01:00 committed by Joshie
parent 9302d33ac7
commit 9ee0f51870
4 changed files with 50 additions and 1 deletions

View File

@ -8,6 +8,8 @@ util_src = files([
'util_monitor.cpp',
'util_shared_res.cpp',
'thread.cpp',
'com/com_guid.cpp',
'com/com_private_data.cpp',

View File

@ -17,7 +17,7 @@ namespace dxvk::sync {
bool RecursiveSpinlock::try_lock() {
uint32_t threadId = GetCurrentThreadId();
uint32_t threadId = dxvk::this_thread::get_id();
uint32_t expected = 0;
bool status = m_owner.compare_exchange_weak(

27
src/util/thread.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "thread.h"
#include "util_likely.h"
#include <atomic>
#ifndef _WIN32
namespace dxvk::this_thread {
static std::atomic<uint32_t> g_threadCtr = { 0u };
static thread_local uint32_t g_threadId = 0u;
// This implementation returns thread ids unique to the current instance.
// ie. if you use this across multiple .so's then you might get conflicting ids.
//
// This isn't an issue for us, as it is only used by the spinlock implementation,
// but may be for you if you use this elsewhere.
uint32_t get_id() {
if (unlikely(!g_threadId))
g_threadId = ++g_threadCtr;
return g_threadId;
}
}
#endif

View File

@ -14,6 +14,7 @@
namespace dxvk {
#ifdef _WIN32
/**
* \brief Thread priority
*/
@ -147,6 +148,10 @@ namespace dxvk {
inline void yield() {
SwitchToThread();
}
inline uint32_t get_id() {
return uint32_t(GetCurrentThreadId());
}
}
@ -323,4 +328,19 @@ namespace dxvk {
};
#else
using mutex = std::mutex;
using thread = std::thread;
using recursive_mutex = std::recursive_mutex;
using condition_variable = std::condition_variable;
namespace this_thread {
inline void yield() {
std::this_thread::yield();
}
uint32_t get_id();
}
#endif
}