xref: /llvm-project/libc/src/sched/linux/sched_getaffinity.cpp (revision f0a3954ef1af274cb0b79a6e0bc51a31e0de540b)
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 __llvm_libc {
20 
21 LLVM_LIBC_FUNCTION(int, sched_getaffinity,
22                    (pid_t tid, size_t cpuset_size, cpu_set_t *mask)) {
23   int ret = __llvm_libc::syscall_impl<int>(SYS_sched_getaffinity, tid,
24                                            cpuset_size, 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 __llvm_libc
40