xref: /aosp_15_r20/external/llvm-libc/src/sched/linux/sched_rr_get_interval.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Implementation of sched_rr_get_interval ---------------------------===//
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_rr_get_interval.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 #include "src/__support/macros/config.h"
14 #include "src/errno/libc_errno.h"
15 
16 #include <sys/syscall.h> // For syscall numbers.
17 
18 #ifdef SYS_sched_rr_get_interval_time64
19 #include <linux/time_types.h> // For __kernel_timespec.
20 #endif
21 
22 namespace LIBC_NAMESPACE_DECL {
23 
24 LLVM_LIBC_FUNCTION(int, sched_rr_get_interval,
25                    (pid_t tid, struct timespec *tp)) {
26 #ifdef SYS_sched_rr_get_interval
27   int ret =
28       LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_rr_get_interval, tid, tp);
29 #elif defined(SYS_sched_rr_get_interval_time64)
30   // The difference between the  and SYS_sched_rr_get_interval
31   // SYS_sched_rr_get_interval_time64 syscalls is the data type used for the
32   // time interval parameter: the latter takes a struct __kernel_timespec
33   int ret;
34   if (tp) {
35     struct __kernel_timespec ts32;
36     ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_rr_get_interval_time64,
37                                             tid, &ts32);
38     if (ret == 0) {
39       tp->tv_sec = ts32.tv_sec;
40       tp->tv_nsec = static_cast<long int>(ts32.tv_nsec);
41     }
42   } else
43     // When tp is a nullptr, we still do the syscall to set ret and errno
44     ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_rr_get_interval_time64,
45                                             tid, nullptr);
46 #else
47 #error                                                                         \
48     "sched_rr_get_interval and sched_rr_get_interval_time64 syscalls not available."
49 #endif
50   if (ret < 0) {
51     libc_errno = -ret;
52     return -1;
53   }
54   return 0;
55 }
56 
57 } // namespace LIBC_NAMESPACE_DECL
58