xref: /haiku/src/libs/gnu/sched_affinity.cpp (revision c5a740cdb23a6dc9973a8142303b67f830c67b54)
1 /*
2  * Copyright 2023, Jérôme Duval, jerome.duval@gmail.com. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <sched.h>
8 
9 #include <pthread_private.h>
10 #include <syscalls.h>
11 
12 
13 int
pthread_setaffinity_np(pthread_t thread,size_t cpusetsize,const cpuset_t * mask)14 pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpuset_t* mask)
15 {
16 	return _kern_set_thread_affinity(thread->id, mask, cpusetsize);
17 }
18 
19 
20 int
pthread_getaffinity_np(pthread_t thread,size_t cpusetsize,cpuset_t * mask)21 pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpuset_t* mask)
22 {
23 	status_t status = _kern_get_thread_affinity(thread->id, mask, cpusetsize);
24 	if (status != B_OK) {
25 		if (status == B_BAD_THREAD_ID)
26 			return ESRCH;
27 
28 		return status;
29 	}
30 
31 	return 0;
32 }
33 
34 
35 int
sched_setaffinity(pid_t id,size_t cpusetsize,const cpuset_t * mask)36 sched_setaffinity(pid_t id, size_t cpusetsize, const cpuset_t* mask)
37 {
38 	status_t status = _kern_set_thread_affinity(id, mask, cpusetsize);
39 	if (status != B_OK) {
40 		if (status == B_BAD_THREAD_ID)
41 			return ESRCH;
42 
43 		return status;
44 	}
45 
46 	return 0;
47 }
48 
49 
50 int
sched_getaffinity(pid_t id,size_t cpusetsize,cpuset_t * mask)51 sched_getaffinity(pid_t id, size_t cpusetsize, cpuset_t* mask)
52 {
53 	status_t status = _kern_get_thread_affinity(id, mask, cpusetsize);
54 	if (status != B_OK) {
55 		if (status == B_BAD_THREAD_ID)
56 			return ESRCH;
57 
58 		return status;
59 	}
60 
61 	return 0;
62 }
63