From 0ebf07338525687335727493fe112580b45678fc Mon Sep 17 00:00:00 2001 From: Triang3l Date: Sun, 22 Nov 2020 16:56:04 +0300 Subject: [PATCH] [Base] Affinity and yield to sched on Android --- src/xenia/base/threading_posix.cc | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/xenia/base/threading_posix.cc b/src/xenia/base/threading_posix.cc index 7194855f2..9ef64b908 100644 --- a/src/xenia/base/threading_posix.cc +++ b/src/xenia/base/threading_posix.cc @@ -11,6 +11,7 @@ #include "xenia/base/assert.h" #include "xenia/base/logging.h" +#include "xenia/base/platform.h" #include #include @@ -19,9 +20,14 @@ #include #include #include +#include #include #include +#if XE_PLATFORM_ANDROID +#include +#endif + namespace xe { namespace threading { @@ -88,7 +94,11 @@ void set_name(std::thread::native_handle_type handle, void set_name(const std::string_view name) { set_name(pthread_self(), name); } void MaybeYield() { +#if XE_PLATFORM_ANDROID + sched_yield(); +#else pthread_yield(); +#endif __sync_synchronize(); } @@ -558,8 +568,14 @@ class PosixCondition : public PosixConditionBase { uint64_t affinity_mask() { WaitStarted(); cpu_set_t cpu_set; - if (pthread_getaffinity_np(thread_, sizeof(cpu_set_t), &cpu_set) != 0) - assert_always(); + int get_result; +#if XE_PLATFORM_ANDROID + get_result = sched_getaffinity(pthread_gettid_np(thread_), + sizeof(cpu_set_t), &cpu_set); +#else + get_result = pthread_getaffinity_np(thread_, sizeof(cpu_set_t), &cpu_set); +#endif + assert_zero(get_result); uint64_t result = 0; auto cpu_count = std::min(CPU_SETSIZE, 64); for (auto i = 0u; i < cpu_count; i++) { @@ -578,9 +594,14 @@ class PosixCondition : public PosixConditionBase { CPU_SET(i, &cpu_set); } } - if (pthread_setaffinity_np(thread_, sizeof(cpu_set_t), &cpu_set) != 0) { - assert_always(); - } + int ret; +#if XE_PLATFORM_ANDROID + ret = sched_setaffinity(pthread_gettid_np(thread_), sizeof(cpu_set_t), + &cpu_set); +#else + ret = pthread_setaffinity_np(thread_, sizeof(cpu_set_t), &cpu_set); +#endif + assert_zero(ret); } int priority() {