diff --git a/src/util/meson.build b/src/util/meson.build index 058b96869..34da01eeb 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -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', diff --git a/src/util/sync/sync_recursive.cpp b/src/util/sync/sync_recursive.cpp index ee6a12653..85af6137d 100644 --- a/src/util/sync/sync_recursive.cpp +++ b/src/util/sync/sync_recursive.cpp @@ -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( diff --git a/src/util/thread.cpp b/src/util/thread.cpp new file mode 100644 index 000000000..eed45d34b --- /dev/null +++ b/src/util/thread.cpp @@ -0,0 +1,27 @@ +#include "thread.h" +#include "util_likely.h" + +#include + +#ifndef _WIN32 + +namespace dxvk::this_thread { + + static std::atomic 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 diff --git a/src/util/thread.h b/src/util/thread.h index 28aeca8a6..1299a715f 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -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 + }