From 9bf5d5785188e4f011588b5402b8ad6b01c067e4 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 17 Sep 2022 21:58:00 +0300 Subject: [PATCH] Add TD_HAVE_THREAD_AFFINITY macros. --- tdactor/td/actor/ConcurrentScheduler.cpp | 2 ++ tdutils/td/utils/port/detail/ThreadPthread.cpp | 2 ++ tdutils/td/utils/port/detail/ThreadPthread.h | 6 ++++++ tdutils/td/utils/port/detail/ThreadStl.h | 12 ++++++------ tdutils/test/port.cpp | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tdactor/td/actor/ConcurrentScheduler.cpp b/tdactor/td/actor/ConcurrentScheduler.cpp index bbfaba362..c904295ad 100644 --- a/tdactor/td/actor/ConcurrentScheduler.cpp +++ b/tdactor/td/actor/ConcurrentScheduler.cpp @@ -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)); } diff --git a/tdutils/td/utils/port/detail/ThreadPthread.cpp b/tdutils/td/utils/port/detail/ThreadPthread.cpp index 493488435..de399f903 100644 --- a/tdutils/td/utils/port/detail/ThreadPthread.cpp +++ b/tdutils/td/utils/port/detail/ThreadPthread.cpp @@ -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() { diff --git a/tdutils/td/utils/port/detail/ThreadPthread.h b/tdutils/td/utils/port/detail/ThreadPthread.h index 88fea76d6..76f6bebe1 100644 --- a/tdutils/td/utils/port/detail/ThreadPthread.h +++ b/tdutils/td/utils/port/detail/ThreadPthread.h @@ -28,6 +28,10 @@ #endif #include +#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 is_inited_; diff --git a/tdutils/td/utils/port/detail/ThreadStl.h b/tdutils/td/utils/port/detail/ThreadStl.h index dd55a21ec..b5313a36c 100644 --- a/tdutils/td/utils/port/detail/ThreadStl.h +++ b/tdutils/td/utils/port/detail/ThreadStl.h @@ -25,6 +25,10 @@ #include #include +#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(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_; diff --git a/tdutils/test/port.cpp b/tdutils/test/port.cpp index 9287dae0b..b5c90fd75 100644 --- a/tdutils/test/port.cpp +++ b/tdutils/test/port.cpp @@ -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);