threads,android: use syscall directly for affinity

The posix symbol may not be always available, but the linux one seems
to be consistently available.

Refs: https://github.com/libuv/libuv/issues/2655 (fixes one issue)
PR-URL: https://github.com/libuv/libuv/pull/3247
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
This commit is contained in:
Jameson Nash 2021-07-23 17:03:39 -04:00 committed by GitHub
parent 4fe755bc5b
commit 853f66f0b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 5 deletions

View File

@ -232,7 +232,6 @@ static void uv__process_child_init(const uv_process_options_t* options,
int fd;
int n;
#if defined(__linux__) || defined(__FreeBSD__)
int r;
int i;
int cpumask_size;
uv__cpu_set_t cpuset;
@ -327,9 +326,15 @@ static void uv__process_child_init(const uv_process_options_t* options,
}
}
r = -pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
if (r != 0) {
uv__write_int(error_fd, r);
#if defined(__linux__)
err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
#else
err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
if (err)
err = errno;
#endif
if (err) {
uv__write_int(error_fd, UV__ERR(err));
_exit(127);
}
}

View File

@ -284,7 +284,13 @@ int uv_thread_setaffinity(uv_thread_t* tid,
if (cpumask[i])
CPU_SET(i, &cpuset);
return UV__ERR(pthread_setaffinity_np(*tid, sizeof(cpuset), &cpuset));
#if defined(__ANDROID__)
r = sched_setaffinity(pthread_gettid_np(*tid), sizeof(cpuset), &cpuset);
#else
r = pthread_setaffinity_np(*tid, sizeof(cpuset), &cpuset);
#endif
return UV__ERR(r);
}
@ -303,7 +309,11 @@ int uv_thread_getaffinity(uv_thread_t* tid,
return UV_EINVAL;
CPU_ZERO(&cpuset);
#if defined(__ANDROID__)
r = sched_getaffinity(pthread_gettid_np(*tid), sizeof(cpuset), &cpuset);
#else
r = pthread_getaffinity_np(*tid, sizeof(cpuset), &cpuset);
#endif
if (r)
return UV__ERR(r);
for (i = 0; i < cpumasksize; i++)

View File

@ -304,7 +304,11 @@ static int maybe_run_test(int argc, char **argv) {
}
#else
CPU_ZERO(&cpuset);
#ifdef __linux__
r = sched_getaffinity(0, sizeof(cpuset), &cpuset);
#else
r = pthread_getaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
#endif
ASSERT(r == 0);
for (i = 0; i < cpumask_size; ++i) {
ASSERT(CPU_ISSET(i, &cpuset) == (i == cpu));

View File

@ -1539,7 +1539,11 @@ TEST_IMPL(spawn_affinity) {
}
#else
CPU_ZERO(&cpuset);
#ifdef __linux__
r = sched_getaffinity(0, sizeof(cpuset), &cpuset);
#else
r = pthread_getaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
#endif
ASSERT(r == 0);
for (i = 0; i < cpumask_size; ++i) {
if (CPU_ISSET(i, &cpuset)) {