1 #define _GNU_SOURCE
2 #include <sched.h>
3 #include <string.h>
4 #include "pthread_impl.h"
5 #include "syscall.h"
6
sched_setaffinity(pid_t tid,size_t size,const cpu_set_t * set)7 int sched_setaffinity(pid_t tid, size_t size, const cpu_set_t *set)
8 {
9 if (size != sizeof(cpu_set_t)) {
10 errno = EINVAL;
11 return -1;
12 }
13 return syscall(SYS_sched_setaffinity, tid, (unsigned short)(set->__bits[0]), MUSL_TYPE_PROCESS);
14 }
15
pthread_setaffinity_np(pthread_t td,size_t size,const cpu_set_t * set)16 int pthread_setaffinity_np(pthread_t td, size_t size, const cpu_set_t *set)
17 {
18 if (size != sizeof(cpu_set_t)) {
19 return EINVAL;
20 }
21 return -__syscall(SYS_sched_setaffinity, td->tid, (unsigned short)(set->__bits[0]), MUSL_TYPE_THREAD);
22 }
23
do_getaffinity(pid_t tid,size_t size,cpu_set_t * set,int flag)24 static int do_getaffinity(pid_t tid, size_t size, cpu_set_t *set, int flag)
25 {
26 unsigned int cpuset;
27 if (size != sizeof(cpu_set_t)) {
28 return -EINVAL;
29 }
30 int ret = __syscall(SYS_sched_getaffinity, tid, &cpuset, flag);
31 if (ret < 0) {
32 return ret;
33 }
34 set->__bits[0] = (long)cpuset;
35 return 0;
36 }
37
sched_getaffinity(pid_t tid,size_t size,cpu_set_t * set)38 int sched_getaffinity(pid_t tid, size_t size, cpu_set_t *set)
39 {
40 return __syscall_ret(do_getaffinity(tid, size, set, MUSL_TYPE_PROCESS));
41 }
42
pthread_getaffinity_np(pthread_t td,size_t size,cpu_set_t * set)43 int pthread_getaffinity_np(pthread_t td, size_t size, cpu_set_t *set)
44 {
45 return -do_getaffinity(td->tid, size, set, MUSL_TYPE_THREAD);
46 }
47