[libc-commits] [libc] [libc] Implement clock_getres in time (PR #230826)

Aman Maurya via libc-commits libc-commits at lists.llvm.org
Sat Oct 10 11:34:34 PDT 2026


https://github.com/amanmaurya92 created https://github.com/llvm/llvm-project/pull/230826

Implement the standard POSIX.1-2008 / POSIX.1-2024 function `clock_getres` in `<time.h>` on Linux and register it in the Linux target entrypoints.

Fixes #230532

>From a8921d9bccd8ab1725be03ba30f1339c823e8e3d Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 10 Oct 2026 23:58:49 +0530
Subject: [PATCH] [libc] Implement clock_getres in time

Implement the standard POSIX.1-2008 / POSIX.1-2024 function clock_getres in <time.h> on Linux and register it in the Linux target entrypoints.

Fixes #230532
---
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/include/time.yaml                    |  7 +++++
 libc/src/time/linux/CMakeLists.txt        | 16 ++++++++++++
 libc/src/time/linux/clock_getres.cpp      | 32 +++++++++++++++++++++++
 libc/test/src/time/CMakeLists.txt         |  2 ++
 libc/test/src/time/clock_getres_test.cpp  |  3 ++-
 8 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/time/linux/clock_getres.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b8a9a5cff2040..9dfb59a6f00bf 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1423,6 +1423,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.ctime_r
     libc.src.time.clock
     libc.src.time.clock_getcpuclockid
+    libc.src.time.clock_getres
     libc.src.time.clock_gettime
     libc.src.time.clock_settime
     libc.src.time.difftime
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 0728bd41a40f9..0108a3c27b40e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1609,6 +1609,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.ctime_r
     libc.src.time.clock
     libc.src.time.clock_getcpuclockid
+    libc.src.time.clock_getres
     libc.src.time.clock_gettime
     libc.src.time.clock_settime
     libc.src.time.difftime
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2df3fb0dfb363..ff740bebd2c4e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1635,6 +1635,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.ctime_r
     libc.src.time.clock
     libc.src.time.clock_getcpuclockid
+    libc.src.time.clock_getres
     libc.src.time.clock_gettime
     libc.src.time.clock_settime
     libc.src.time.difftime
diff --git a/libc/include/time.yaml b/libc/include/time.yaml
index 44044604a6477..837b844579d27 100644
--- a/libc/include/time.yaml
+++ b/libc/include/time.yaml
@@ -77,6 +77,13 @@ functions:
     return_type: clock_t
     arguments:
       - type: void
+  - name: clock_getres
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: clockid_t
+      - type: struct timespec *
   - name: clock_gettime
     standards:
       - posix
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index c16717a2acfaa..a868c06d73d33 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -47,6 +47,22 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  clock_getres
+  SRCS
+    clock_getres.cpp
+  HDRS
+    ../clock_getres.h
+  DEPENDS
+    libc.hdr.types.clockid_t
+    libc.hdr.types.struct_timespec
+    libc.src.__support.OSUtil.linux.syscall_wrappers.clock_getres
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   clock_gettime
   SRCS
diff --git a/libc/src/time/linux/clock_getres.cpp b/libc/src/time/linux/clock_getres.cpp
new file mode 100644
index 0000000000000..c3eb2e812d38c
--- /dev/null
+++ b/libc/src/time/linux/clock_getres.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 clock_getres function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/time/clock_getres.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/clock_getres.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, clock_getres, (clockid_t clockid, timespec *tp)) {
+  auto result = linux_syscalls::clock_getres(clockid, tp);
+  if (!result) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index f04e0b1425e82..3f231c5fd793f 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -142,6 +142,8 @@ add_libc_test(
   SRCS
     clock_getres_test.cpp
   DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.time_macros
     libc.src.time.clock_getres
 )
 
diff --git a/libc/test/src/time/clock_getres_test.cpp b/libc/test/src/time/clock_getres_test.cpp
index d8b3f01b37a38..117900253c018 100644
--- a/libc/test/src/time/clock_getres_test.cpp
+++ b/libc/test/src/time/clock_getres_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for clock_getres- ---------------------------------------===//
+//===-- Unittests for clock_getres ----------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/errno_macros.h"
 #include "hdr/time_macros.h"
 #include "src/time/clock_getres.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"



More information about the libc-commits mailing list