diff --git a/src/unix/process.c b/src/unix/process.c index ca3f6e40..17e80d21 100644 --- a/src/unix/process.c +++ b/src/unix/process.c @@ -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); } } diff --git a/src/unix/thread.c b/src/unix/thread.c index b003ea5c..19d559bc 100644 --- a/src/unix/thread.c +++ b/src/unix/thread.c @@ -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++) diff --git a/test/run-tests.c b/test/run-tests.c index 0f999f74..da9a24c4 100644 --- a/test/run-tests.c +++ b/test/run-tests.c @@ -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)); diff --git a/test/test-spawn.c b/test/test-spawn.c index e85b2d33..f452bd35 100644 --- a/test/test-spawn.c +++ b/test/test-spawn.c @@ -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)) {