https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/238738
This PR move clock_settime behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. >From 6abfee449cef57f692de19b39258a4a9db0b3be7 Mon Sep 17 00:00:00 2001 From: c8ef <[email protected]> Date: Wed, 4 Nov 2026 18:46:14 +0800 Subject: [PATCH] [libc] Use a syscall wrapper for clock_settime This PR move clock_settime behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. --- .../linux/syscall_wrappers/CMakeLists.txt | 14 ++++++ .../linux/syscall_wrappers/clock_settime.h | 46 +++++++++++++++++++ libc/src/__support/time/linux/CMakeLists.txt | 6 +-- .../__support/time/linux/clock_settime.cpp | 28 +---------- .../llvm-project-overlay/libc/BUILD.bazel | 35 ++++++++++++++ 5 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt index 21300c4d5b630b..4c73e69bf905d2 100644 --- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt @@ -1916,6 +1916,20 @@ add_header_library( libc.src.__support.macros.config ) +add_header_library( + clock_settime + HDRS + clock_settime.h + DEPENDS + libc.hdr.types.clockid_t + libc.hdr.types.struct_timespec + 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( getrusage HDRS diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h new file mode 100644 index 00000000000000..7170163804fea6 --- /dev/null +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// 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_settime syscall wrapper. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_SETTIME_H +#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_SETTIME_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_settime(clockid_t clockid, const timespec *ts) { +#if defined(SYS_clock_settime64) + return syscall_checked<int>(SYS_clock_settime64, clockid, ts); +#elif defined(SYS_clock_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_clock_settime, clockid, ts); +#else +#error "SYS_clock_settime and SYS_clock_settime64 syscalls not available." +#endif +} + +} // namespace linux_syscalls +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_SETTIME_H diff --git a/libc/src/__support/time/linux/CMakeLists.txt b/libc/src/__support/time/linux/CMakeLists.txt index 4a8be1d2db9243..2d5d9f10faa44d 100644 --- a/libc/src/__support/time/linux/CMakeLists.txt +++ b/libc/src/__support/time/linux/CMakeLists.txt @@ -22,10 +22,10 @@ add_object_library( SRCS clock_settime.cpp DEPENDS - libc.include.sys_syscall - libc.hdr.types.struct_timespec libc.hdr.types.clockid_t + libc.hdr.types.struct_timespec + libc.src.__support.OSUtil.linux.syscall_wrappers.clock_settime libc.src.__support.common libc.src.__support.error_or - libc.src.__support.OSUtil.osutil + libc.src.__support.macros.config ) diff --git a/libc/src/__support/time/linux/clock_settime.cpp b/libc/src/__support/time/linux/clock_settime.cpp index ec96828b423520..47af1c5848615c 100644 --- a/libc/src/__support/time/linux/clock_settime.cpp +++ b/libc/src/__support/time/linux/clock_settime.cpp @@ -9,39 +9,15 @@ #include "src/__support/time/clock_settime.h" #include "hdr/types/clockid_t.h" #include "hdr/types/struct_timespec.h" -#include "src/__support/OSUtil/syscall.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h" #include "src/__support/common.h" #include "src/__support/error_or.h" #include "src/__support/macros/config.h" -#include <sys/syscall.h> - -#if defined(SYS_clock_settime64) -#include <linux/time_types.h> -#endif namespace LIBC_NAMESPACE_DECL { namespace internal { ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) { - int ret; -#if defined(SYS_clock_settime64) - ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64, - static_cast<long>(clockid), - reinterpret_cast<long>(ts)); -#elif defined(SYS_clock_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."); - ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime, - static_cast<long>(clockid), - reinterpret_cast<long>(ts)); -#else -#error "SYS_clock_settime and SYS_clock_settime64 syscalls not available." -#endif - if (ret < 0) - return Error(-ret); - return ret; + return linux_syscalls::clock_settime(clockid, ts); } } // namespace internal diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index cb35dc10dd84cf..e5c15197ed7005 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -4375,6 +4375,23 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_osutil_linux_syscall_wrappers_clock_settime", + hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/clock_settime.h"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + deps = [ + ":__support_common", + ":__support_error_or", + ":__support_macros_config", + ":__support_osutil_syscall", + ":types_clockid_t", + ":types_struct_timespec", + ], +) + libc_support_library( name = "__support_osutil_linux_syscall_wrappers_close", hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/close.h"], @@ -5425,6 +5442,24 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_time_clock_settime", + srcs = ["src/__support/time/linux/clock_settime.cpp"], + hdrs = ["src/__support/time/clock_settime.h"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + deps = [ + ":__support_common", + ":__support_error_or", + ":__support_macros_config", + ":__support_osutil_linux_syscall_wrappers_clock_settime", + ":types_clockid_t", + ":types_struct_timespec", + ], +) + libc_support_library( name = "__support_wctype_utils", hdrs = ["src/__support/wctype_utils.h"], _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
