[libc-commits] [libc] [libc] Implement clock_nanosleep in time (PR #240025)

Aman Maurya via libc-commits libc-commits at lists.llvm.org
Sun Nov 8 10:58:33 PST 2026


https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/240025

>From 45bfd9ec9b01b0f3fe0c58d002d17b8e6359ff9c Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 8 Nov 2026 01:43:32 +0530
Subject: [PATCH 1/2] [libc] Implement clock_nanosleep in time (#240020)

Implement standard POSIX clock_nanosleep in <time.h>.

- Adds clock_nanosleep entrypoint and internal syscall wrapper.
- Returns error numbers directly without modifying errno per POSIX.
- Adds public declaration to libc/include/time.yaml.
- Registers entrypoints across Linux targets (x86_64, aarch64, riscv).
- Adds unit tests in libc/test/src/time/clock_nanosleep_test.cpp.

Fixes #240020.
---
 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                        |  9 ++++
 .../linux/syscall_wrappers/CMakeLists.txt     | 14 +++++
 .../linux/syscall_wrappers/clock_nanosleep.h  | 53 +++++++++++++++++++
 libc/src/time/CMakeLists.txt                  |  7 +++
 libc/src/time/clock_nanosleep.h               | 23 ++++++++
 libc/src/time/linux/CMakeLists.txt            | 14 +++++
 libc/src/time/linux/clock_nanosleep.cpp       | 32 +++++++++++
 libc/test/src/time/CMakeLists.txt             | 16 ++++++
 libc/test/src/time/clock_nanosleep_test.cpp   | 46 ++++++++++++++++
 12 files changed, 217 insertions(+)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/clock_nanosleep.h
 create mode 100644 libc/src/time/clock_nanosleep.h
 create mode 100644 libc/src/time/linux/clock_nanosleep.cpp
 create mode 100644 libc/test/src/time/clock_nanosleep_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8ba560989af6af..f4b76ac70c2223 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1490,6 +1490,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.gmtime
     libc.src.time.gmtime_r
     libc.src.time.mktime
+    libc.src.time.clock_nanosleep
     libc.src.time.nanosleep
     libc.src.time.time
     libc.src.time.timespec_get
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 56813aaf66a91f..a00cfae7fcec49 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1686,6 +1686,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.localtime
     libc.src.time.localtime_r
     libc.src.time.mktime
+    libc.src.time.clock_nanosleep
     libc.src.time.nanosleep
     libc.src.time.strftime
     libc.src.time.strftime_l
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 25b24740400b97..bcc9c39affb043 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1712,6 +1712,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.localtime
     libc.src.time.localtime_r
     libc.src.time.mktime
+    libc.src.time.clock_nanosleep
     libc.src.time.nanosleep
     libc.src.time.strftime
     libc.src.time.strftime_l
diff --git a/libc/include/time.yaml b/libc/include/time.yaml
index d63dc20e46e587..adacfab194f822 100644
--- a/libc/include/time.yaml
+++ b/libc/include/time.yaml
@@ -124,6 +124,15 @@ functions:
     return_type: time_t
     arguments:
       - type: struct tm *
+  - name: clock_nanosleep
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: clockid_t
+      - type: int
+      - type: const struct timespec *
+      - type: struct timespec *
   - name: nanosleep
     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 d89bb6d8df7b14..197c783f16e4aa 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1810,6 +1810,20 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  clock_nanosleep
+  HDRS
+    clock_nanosleep.h
+  DEPENDS
+    libc.hdr.types.clockid_t
+    libc.hdr.types.struct_timespec
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
+
 add_header_library(
   nanosleep
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_nanosleep.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_nanosleep.h
new file mode 100644
index 00000000000000..eddbf9d649a298
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_nanosleep.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
+/// Implementation header for clock_nanosleep syscall wrapper.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_NANOSLEEP_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_NANOSLEEP_H
+
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> clock_nanosleep(clockid_t clockid, int flags,
+                                         const timespec *req, timespec *rem) {
+#if defined(SYS_clock_nanosleep_time64)
+  static_assert(
+      sizeof(time_t) == sizeof(int64_t),
+      "SYS_clock_nanosleep_time64 requires struct timespec with 64-bit "
+      "members.");
+  return syscall_checked<int>(SYS_clock_nanosleep_time64, clockid, flags, req,
+                              rem);
+#elif defined(SYS_clock_nanosleep)
+  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_clock_nanosleep, clockid, flags, req, rem);
+#else
+#error                                                                         \
+    "SYS_clock_nanosleep and SYS_clock_nanosleep_time64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_NANOSLEEP_H
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index 8bf54a46528837..10415731078965 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -272,6 +272,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.clock
 )
 
+add_entrypoint_object(
+  clock_nanosleep
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.clock_nanosleep
+)
+
 add_entrypoint_object(
   nanosleep
   ALIAS
diff --git a/libc/src/time/clock_nanosleep.h b/libc/src/time/clock_nanosleep.h
new file mode 100644
index 00000000000000..98678ecacc1e84
--- /dev/null
+++ b/libc/src/time/clock_nanosleep.h
@@ -0,0 +1,23 @@
+//===-- Implementation header of clock_nanosleep ----------------*- C++-*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_CLOCK_NANOSLEEP_H
+#define LLVM_LIBC_SRC_TIME_CLOCK_NANOSLEEP_H
+
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int clock_nanosleep(clockid_t clockid, int flags, const timespec *req,
+                    timespec *rem);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_CLOCK_NANOSLEEP_H
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index 9ecd82a91be832..74891dce27445a 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -32,6 +32,20 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  clock_nanosleep
+  SRCS
+    clock_nanosleep.cpp
+  HDRS
+    ../clock_nanosleep.h
+  DEPENDS
+    libc.hdr.types.clockid_t
+    libc.hdr.types.struct_timespec
+    libc.src.__support.OSUtil.linux.syscall_wrappers.clock_nanosleep
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   nanosleep
   SRCS
diff --git a/libc/src/time/linux/clock_nanosleep.cpp b/libc/src/time/linux/clock_nanosleep.cpp
new file mode 100644
index 00000000000000..b771f03f9e30c0
--- /dev/null
+++ b/libc/src/time/linux/clock_nanosleep.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_nanosleep function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/time/clock_nanosleep.h"
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/clock_nanosleep.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, clock_nanosleep,
+                   (clockid_t clockid, int flags, const timespec *req,
+                    timespec *rem)) {
+  auto result = linux_syscalls::clock_nanosleep(clockid, flags, req, rem);
+  if (!result.has_value())
+    return result.error();
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index d31e90f9a5b4de..42c77986c42bef 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -265,6 +265,22 @@ add_libc_test(
     libc.src.time.time_constants
 )
 
+add_libc_test(
+  clock_nanosleep_test
+  SUITE
+    libc-time-tests
+  SRCS
+    clock_nanosleep_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.time_macros
+    libc.hdr.types.clockid_t
+    libc.hdr.types.struct_timespec
+    libc.src.time.clock_nanosleep
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.Test
+)
+
 add_libc_test(
   nanosleep_test
   SUITE
diff --git a/libc/test/src/time/clock_nanosleep_test.cpp b/libc/test/src/time/clock_nanosleep_test.cpp
new file mode 100644
index 00000000000000..97955f1f4bfeef
--- /dev/null
+++ b/libc/test/src/time/clock_nanosleep_test.cpp
@@ -0,0 +1,46 @@
+//===-- Unittests for clock_nanosleep -------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/time_macros.h"
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/time/clock_nanosleep.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcClockNanosleep = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcClockNanosleep, InvalidClock) {
+  struct timespec tim = {0, 500};
+  // Invalid clock id should return EINVAL directly without setting errno.
+  int ret = LIBC_NAMESPACE::clock_nanosleep(clockid_t(-1), 0, &tim, nullptr);
+  EXPECT_EQ(ret, EINVAL);
+}
+
+TEST_F(LlvmLibcClockNanosleep, InvalidNsec) {
+  struct timespec tim = {0, 1000000000L}; // tv_nsec >= 10^9
+  int ret = LIBC_NAMESPACE::clock_nanosleep(CLOCK_REALTIME, 0, &tim, nullptr);
+  EXPECT_EQ(ret, EINVAL);
+
+  struct timespec tim_neg = {0, -1};
+  ret = LIBC_NAMESPACE::clock_nanosleep(CLOCK_REALTIME, 0, &tim_neg, nullptr);
+  EXPECT_EQ(ret, EINVAL);
+}
+
+TEST_F(LlvmLibcClockNanosleep, SleepRealtime) {
+  struct timespec tim = {0, 500};
+  int ret = LIBC_NAMESPACE::clock_nanosleep(CLOCK_REALTIME, 0, &tim, nullptr);
+  EXPECT_EQ(ret, 0);
+}
+
+TEST_F(LlvmLibcClockNanosleep, SleepMonotonic) {
+  struct timespec tim = {0, 500};
+  int ret = LIBC_NAMESPACE::clock_nanosleep(CLOCK_MONOTONIC, 0, &tim, nullptr);
+  EXPECT_EQ(ret, 0);
+}

>From 9ec9692b47f94b9b7a0b839dc8cd8da916c6c19c Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Mon, 9 Nov 2026 00:27:51 +0530
Subject: [PATCH 2/2] [libc] Update clock_nanosleep header and test to new file
 header format

---
 libc/src/time/clock_nanosleep.h             | 7 ++++++-
 libc/test/src/time/clock_nanosleep_test.cpp | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/libc/src/time/clock_nanosleep.h b/libc/src/time/clock_nanosleep.h
index 98678ecacc1e84..83c44770c29fbb 100644
--- a/libc/src/time/clock_nanosleep.h
+++ b/libc/src/time/clock_nanosleep.h
@@ -1,10 +1,15 @@
-//===-- Implementation header of clock_nanosleep ----------------*- C++-*-===//
+//===----------------------------------------------------------------------===//
 //
 // 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 clock_nanosleep.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC_TIME_CLOCK_NANOSLEEP_H
 #define LLVM_LIBC_SRC_TIME_CLOCK_NANOSLEEP_H
diff --git a/libc/test/src/time/clock_nanosleep_test.cpp b/libc/test/src/time/clock_nanosleep_test.cpp
index 97955f1f4bfeef..6c62eeb7dce9f2 100644
--- a/libc/test/src/time/clock_nanosleep_test.cpp
+++ b/libc/test/src/time/clock_nanosleep_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for clock_nanosleep -------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Unittests for clock_nanosleep.
+///
+//===----------------------------------------------------------------------===//
 
 #include "hdr/errno_macros.h"
 #include "hdr/time_macros.h"



More information about the libc-commits mailing list