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/__support/libc_errno.h" |
14 | #include "src/__support/macros/config.h" |
15 | |
16 | #include <sched.h> |
17 | #include <stdint.h> |
18 | #include <sys/syscall.h> // For syscall numbers. |
19 | |
20 | namespace LIBC_NAMESPACE_DECL { |
21 | |
22 | LLVM_LIBC_FUNCTION(int, sched_getaffinity, |
23 | (pid_t tid, size_t cpuset_size, cpu_set_t *mask)) { |
24 | int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_getaffinity, tid, |
25 | cpuset_size, mask); |
26 | if (ret < 0) { |
27 | libc_errno = -ret; |
28 | return -1; |
29 | } |
30 | if (size_t(ret) < cpuset_size) { |
31 | // This means that only |ret| bytes in |mask| have been set. We will have to |
32 | // zero out the remaining bytes. |
33 | auto *mask_bytes = reinterpret_cast<uint8_t *>(mask); |
34 | for (size_t i = size_t(ret); i < cpuset_size; ++i) |
35 | mask_bytes[i] = 0; |
36 | } |
37 | return 0; |
38 | } |
39 | |
40 | } // namespace LIBC_NAMESPACE_DECL |
41 | |