[libc-commits] [libc] [libc] Implement timerfd_(s|g)ettime on Linux. (PR #243657)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 16:07:53 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Alexey Samsonov (vonosmas)
<details>
<summary>Changes</summary>
* Implement `timerfd_settime` and `timerfd_gettime` interface on Linux platforms;
* These are simple syscall wrappers, with the caveat that we also use
`SYS_timerfd_(s|g)ettime64` on 32-bit platforms, if those are available (RISCV-32).
* Adds a basic test case to validate relative/absolute timer behavior, and reading
the number of expirations from the returned descriptor.
Assisted-by: Gemini, human-reviewed.
---
Patch is 23.86 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/243657.diff
17 Files Affected:
- (modified) libc/config/linux/aarch64/entrypoints.txt (+2)
- (modified) libc/config/linux/riscv/entrypoints.txt (+2)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+2)
- (modified) libc/include/CMakeLists.txt (+1)
- (modified) libc/include/sys/syscall.h.def (+8)
- (modified) libc/include/sys/timerfd.yaml (+14-1)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+28)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h (+50)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h (+53)
- (modified) libc/src/sys/timerfd/CMakeLists.txt (+14)
- (modified) libc/src/sys/timerfd/linux/CMakeLists.txt (+30)
- (added) libc/src/sys/timerfd/linux/timerfd_gettime.cpp (+34)
- (added) libc/src/sys/timerfd/linux/timerfd_settime.cpp (+36)
- (added) libc/src/sys/timerfd/timerfd_gettime.h (+26)
- (added) libc/src/sys/timerfd/timerfd_settime.h (+27)
- (modified) libc/test/src/sys/timerfd/linux/CMakeLists.txt (+26)
- (added) libc/test/src/sys/timerfd/linux/timerfd_gettime_test.cpp (+159)
``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c115a3b670891..35b9f6ac7f1b4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -406,6 +406,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/timerfd.h entrypoints
libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
# sys/utsname.h entrypoints
libc.src.sys.utsname.uname
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index cf3de959f57ae..d97e9d2aebc96 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -449,6 +449,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/timerfd.h entrypoints
libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
# sys/sysinfo.h entrypoints
libc.src.sys.sysinfo.sysinfo
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 35d23c3b9977b..50b658f83feba 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -449,6 +449,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/timerfd.h entrypoints
libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
# sys/sysinfo.h entrypoints
libc.src.sys.sysinfo.sysinfo
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 2261acf58095d..1525c1662acda 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1057,6 +1057,7 @@ add_header_macro(
.llvm_libc_common_h
.llvm-libc-macros.sys_timerfd_macros
.llvm-libc-macros.time_macros
+ .llvm-libc-types.struct_itimerspec
)
add_header_macro(
diff --git a/libc/include/sys/syscall.h.def b/libc/include/sys/syscall.h.def
index 5c2b1eab9c285..5e80ad3c20d94 100644
--- a/libc/include/sys/syscall.h.def
+++ b/libc/include/sys/syscall.h.def
@@ -2253,10 +2253,18 @@
#define SYS_timerfd_gettime __NR_timerfd_gettime
#endif
+#ifdef __NR_timerfd_gettime64
+#define SYS_timerfd_gettime64 __NR_timerfd_gettime64
+#endif
+
#ifdef __NR_timerfd_settime
#define SYS_timerfd_settime __NR_timerfd_settime
#endif
+#ifdef __NR_timerfd_settime64
+#define SYS_timerfd_settime64 __NR_timerfd_settime64
+#endif
+
#ifdef __NR_times
#define SYS_times __NR_times
#endif
diff --git a/libc/include/sys/timerfd.yaml b/libc/include/sys/timerfd.yaml
index 285bc5aa16c1c..f840b1b837505 100644
--- a/libc/include/sys/timerfd.yaml
+++ b/libc/include/sys/timerfd.yaml
@@ -20,7 +20,8 @@ macros:
macro_header: sys-timerfd-macros.h
- macro_name: TFD_TIMER_CANCEL_ON_SET
macro_header: sys-timerfd-macros.h
-types: []
+types:
+ - type_name: struct_itimerspec
enums: []
objects: []
functions:
@@ -29,3 +30,15 @@ functions:
arguments:
- type: int
- type: int
+ - name: timerfd_gettime
+ return_type: int
+ arguments:
+ - type: int
+ - type: struct itimerspec *
+ - name: timerfd_settime
+ return_type: int
+ arguments:
+ - type: int
+ - type: int
+ - type: const struct itimerspec *
+ - type: struct itimerspec *
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index cd2f92ce59e71..bb9d3a1846bef 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -2673,3 +2673,31 @@ add_header_library(
libc.src.__support.error_or
libc.src.__support.macros.config
)
+
+add_header_library(
+ timerfd_gettime
+ HDRS
+ timerfd_gettime.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.hdr.types.struct_itimerspec
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ timerfd_settime
+ HDRS
+ timerfd_settime.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.hdr.types.struct_itimerspec
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
new file mode 100644
index 0000000000000..7f7a7089cce1b
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for timerfd_gettime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_GETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_GETTIME_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> timerfd_gettime(int fd,
+ struct itimerspec *curr_value) {
+#if defined(SYS_timerfd_gettime64)
+ static_assert(
+ sizeof(timespec::tv_sec) == sizeof(int64_t),
+ "SYS_timerfd_gettime64 requires struct timespec with 64-bit members.");
+ return syscall_checked<int>(SYS_timerfd_gettime64, fd, curr_value);
+#elif defined(SYS_timerfd_gettime)
+ static_assert(
+ sizeof(timespec::tv_nsec) == sizeof(long),
+ "This legacy syscall fallback is only safe on platforms where tv_nsec "
+ "matches the register size (long). It is unsafe on 32-bit platforms "
+ "with 64-bit tv_nsec.");
+ return syscall_checked<int>(SYS_timerfd_gettime, fd, curr_value);
+#else
+#error "SYS_timerfd_gettime and SYS_timerfd_gettime64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_GETTIME_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
new file mode 100644
index 0000000000000..8c9a5530a4dba
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for timerfd_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_SETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_SETTIME_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> timerfd_settime(int fd, int flags,
+ const struct itimerspec *new_value,
+ struct itimerspec *old_value) {
+#if defined(SYS_timerfd_settime64)
+ static_assert(
+ sizeof(timespec::tv_sec) == sizeof(int64_t),
+ "SYS_timerfd_settime64 requires struct timespec with 64-bit members.");
+ return syscall_checked<int>(SYS_timerfd_settime64, fd, flags, new_value,
+ old_value);
+#elif defined(SYS_timerfd_settime)
+ static_assert(
+ sizeof(timespec::tv_nsec) == sizeof(long),
+ "This legacy syscall fallback is only safe on platforms where tv_nsec "
+ "matches the register size (long). It is unsafe on 32-bit platforms "
+ "with 64-bit tv_nsec.");
+ return syscall_checked<int>(SYS_timerfd_settime, fd, flags, new_value,
+ old_value);
+#else
+#error "SYS_timerfd_settime and SYS_timerfd_settime64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMERFD_SETTIME_H
diff --git a/libc/src/sys/timerfd/CMakeLists.txt b/libc/src/sys/timerfd/CMakeLists.txt
index 69c51ec8919a4..d8668558f524c 100644
--- a/libc/src/sys/timerfd/CMakeLists.txt
+++ b/libc/src/sys/timerfd/CMakeLists.txt
@@ -8,3 +8,17 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.timerfd_create
)
+
+add_entrypoint_object(
+ timerfd_gettime
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.timerfd_gettime
+)
+
+add_entrypoint_object(
+ timerfd_settime
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.timerfd_settime
+)
diff --git a/libc/src/sys/timerfd/linux/CMakeLists.txt b/libc/src/sys/timerfd/linux/CMakeLists.txt
index b1a485a907a61..b8c90d52bd608 100644
--- a/libc/src/sys/timerfd/linux/CMakeLists.txt
+++ b/libc/src/sys/timerfd/linux/CMakeLists.txt
@@ -11,3 +11,33 @@ add_entrypoint_object(
libc.src.__support.macros.config
libc.src.errno.errno
)
+
+add_entrypoint_object(
+ timerfd_gettime
+ SRCS
+ timerfd_gettime.cpp
+ HDRS
+ ../timerfd_gettime.h
+ DEPENDS
+ libc.hdr.types.struct_itimerspec
+ libc.src.__support.OSUtil.linux.syscall_wrappers.timerfd_gettime
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
+add_entrypoint_object(
+ timerfd_settime
+ SRCS
+ timerfd_settime.cpp
+ HDRS
+ ../timerfd_settime.h
+ DEPENDS
+ libc.hdr.types.struct_itimerspec
+ libc.src.__support.OSUtil.linux.syscall_wrappers.timerfd_settime
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
diff --git a/libc/src/sys/timerfd/linux/timerfd_gettime.cpp b/libc/src/sys/timerfd/linux/timerfd_gettime.cpp
new file mode 100644
index 0000000000000..76b2ef719b42f
--- /dev/null
+++ b/libc/src/sys/timerfd/linux/timerfd_gettime.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of timerfd_gettime.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/timerfd/timerfd_gettime.h"
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/timerfd_gettime.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, timerfd_gettime,
+ (int fd, struct itimerspec *curr_value)) {
+ ErrorOr<int> ret = linux_syscalls::timerfd_gettime(fd, curr_value);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return *ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/timerfd/linux/timerfd_settime.cpp b/libc/src/sys/timerfd/linux/timerfd_settime.cpp
new file mode 100644
index 0000000000000..16557c823f2a4
--- /dev/null
+++ b/libc/src/sys/timerfd/linux/timerfd_settime.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of timerfd_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/timerfd/timerfd_settime.h"
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/timerfd_settime.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, timerfd_settime,
+ (int fd, int flags, const struct itimerspec *new_value,
+ struct itimerspec *old_value)) {
+ ErrorOr<int> ret =
+ linux_syscalls::timerfd_settime(fd, flags, new_value, old_value);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return *ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/timerfd/timerfd_gettime.h b/libc/src/sys/timerfd/timerfd_gettime.h
new file mode 100644
index 0000000000000..c42568b6909b9
--- /dev/null
+++ b/libc/src/sys/timerfd/timerfd_gettime.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for timerfd_gettime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_GETTIME_H
+#define LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_GETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timerfd_gettime(int fd, struct itimerspec *curr_value);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_GETTIME_H
diff --git a/libc/src/sys/timerfd/timerfd_settime.h b/libc/src/sys/timerfd/timerfd_settime.h
new file mode 100644
index 0000000000000..5a0ff0eb75d1e
--- /dev/null
+++ b/libc/src/sys/timerfd/timerfd_settime.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for timerfd_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_SETTIME_H
+#define LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_SETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timerfd_settime(int fd, int flags, const struct itimerspec *new_value,
+ struct itimerspec *old_value);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_TIMERFD_TIMERFD_SETTIME_H
diff --git a/libc/test/src/sys/timerfd/linux/CMakeLists.txt b/libc/test/src/sys/timerfd/linux/CMakeLists.txt
index b8ba88008737e..769dcf3e9b587 100644
--- a/libc/test/src/sys/timerfd/linux/CMakeLists.txt
+++ b/libc/test/src/sys/timerfd/linux/CMakeLists.txt
@@ -21,3 +21,29 @@ add_libc_test(
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
+
+add_libc_test(
+ timerfd_gettime_test
+ SUITE
+ libc-sys-timerfd-tests
+ SRCS
+ timerfd_gettime_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.stdint_proxy
+ libc.hdr.sys_timerfd_macros
+ libc.hdr.time_macros
+ libc.hdr.types.ssize_t
+ libc.hdr.types.struct_itimerspec
+ libc.hdr.types.struct_timespec
+ libc.hdr.types.time_t
+ libc.src.__support.CPP.scope
+ libc.src.__support.time.clock_gettime
+ libc.src.sys.timerfd.timerfd_create
+ libc.src.sys.timerfd.timerfd_gettime
+ libc.src.sys.timerfd.timerfd_settime
+ libc.src.unistd.close
+ libc.src.unistd.read
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/timerfd/linux/timerfd_gettime_test.cpp b/libc/test/src/sys/timerfd/linux/timerfd_gettime_test.cpp
new file mode 100644
index 0000000000000..741ce7705b1fc
--- /dev/null
+++ b/libc/test/src/sys/timerfd/linux/timerfd_gettime_test.cpp
@@ -0,0 +1,159 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unit tests for timerfd_gettime and timerfd_settime.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/sys_timerfd_macros.h"
+#include "hdr/time_macros.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_itimerspec.h"
+#include "hdr/types/struct_timespec.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/CPP/scope.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/sys/timerfd/timerfd_create.h"
+#include "src/sys/timerfd/timerfd_gettime.h"
+#include "src/sys/timerfd/timerfd_settime.h"
+#include "src/unistd/close.h"
+#include "src/unistd/read.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LIBC_NAMESPACE::cpp::scope_exit;
+using LlvmLibcTimerfdGettimeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using NsecT = decltype(timespec::tv_nsec);
+
+TEST_F(LlvmLibcTimerfdGettimeTest, SetAndGetRelative) {
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::timerfd_create(CLOCK_MONOTONIC,
+ TFD_CLOEXEC | TFD_NONBLOCK),
+ returns(GE(0)).with_errno(EQ(0)));
+ scope_exit close_fd(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ // Newly created timer should be disarmed.
+ struct itimerspec curr_val = {{1, 1}, {1, 1}};
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_gettime(fd, &curr_val), Succeeds(0));
+ EXPECT_EQ(curr_val.it_value.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_value.tv_nsec, static_cast<NsecT>(0));
+ EXPECT_EQ(curr_val.it_interval.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(curr_val.it_interval.tv_nsec, static_cast<NsecT>(0));
+
+ // Arm the timer with a relative timeout and periodic interval.
+ struct itimerspec new_val = {};
+ new_val.it_value.tv_sec = 60;
+ new_val.it_value.tv_nsec = 0;
+ new_val.it_interval.tv_sec = 2;
+ new_val.it_interval.tv_nsec = 500000000;
+
+ struct itimerspec old_val = {{1, 1}, {1, 1}};
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_settime(fd, 0, &new_val, &old_val),
+ Succeeds(0));
+ EXPECT_EQ(old_val.it_value.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(old_val.it_value.tv_nsec, static_cast<NsecT>(0));
+ EXPECT_EQ(old_val.it_interval.tv_sec, static_cast<time_t>(0));
+ EXPECT_EQ(old_val.it_interval.tv_nsec, static_cast<NsecT>(0));
+
+ ASSERT_THAT(LIBC_NAMESPACE::timerfd_gettime(fd, &curr_val), Succeeds(0));
+ EXP...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/243657
More information about the libc-commits
mailing list