Add TD_HAVE_THREAD_AFFINITY macros.
This commit is contained in:
parent
b8abc6c19e
commit
9bf5d57851
|
@ -80,9 +80,11 @@ void ConcurrentScheduler::start() {
|
||||||
#if TD_PORT_WINDOWS
|
#if TD_PORT_WINDOWS
|
||||||
detail::Iocp::Guard iocp_guard(iocp_.get());
|
detail::Iocp::Guard iocp_guard(iocp_.get());
|
||||||
#endif
|
#endif
|
||||||
|
#if TD_HAVE_THREAD_AFFINITY
|
||||||
if (thread_affinity_mask != 0) {
|
if (thread_affinity_mask != 0) {
|
||||||
thread::set_affinity_mask(this_thread::get_id(), thread_affinity_mask).ignore();
|
thread::set_affinity_mask(this_thread::get_id(), thread_affinity_mask).ignore();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
while (!is_finished()) {
|
while (!is_finished()) {
|
||||||
sched->run(Timestamp::in(10));
|
sched->run(Timestamp::in(10));
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
return pthread_create(thread, attr, start_routine, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if TD_HAVE_THREAD_AFFINITY
|
||||||
Status ThreadPthread::set_affinity_mask(id thread_id, uint64 mask) {
|
Status ThreadPthread::set_affinity_mask(id thread_id, uint64 mask) {
|
||||||
#if TD_LINUX || TD_FREEBSD
|
#if TD_LINUX || TD_FREEBSD
|
||||||
#if TD_FREEBSD
|
#if TD_FREEBSD
|
||||||
|
@ -193,6 +194,7 @@ uint64 ThreadPthread::get_affinity_mask(id thread_id) {
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace this_thread_pthread {
|
namespace this_thread_pthread {
|
||||||
ThreadPthread::id get_id() {
|
ThreadPthread::id get_id() {
|
||||||
|
|
|
@ -28,6 +28,10 @@
|
||||||
#endif
|
#endif
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#if TD_LINUX || TD_FREEBSD || TD_NETBSD
|
||||||
|
#define TD_HAVE_THREAD_AFFINITY 1
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
@ -70,9 +74,11 @@ class ThreadPthread {
|
||||||
|
|
||||||
static void send_real_time_signal(id thread_id, int real_time_signal_number);
|
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 Status set_affinity_mask(id thread_id, uint64 mask);
|
||||||
|
|
||||||
static uint64 get_affinity_mask(id thread_id);
|
static uint64 get_affinity_mask(id thread_id);
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MovableValue<bool> is_inited_;
|
MovableValue<bool> is_inited_;
|
||||||
|
|
|
@ -25,6 +25,10 @@
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#if TD_WINDOWS
|
||||||
|
#define TD_HAVE_THREAD_AFFINITY 1
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
@ -79,8 +83,8 @@ class ThreadStl {
|
||||||
// not supported
|
// not supported
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if TD_HAVE_THREAD_AFFINITY
|
||||||
static Status set_affinity_mask(id thread_id, uint64 mask) {
|
static Status set_affinity_mask(id thread_id, uint64 mask) {
|
||||||
#if TD_WINDOWS
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
@ -93,13 +97,9 @@ class ThreadStl {
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
}
|
}
|
||||||
return OS_ERROR("Failed to set thread affinity mask");
|
return OS_ERROR("Failed to set thread affinity mask");
|
||||||
#else
|
|
||||||
return Status::Error("Unsupported");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint64 get_affinity_mask(id thread_id) {
|
static uint64 get_affinity_mask(id thread_id) {
|
||||||
#if TD_WINDOWS
|
|
||||||
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)) {
|
||||||
|
@ -114,9 +114,9 @@ class ThreadStl {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::thread thread_;
|
std::thread thread_;
|
||||||
|
|
|
@ -285,7 +285,7 @@ TEST(Port, EventFdAndSignals) {
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !TD_THREAD_UNSUPPORTED
|
#if TD_HAVE_THREAD_AFFINITY
|
||||||
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);
|
||||||
|
|
Loading…
Reference in New Issue