1 | //===-- Implementation of sched_getaffinity -------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/sched/sched_getaffinity.h" |
10 | |
11 | #include "src/__support/OSUtil/syscall.h" // For internal syscall function. |
12 | #include "src/__support/common.h" |
13 | #include "src/errno/libc_errno.h" |
14 | |
15 | #include <sched.h> |
16 | #include <stdint.h> |
17 | #include <sys/syscall.h> // For syscall numbers. |
18 | |
19 | namespace LIBC_NAMESPACE { |
20 | |
21 | LLVM_LIBC_FUNCTION(int, sched_getaffinity, |
22 | (pid_t tid, size_t cpuset_size, cpu_set_t *mask)) { |
23 | int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_getaffinity, ts: tid, |
24 | ts: cpuset_size, ts: mask); |
25 | if (ret < 0) { |
26 | libc_errno = -ret; |
27 | return -1; |
28 | } |
29 | if (size_t(ret) < cpuset_size) { |
30 | // This means that only |ret| bytes in |mask| have been set. We will have to |
31 | // zero out the remaining bytes. |
32 | auto *mask_bytes = reinterpret_cast<uint8_t *>(mask); |
33 | for (size_t i = size_t(ret); i < cpuset_size; ++i) |
34 | mask_bytes[i] = 0; |
35 | } |
36 | return 0; |
37 | } |
38 | |
39 | } // namespace LIBC_NAMESPACE |
40 |