Use DWORD as thread::id on Windows.

This commit is contained in:
levlam 2022-09-17 13:21:04 +03:00
parent 0cc3fb0e9d
commit 9d5c151a3f
2 changed files with 25 additions and 11 deletions

View File

@ -12,6 +12,9 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/invoke.h" #include "td/utils/invoke.h"
#if TD_WINDOWS
#include "td/utils/port/detail/NativeFd.h"
#endif
#include "td/utils/port/detail/ThreadIdGuard.h" #include "td/utils/port/detail/ThreadIdGuard.h"
#include "td/utils/port/thread_local.h" #include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
@ -67,7 +70,7 @@ class ThreadStl {
} }
#if TD_WINDOWS #if TD_WINDOWS
using id = HANDLE; using id = DWORD;
#else #else
using id = std::thread::id; using id = std::thread::id;
#endif #endif
@ -81,7 +84,12 @@ class ThreadStl {
if (static_cast<DWORD_PTR>(mask) != mask) { if (static_cast<DWORD_PTR>(mask) != mask) {
return Status::Error("Invalid thread affinity mask specified"); return Status::Error("Invalid thread affinity mask specified");
} }
if (SetThreadAffinityMask(thread_id, static_cast<DWORD_PTR>(mask))) { auto handle = OpenThread(THREAD_SET_LIMITED_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION, FALSE, thread_id);
if (handle == nullptr) {
return Status::Error("Failed to access thread");
}
NativeFd thread_handle(handle);
if (SetThreadAffinityMask(thread_handle.fd(), static_cast<DWORD_PTR>(mask))) {
return Status::OK(); return Status::OK();
} }
return OS_ERROR("Failed to set thread affinity mask"); return OS_ERROR("Failed to set thread affinity mask");
@ -95,9 +103,14 @@ class ThreadStl {
DWORD_PTR process_mask = 0; DWORD_PTR process_mask = 0;
DWORD_PTR system_mask = 0; DWORD_PTR system_mask = 0;
if (GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask)) { if (GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask)) {
auto result = SetThreadAffinityMask(thread_id, process_mask); auto handle = OpenThread(THREAD_SET_LIMITED_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION, FALSE, thread_id);
if (handle == nullptr) {
return 0;
}
NativeFd thread_handle(handle);
auto result = SetThreadAffinityMask(thread_handle.fd(), process_mask);
if (result != 0 && result != process_mask) { if (result != 0 && result != process_mask) {
SetThreadAffinityMask(thread_id, result); SetThreadAffinityMask(thread_handle.fd(), result);
} }
return result; return result;
} }
@ -117,7 +130,7 @@ class ThreadStl {
namespace this_thread_stl { namespace this_thread_stl {
#if TD_WINDOWS #if TD_WINDOWS
inline ThreadStl::id get_id() { inline ThreadStl::id get_id() {
return GetCurrentThread(); return GetCurrentThreadId();
} }
#else #else
using std::this_thread::get_id; using std::this_thread::get_id;

View File

@ -289,19 +289,20 @@ TEST(Port, EventFdAndSignals) {
TEST(Port, ThreadAffinityMask) { TEST(Port, ThreadAffinityMask) {
auto thread_id = td::this_thread::get_id(); auto thread_id = td::this_thread::get_id();
auto old_mask = td::thread::get_affinity_mask(thread_id); auto old_mask = td::thread::get_affinity_mask(thread_id);
LOG(INFO) << "Initial thread affinity mask: " << old_mask; LOG(INFO) << "Initial thread " << thread_id << " affinity mask: " << old_mask;
for (size_t i = 0; i < 64; i++) { for (size_t i = 0; i < 64; i++) {
auto mask = td::thread::get_affinity_mask(thread_id); auto mask = td::thread::get_affinity_mask(thread_id);
LOG(INFO) << mask; LOG(INFO) << mask;
auto result = td::thread::set_affinity_mask(thread_id, static_cast<td::uint64>(1) << i); auto result = td::thread::set_affinity_mask(thread_id, static_cast<td::uint64>(1) << i);
LOG(INFO) << i << ": " << result; LOG(INFO) << i << ": " << result << ' ' << td::thread::get_affinity_mask(thread_id);
mask = td::thread::get_affinity_mask(thread_id);
LOG(INFO) << mask;
if (i <= 1) { if (i <= 1) {
td::thread thread([] { td::thread thread([] {
auto mask = td::thread::get_affinity_mask(td::this_thread::get_id()); auto thread_id = td::this_thread::get_id();
LOG(INFO) << "New thread affinity mask: " << mask; auto mask = td::thread::get_affinity_mask(thread_id);
LOG(INFO) << "New thread " << thread_id << " affinity mask: " << mask;
auto result = td::thread::set_affinity_mask(thread_id, 1);
LOG(INFO) << "Thread " << thread_id << ": " << result << ' ' << td::thread::get_affinity_mask(thread_id);
}); });
} }
} }