Add TD_HAVE_THREAD_AFFINITY macros.

This commit is contained in:
levlam 2022-09-17 21:58:00 +03:00
parent b8abc6c19e
commit 9bf5d57851
5 changed files with 17 additions and 7 deletions

View File

@ -80,9 +80,11 @@ void ConcurrentScheduler::start() {
#if TD_PORT_WINDOWS
detail::Iocp::Guard iocp_guard(iocp_.get());
#endif
#if TD_HAVE_THREAD_AFFINITY
if (thread_affinity_mask != 0) {
thread::set_affinity_mask(this_thread::get_id(), thread_affinity_mask).ignore();
}
#endif
while (!is_finished()) {
sched->run(Timestamp::in(10));
}

View File

@ -104,6 +104,7 @@ int ThreadPthread::do_pthread_create(pthread_t *thread, const pthread_attr_t *at
return pthread_create(thread, attr, start_routine, arg);
}
#if TD_HAVE_THREAD_AFFINITY
Status ThreadPthread::set_affinity_mask(id thread_id, uint64 mask) {
#if TD_LINUX || TD_FREEBSD
#if TD_FREEBSD
@ -193,6 +194,7 @@ uint64 ThreadPthread::get_affinity_mask(id thread_id) {
return 0;
#endif
}
#endif
namespace this_thread_pthread {
ThreadPthread::id get_id() {

View File

@ -28,6 +28,10 @@
#endif
#include <sys/types.h>
#if TD_LINUX || TD_FREEBSD || TD_NETBSD
#define TD_HAVE_THREAD_AFFINITY 1
#endif
namespace td {
namespace detail {
@ -70,9 +74,11 @@ class ThreadPthread {
static void send_real_time_signal(id thread_id, int real_time_signal_number);
#if TD_HAVE_THREAD_AFFINITY
static Status set_affinity_mask(id thread_id, uint64 mask);
static uint64 get_affinity_mask(id thread_id);
#endif
private:
MovableValue<bool> is_inited_;

View File

@ -25,6 +25,10 @@
#include <type_traits>
#include <utility>
#if TD_WINDOWS
#define TD_HAVE_THREAD_AFFINITY 1
#endif
namespace td {
namespace detail {
@ -79,8 +83,8 @@ class ThreadStl {
// not supported
}
#if TD_HAVE_THREAD_AFFINITY
static Status set_affinity_mask(id thread_id, uint64 mask) {
#if TD_WINDOWS
if (static_cast<DWORD_PTR>(mask) != mask) {
return Status::Error("Invalid thread affinity mask specified");
}
@ -93,13 +97,9 @@ class ThreadStl {
return Status::OK();
}
return OS_ERROR("Failed to set thread affinity mask");
#else
return Status::Error("Unsupported");
#endif
}
static uint64 get_affinity_mask(id thread_id) {
#if TD_WINDOWS
DWORD_PTR process_mask = 0;
DWORD_PTR system_mask = 0;
if (GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask)) {
@ -114,9 +114,9 @@ class ThreadStl {
}
return result;
}
#endif
return 0;
}
#endif
private:
std::thread thread_;

View File

@ -285,7 +285,7 @@ TEST(Port, EventFdAndSignals) {
#endif
#endif
#if !TD_THREAD_UNSUPPORTED
#if TD_HAVE_THREAD_AFFINITY
TEST(Port, ThreadAffinityMask) {
auto thread_id = td::this_thread::get_id();
auto old_mask = td::thread::get_affinity_mask(thread_id);