[libc-commits] [libc] [libc][time] Add timer_settime and timer_gettime functions. (PR #219360)

via libc-commits libc-commits at lists.llvm.org
Thu Aug 27 21:32:20 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: lntue

<details>
<summary>Changes</summary>

Follow their documentation at https://man7.org/linux/man-pages/man2/timer_settime.2.html.

Assisted-by: Gemini

---

Patch is 22.41 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/219360.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/llvm-libc-macros/linux/time-macros.h (+3) 
- (modified) libc/include/sys/syscall.h.def (+8) 
- (modified) libc/include/time.yaml (+18) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+28) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/timer_gettime.h (+49) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/timer_settime.h (+50) 
- (modified) libc/src/time/CMakeLists.txt (+14) 
- (modified) libc/src/time/linux/CMakeLists.txt (+32) 
- (added) libc/src/time/linux/timer_gettime.cpp (+31) 
- (added) libc/src/time/linux/timer_settime.cpp (+34) 
- (added) libc/src/time/timer_gettime.h (+27) 
- (added) libc/src/time/timer_settime.h (+28) 
- (modified) libc/test/src/time/CMakeLists.txt (+4) 
- (modified) libc/test/src/time/timer_test.cpp (+138-3) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 39d2588794d65..360ec61755ed3 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1339,6 +1339,8 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.timespec_get
     libc.src.time.timer_create
     libc.src.time.timer_delete
+    libc.src.time.timer_gettime
+    libc.src.time.timer_settime
 
     # unistd.h entrypoints
     libc.src.unistd.__llvm_libc_syscall
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 40fef13470322..4753b82e3b08f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1535,6 +1535,8 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.timespec_get
     libc.src.time.timer_create
     libc.src.time.timer_delete
+    libc.src.time.timer_gettime
+    libc.src.time.timer_settime
 
     # locale.h entrypoints
     libc.src.locale.localeconv
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 53f169f18ea03..113a0c7d2a711 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1548,6 +1548,8 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.timespec_get
     libc.src.time.timer_create
     libc.src.time.timer_delete
+    libc.src.time.timer_gettime
+    libc.src.time.timer_settime
 
     # locale.h entrypoints
     libc.src.locale.localeconv
diff --git a/libc/include/llvm-libc-macros/linux/time-macros.h b/libc/include/llvm-libc-macros/linux/time-macros.h
index 407a1eb30eeaf..a779f4bb60ee9 100644
--- a/libc/include/llvm-libc-macros/linux/time-macros.h
+++ b/libc/include/llvm-libc-macros/linux/time-macros.h
@@ -23,4 +23,7 @@
 
 #define CLOCKS_PER_SEC 1000000
 
+// timer flags
+#define TIMER_ABSTIME 1
+
 #endif // LLVM_LIBC_MACROS_LINUX_TIME_MACROS_H
diff --git a/libc/include/sys/syscall.h.def b/libc/include/sys/syscall.h.def
index 7235d33f50ee1..f3670a234c638 100644
--- a/libc/include/sys/syscall.h.def
+++ b/libc/include/sys/syscall.h.def
@@ -2221,10 +2221,18 @@
 #define SYS_timer_gettime __NR_timer_gettime
 #endif
 
+#ifdef __NR_timer_gettime64
+#define SYS_timer_gettime64 __NR_timer_gettime64
+#endif
+
 #ifdef __NR_timer_settime
 #define SYS_timer_settime __NR_timer_settime
 #endif
 
+#ifdef __NR_timer_settime64
+#define SYS_timer_settime64 __NR_timer_settime64
+#endif
+
 #ifdef __NR_timerfd
 #define SYS_timerfd __NR_timerfd
 #endif
diff --git a/libc/include/time.yaml b/libc/include/time.yaml
index d95ddeefe1cae..44044604a6477 100644
--- a/libc/include/time.yaml
+++ b/libc/include/time.yaml
@@ -6,6 +6,8 @@ macros:
     macro_header: null-macro.h
   - macro_name: CLOCKS_PER_SEC
     macro_header: time-macros.h
