Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / sched / linux / sched_getaffinity.cpp
blob476576178fa48f041f4aae2a1f866a09c68486d8
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 //===----------------------------------------------------------------------===//
9 #include "src/sched/sched_getaffinity.h"
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 #include "src/errno/libc_errno.h"
15 #include <sched.h>
16 #include <stdint.h>
17 #include <sys/syscall.h> // For syscall numbers.
19 namespace LIBC_NAMESPACE {
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, tid,
24 cpuset_size, mask);
25 if (ret < 0) {
26 libc_errno = -ret;
27 return -1;
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;
36 return 0;
39 } // namespace LIBC_NAMESPACE