Skip to content

Commit

Permalink
[util] Implement thread helpers on non-Windows platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
misyltoad committed Apr 19, 2022
1 parent 9302d33 commit 9ee0f51
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down
2 changes: 1 addition & 1 deletion src/util/sync/sync_recursive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 27 additions & 0 deletions src/util/thread.cpp
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions src/util/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace dxvk {

#ifdef _WIN32
/**
* \brief Thread priority
*/
Expand Down Expand Up @@ -147,6 +148,10 @@ namespace dxvk {
inline void yield() {
SwitchToThread();
}

inline uint32_t get_id() {
return uint32_t(GetCurrentThreadId());
}
}


Expand Down Expand Up @@ -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

}

0 comments on commit 9ee0f51

Please sign in to comment.