+  - macro_name: TIMER_ABSTIME
+    macro_header: time-macros.h
 types:
   - type_name: struct_timeval
   - type_name: clockid_t
@@ -176,6 +178,22 @@ functions:
     return_type: int
     arguments:
       - type: timer_t
+  - name: timer_gettime
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: timer_t
+      - type: struct itimerspec *
+  - name: timer_settime
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: timer_t
+      - type: int
+      - type: const struct itimerspec *__restrict
+      - type: struct itimerspec *__restrict
   - name: tzset
     standards:
       - posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 7d09f46696308..30fbda86f6ba1 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1040,6 +1040,34 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  timer_gettime
+  HDRS
+    timer_gettime.h
+  DEPENDS
+    libc.hdr.types.struct_itimerspec
+    libc.hdr.types.timer_t
+    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(
+  timer_settime
+  HDRS
+    timer_settime.h
+  DEPENDS
+    libc.hdr.types.struct_itimerspec
+    libc.hdr.types.timer_t
+    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(
   clock_getres
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timer_gettime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timer_gettime.h
new file mode 100644
index 0000000000000..ae31d1a498c00
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timer_gettime.h
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 timer_gettime syscall wrapper.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMER_GETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMER_GETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "hdr/types/timer_t.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> timer_gettime(timer_t timerid, itimerspec *val) {
+#if defined(SYS_timer_gettime64)
+  static_assert(
+      sizeof(time_t) == sizeof(int64_t),
+      "SYS_timer_gettime64 requires struct timespec with 64-bit members.");
+  return syscall_checked<int>(SYS_timer_gettime64, timerid, val);
+#elif defined(SYS_timer_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_timer_gettime, timerid, val);
+#else
+#error "SYS_timer_gettime and SYS_timer_gettime64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMER_GETTIME_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/timer_settime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/timer_settime.h
new file mode 100644
index 0000000000000..d7a44da40546d
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/timer_settime.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
+/// Implementation header for timer_settime syscall wrapper.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMER_SETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMER_SETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "hdr/types/timer_t.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> timer_settime(timer_t timerid, int flags,
+                                       const itimerspec *val, itimerspec *old) {
+#if defined(SYS_timer_settime64)
+  static_assert(
+      sizeof(time_t) == sizeof(int64_t),
+      "SYS_timer_settime64 requires struct timespec with 64-bit members.");
+  return syscall_checked<int>(SYS_timer_settime64, timerid, flags, val, old);
+#elif defined(SYS_timer_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_timer_settime, timerid, flags, val, old);
+#else
+#error "SYS_timer_settime and SYS_timer_settime64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_TIMER_SETTIME_H
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index 61efb550ded5b..fcf25bf7e67f1 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -314,6 +314,20 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.timer_delete
 )
 
+add_entrypoint_object(
+  timer_gettime
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.timer_gettime
+)
+
+add_entrypoint_object(
+  timer_settime
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.timer_settime
+)
+
 add_entrypoint_object(
   tz_variables
   ALIAS
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index d94cbd5aee78b..c16717a2acfaa 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -105,6 +105,38 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  timer_gettime
+  SRCS
+    timer_gettime.cpp
+  HDRS
+    ../timer_gettime.h
+  DEPENDS
+    libc.hdr.types.struct_itimerspec
+    libc.hdr.types.timer_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.timer_gettime
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  timer_settime
+  SRCS
+    timer_settime.cpp
+  HDRS
+    ../timer_settime.h
+  DEPENDS
+    libc.hdr.types.struct_itimerspec
+    libc.hdr.types.timer_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.timer_settime
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   tz_variables
   SRCS
diff --git a/libc/src/time/linux/timer_gettime.cpp b/libc/src/time/linux/timer_gettime.cpp
new file mode 100644
index 0000000000000..1504bd7970563
--- /dev/null
+++ b/libc/src/time/linux/timer_gettime.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 timer_gettime function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/time/timer_gettime.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/timer_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, timer_gettime, (timer_t timerid, itimerspec *val)) {
+  auto result = linux_syscalls::timer_gettime(timerid, val);
+  if (!result) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/linux/timer_settime.cpp b/libc/src/time/linux/timer_settime.cpp
new file mode 100644
index 0000000000000..dcbc8ebe0dc69
--- /dev/null
+++ b/libc/src/time/linux/timer_settime.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 timer_settime function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/time/timer_settime.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/timer_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, timer_settime,
+                   (timer_t timerid, int flags,
+                    const itimerspec *__restrict val,
+                    itimerspec *__restrict old)) {
+  auto result = linux_syscalls::timer_settime(timerid, flags, val, old);
+  if (!result) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/timer_gettime.h b/libc/src/time/timer_gettime.h
new file mode 100644
index 0000000000000..358e136949485
--- /dev/null
+++ b/libc/src/time/timer_gettime.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 timer_gettime function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_TIMER_GETTIME_H
+#define LLVM_LIBC_SRC_TIME_TIMER_GETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "hdr/types/timer_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timer_gettime(timer_t timerid, itimerspec *val);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_TIMER_GETTIME_H
diff --git a/libc/src/time/timer_settime.h b/libc/src/time/timer_settime.h
new file mode 100644
index 0000000000000..5fcec26de0158
--- /dev/null
+++ b/libc/src/time/timer_settime.h
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 timer_settime function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_TIMER_SETTIME_H
+#define LLVM_LIBC_SRC_TIME_TIMER_SETTIME_H
+
+#include "hdr/types/struct_itimerspec.h"
+#include "hdr/types/timer_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timer_settime(timer_t timerid, int flags, const itimerspec *__restrict val,
+                  itimerspec *__restrict old);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_TIMER_SETTIME_H
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index bb8611f5e59f1..f04e0b1425e82 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -171,10 +171,14 @@ add_libc_test(
     libc.hdr.signal_macros
     libc.hdr.time_macros
     libc.hdr.types.clockid_t
+    libc.hdr.types.struct_itimerspec
     libc.hdr.types.struct_sigevent
     libc.hdr.types.timer_t
+    libc.src.time.clock_gettime
     libc.src.time.timer_create
     libc.src.time.timer_delete
+    libc.src.time.timer_gettime
+    libc.src.time.timer_settime
     libc.src.unistd.gettid
     libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
diff --git a/libc/test/src/time/timer_test.cpp b/libc/test/src/time/timer_test.cpp
index e9b20d9615314..01e504ff1b823 100644
--- a/libc/test/src/time/timer_test.cpp
+++ b/libc/test/src/time/timer_test.cpp
@@ -15,10 +15,14 @@
 #include "hdr/signal_macros.h"
 #include "hdr/time_macros.h"
 #include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_itimerspec.h"
 #include "hdr/types/struct_sigevent.h"
 #include "hdr/types/timer_t.h"
+#include "src/time/clock_gettime.h"
 #include "src/time/timer_create.h"
 #include "src/time/timer_delete.h"
+#include "src/time/timer_gettime.h"
+#include "src/time/timer_settime.h"
 #include "src/unistd/gettid.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
@@ -37,7 +41,7 @@ TEST_F(LlvmLibcTimerTest, NullSigevent) {
 }
 
 TEST_F(LlvmLibcTimerTest, ValidSigeventSigevNone) {
-  struct sigevent se;
+  sigevent se;
   se.sigev_notify = SIGEV_NONE;
   timer_t timerid;
   ASSERT_THAT(LIBC_NAMESPACE::timer_create(CLOCK_MONOTONIC, &se, &timerid),
@@ -46,7 +50,7 @@ TEST_F(LlvmLibcTimerTest, ValidSigeventSigevNone) {
 }
 
 TEST_F(LlvmLibcTimerTest, ValidSigeventSigevSignal) {
-  struct sigevent se;
+  sigevent se;
   se.sigev_notify = SIGEV_SIGNAL;
   se.sigev_signo = SIGALRM;
   se.sigev_value.sival_int = 42;
@@ -58,7 +62,7 @@ TEST_F(LlvmLibcTimerTest, ValidSigeventSigevSignal) {
 
 #ifdef SIGEV_THREAD_ID
 TEST_F(LlvmLibcTimerTest, ValidSigeventSigevThreadId) {
-  struct sigevent se;
+  sigevent se;
   se.sigev_notify = SIGEV_THREAD_ID;
   se.sigev_signo = SIGALRM;
   se.sigev_value.sival_int = 42;
@@ -94,3 +98,134 @@ TEST_F(LlvmLibcTimerTest, CreateAndDelete) {
   ASSERT_THAT(LIBC_NAMESPACE::timer_delete(timerid), Succeeds(0));
   ASSERT_THAT(LIBC_NAMESPACE::timer_delete(timerid), Fails(EINVAL));
 }
+
+TEST_F(LlvmLibcTimerTest, SetTimeRelativeAndDisarm) {
+  timer_t timerid;
+  ASSERT_THAT(LIBC_NAMESPACE::timer_create(CLOCK_REALTIME, nullptr, &timerid),
+              Succeeds(0));
+
+  itimerspec new_val{};
+  new_val.it_value.tv_sec = 10;
+  new_val.it_value.tv_nsec = 0;
+  new_val.it_interval.tv_sec = 1;
+  new_val.it_interval.tv_nsec = 0;
+
+  itimerspec old_val{};
+  ASSERT_THAT(LIBC_NAMESPACE::timer_settime(timerid, 0, &new_val, &old_val),
+              Succeeds(0));
+  // Initially, old_val should be 0 because it was disarmed.
+  ASSERT_EQ(old_val.it_value.tv_sec, static_cast<time_t>(0));
+  ASSERT_EQ(old_val.it_value.tv_nsec,
+            static_cast<decltype(old_val.it_value.tv_nsec)>(0));
+
+  // Disarm the timer by setting it_value to 0
+  itimerspec disarm_val{};
+  ASSERT_THAT(LIBC_NAMESPACE::timer_settime(timerid, 0, &disarm_val, &old_val),
+              Succeeds(0));
+  // old_val should have been armed previously
+  ASSERT_GT(old_val.it_value.tv_sec, static_cast<time_t>(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::timer_delete(timerid), Succeeds(0));
+}
+
+TEST_F(LlvmLibcTimerTest, SetTimeAbsolute) {
+  timer_t timerid;
+  ASSERT_THAT(LIBC_NAMESPACE::timer_create(CLOCK_REALTIME, nullptr, &timerid),
+              Succeeds(0));
+
+  timespec now{};
+  ASSERT_THAT(LIBC_NAMESPACE::clock_gettime(CLOCK_REALTIME, &now), Succeeds(0));
+
+  itimerspec new_val{};
+  new_val.it_value.tv_sec = now.tv_sec + 100;
+  new_val.it_value.tv_nsec = 0;
+
+  ASSERT_THAT(
+      LIBC_NAMESPACE::timer_settime(timerid, TIMER_ABSTIME, &new_val, nullptr),
+      Succeeds(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::timer_delete(timerid), Succeeds(0));
+}
+
+TEST_F(LlvmLibcTimerTest, SetTimeInvalidTimerId) {
+  itimerspec new_val{};
+  new_val.it_value.tv_sec = 1;
+  ASSERT_THAT(LIBC_NAMESPACE::timer_settime(reinterpret_cast<timer_t>(-1), 0,
+                                            &new_val, nullptr),
+              Fails(EINVAL));
+}
+
+TEST_F(LlvmLibcTimerTest, SetTimeNullNewValue) {
+  timer_t timerid;
+  ASSERT_THAT(LIBC_NAMESPACE::timer_create(CLOCK_REALTIME, nullptr, &time...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/219360


More information about the libc-commits mailing list