Skip to content

Commit 2dd0439

Browse files
committed
win32: implement set_thread_name, move process priority class outside of library code
1 parent 1032c75 commit 2dd0439

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/ossia/audio/dummy_protocol.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class dummy_engine final : public audio_engine
3434
m_runThread = std::thread{[this, us_per_buffer] {
3535
ossia::set_thread_name("ossia audio 0");
3636
ossia::set_thread_pinned(thread_type::Audio, 0);
37+
ossia::set_thread_realtime(m_runThread, 99);
3738

3839
using clk = std::chrono::high_resolution_clock;
3940

src/ossia/detail/thread.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,43 @@ void set_thread_realtime(std::thread& t, int prio, bool algo_fifo)
2020
{
2121
auto hdl = reinterpret_cast<HANDLE>(t.native_handle());
2222

23-
SetPriorityClass(hdl, REALTIME_PRIORITY_CLASS);
24-
SetThreadPriority(hdl, THREAD_PRIORITY_TIME_CRITICAL);
23+
auto err = SetThreadPriority(hdl, THREAD_PRIORITY_TIME_CRITICAL);
24+
if(err == 0)
25+
SetThreadPriority(hdl, THREAD_PRIORITY_HIGHEST);
2526
}
2627

27-
void set_thread_name(std::thread& t, std::string_view name) { }
28-
void set_thread_name(std::string_view name) { }
28+
void set_thread_name(std::thread& t, std::string_view name)
29+
{
30+
auto hdl = reinterpret_cast<HANDLE>(t.native_handle());
31+
std::wstring wname;
32+
if (!name.empty())
33+
{
34+
const int sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, name.data(), name.size(), nullptr, 0);
35+
if(sizeNeeded > 0) {
36+
wname.resize(sizeNeeded);
37+
MultiByteToWideChar(CP_UTF8, 0, name.data(), name.size(), wname.data(), sizeNeeded);
38+
}
39+
}
40+
SetThreadDescription(hdl, wname.data());
41+
}
42+
43+
void set_thread_name(std::string_view name)
44+
{
45+
auto hdl = GetCurrentThread();
46+
if(!hdl)
47+
return;
48+
49+
std::wstring wname;
50+
if (!name.empty())
51+
{
52+
const int sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, name.data(), name.size(), nullptr, 0);
53+
if(sizeNeeded > 0) {
54+
wname.resize(sizeNeeded);
55+
MultiByteToWideChar(CP_UTF8, 0, name.data(), name.size(), wname.data(), sizeNeeded);
56+
}
57+
}
58+
SetThreadDescription(hdl, wname.data());
59+
}
2960
void set_thread_pinned(std::thread& t, int cpu) { }
3061
void set_thread_pinned(int cpu) { }
3162

0 commit comments

Comments
 (0)