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