[libc-commits] [libc] [libc][NFC] adjust time related implementations (PR #91485)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Thu May 9 12:23:38 PDT 2024
https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/91485
>From a11bba401f0f080171495383bb130823fec9359c Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Wed, 8 May 2024 11:01:24 -0400
Subject: [PATCH 1/3] [libc][NFC] adjust time related implementations
---
libc/src/time/linux/CMakeLists.txt | 26 ++++++++------
libc/src/time/linux/clock.cpp | 17 ++++------
libc/src/time/linux/clock_gettime.cpp | 8 ++---
...lockGetTimeImpl.h => clock_gettime_impl.h} | 34 +++++++++++++++----
libc/src/time/linux/gettimeofday.cpp | 11 +++---
libc/src/time/linux/time.cpp | 12 +++----
6 files changed, 61 insertions(+), 47 deletions(-)
rename libc/src/time/linux/{clockGetTimeImpl.h => clock_gettime_impl.h} (60%)
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index df79bf5986261..e0008ed7cdf1f 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -1,3 +1,13 @@
+add_header_library(
+ clock_gettime_impl
+ HDRS
+ clock_gettime_impl.h
+ DEPENDS
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.error_or
+)
+
add_entrypoint_object(
time
SRCS
@@ -5,9 +15,8 @@ add_entrypoint_object(
HDRS
../time_func.h
DEPENDS
+ .clock_gettime_impl
libc.include.time
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
@@ -18,10 +27,9 @@ add_entrypoint_object(
HDRS
../clock.h
DEPENDS
+ .clock_gettime_impl
libc.include.time
- libc.include.sys_syscall
libc.src.__support.CPP.limits
- libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
@@ -32,10 +40,10 @@ add_entrypoint_object(
HDRS
../nanosleep.h
DEPENDS
- libc.include.time
libc.include.sys_syscall
- libc.src.__support.CPP.limits
libc.src.__support.OSUtil.osutil
+ libc.include.time
+ libc.src.__support.CPP.limits
libc.src.errno.errno
)
@@ -46,9 +54,8 @@ add_entrypoint_object(
HDRS
../clock_gettime.h
DEPENDS
+ .clock_gettime_impl
libc.include.time
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
@@ -59,8 +66,7 @@ add_entrypoint_object(
HDRS
../gettimeofday.h
DEPENDS
+ .clock_gettime_impl
libc.include.time
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
diff --git a/libc/src/time/linux/clock.cpp b/libc/src/time/linux/clock.cpp
index 1e95f0526bc9c..39606f6d4e988 100644
--- a/libc/src/time/linux/clock.cpp
+++ b/libc/src/time/linux/clock.cpp
@@ -7,21 +7,18 @@
//===----------------------------------------------------------------------===//
#include "src/time/clock.h"
-
#include "src/__support/CPP/limits.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clockGetTimeImpl.h"
-
-#include <sys/syscall.h> // For syscall numbers.
+#include "src/time/linux/clock_gettime_impl.h"
#include <time.h>
namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
+ using namespace internal::time_units;
struct timespec ts;
- auto result = internal::clock_gettimeimpl(CLOCK_PROCESS_CPUTIME_ID, &ts);
+ auto result = internal::clock_gettime_impl(CLOCK_PROCESS_CPUTIME_ID, &ts);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
@@ -34,15 +31,15 @@ LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC;
if (ts.tv_sec > CLOCK_SECS_MAX)
return clock_t(-1);
- if (ts.tv_nsec / 1000000000 > CLOCK_SECS_MAX - ts.tv_sec)
+ if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec)
return clock_t(-1);
// For the integer computation converting tv_nsec to clocks to work
// correctly, we want CLOCKS_PER_SEC to be less than 1000000000.
- static_assert(1000000000 > CLOCKS_PER_SEC,
- "Expected CLOCKS_PER_SEC to be less than 1000000000.");
+ static_assert(1_s_ns > CLOCKS_PER_SEC,
+ "Expected CLOCKS_PER_SEC to be less than 1'000'000'000.");
return clock_t(ts.tv_sec * CLOCKS_PER_SEC +
- ts.tv_nsec / (1000000000 / CLOCKS_PER_SEC));
+ ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC));
}
} // namespace LIBC_NAMESPACE
diff --git a/libc/src/time/linux/clock_gettime.cpp b/libc/src/time/linux/clock_gettime.cpp
index 47e974a866c83..f2943955bc78b 100644
--- a/libc/src/time/linux/clock_gettime.cpp
+++ b/libc/src/time/linux/clock_gettime.cpp
@@ -7,13 +7,9 @@
//===----------------------------------------------------------------------===//
#include "src/time/clock_gettime.h"
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clockGetTimeImpl.h"
-
-#include <sys/syscall.h> // For syscall numbers.
+#include "src/time/linux/clock_gettime_impl.h"
#include <time.h>
namespace LIBC_NAMESPACE {
@@ -21,7 +17,7 @@ namespace LIBC_NAMESPACE {
// TODO(michaelrj): Move this into time/linux with the other syscalls.
LLVM_LIBC_FUNCTION(int, clock_gettime,
(clockid_t clockid, struct timespec *ts)) {
- auto result = internal::clock_gettimeimpl(clockid, ts);
+ auto result = internal::clock_gettime_impl(clockid, ts);
// A negative return value indicates an error with the magnitude of the
// value being the error code.
diff --git a/libc/src/time/linux/clockGetTimeImpl.h b/libc/src/time/linux/clock_gettime_impl.h
similarity index 60%
rename from libc/src/time/linux/clockGetTimeImpl.h
rename to libc/src/time/linux/clock_gettime_impl.h
index 8c8c9fcf845cc..752434018e261 100644
--- a/libc/src/time/linux/clockGetTimeImpl.h
+++ b/libc/src/time/linux/clock_gettime_impl.h
@@ -6,13 +6,13 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
-#define LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
+#ifndef LLVM_LIBC_SRC_TIME_LINUX_CLOCK_GETTIME_IMPL_H
+#define LLVM_LIBC_SRC_TIME_LINUX_CLOCK_GETTIME_IMPL_H
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/error_or.h"
-#include "src/errno/libc_errno.h"
+#include "src/__support/macros/attributes.h"
#include <stdint.h> // For int64_t.
#include <sys/syscall.h> // For syscall numbers.
@@ -21,8 +21,30 @@
namespace LIBC_NAMESPACE {
namespace internal {
-LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
- struct timespec *ts) {
+namespace time_units {
+LIBC_INLINE constexpr time_t operator""_s_ns(unsigned long long s) {
+ return s * 1'000'000'000;
+}
+LIBC_INLINE constexpr time_t operator""_s_us(unsigned long long s) {
+ return s * 1'000'000;
+}
+LIBC_INLINE constexpr time_t operator""_s_ms(unsigned long long s) {
+ return s * 1'000;
+}
+LIBC_INLINE constexpr time_t operator""_ms_ns(unsigned long long ms) {
+ return ms * 1'000'000;
+}
+LIBC_INLINE constexpr time_t operator""_ms_us(unsigned long long ms) {
+ return ms * 1'000;
+}
+LIBC_INLINE constexpr time_t operator""_us_ns(unsigned long long us) {
+ return us * 1'000;
+}
+} // namespace time_units
+// namespace time_units
+
+LIBC_INLINE ErrorOr<int> clock_gettime_impl(clockid_t clockid,
+ struct timespec *ts) {
#if SYS_clock_gettime
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime,
static_cast<long>(clockid),
@@ -45,4 +67,4 @@ LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
} // namespace internal
} // namespace LIBC_NAMESPACE
-#endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
+#endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCK_GETTIME_IMPL_H
diff --git a/libc/src/time/linux/gettimeofday.cpp b/libc/src/time/linux/gettimeofday.cpp
index 07ab4d579176e..bf388a656441f 100644
--- a/libc/src/time/linux/gettimeofday.cpp
+++ b/libc/src/time/linux/gettimeofday.cpp
@@ -7,24 +7,21 @@
//===----------------------------------------------------------------------===//
#include "src/time/gettimeofday.h"
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clockGetTimeImpl.h"
-
-#include <sys/syscall.h> // For syscall numbers.
+#include "src/time/linux/clock_gettime_impl.h"
namespace LIBC_NAMESPACE {
// TODO(michaelrj): Move this into time/linux with the other syscalls.
LLVM_LIBC_FUNCTION(int, gettimeofday,
(struct timeval * tv, [[maybe_unused]] void *unused)) {
+ using namespace internal::time_units;
if (tv == nullptr)
return 0;
struct timespec ts;
- auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, &ts);
+ auto result = internal::clock_gettime_impl(CLOCK_REALTIME, &ts);
// A negative return value indicates an error with the magnitude of the
// value being the error code.
@@ -34,7 +31,7 @@ LLVM_LIBC_FUNCTION(int, gettimeofday,
}
tv->tv_sec = ts.tv_sec;
- tv->tv_usec = static_cast<suseconds_t>(ts.tv_nsec / 1000);
+ tv->tv_usec = static_cast<suseconds_t>(ts.tv_nsec / 1_us_ns);
return 0;
}
diff --git a/libc/src/time/linux/time.cpp b/libc/src/time/linux/time.cpp
index e286fae095b2a..336c723356805 100644
--- a/libc/src/time/linux/time.cpp
+++ b/libc/src/time/linux/time.cpp
@@ -6,22 +6,18 @@
//
//===----------------------------------------------------------------------===//
-#include "src/time/time_func.h"
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clockGetTimeImpl.h"
-
-#include <sys/syscall.h> // For syscall numbers.
+#include "src/time/linux/clock_gettime_impl.h"
+#include "src/time/time_func.h"
#include <time.h>
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
+LLVM_LIBC_FUNCTION(time_t, time, (time_t *tp)) {
// TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
struct timespec ts;
- auto result = internal::clock_gettimeimpl(CLOCK_REALTIME, &ts);
+ auto result = internal::clock_gettime_impl(CLOCK_REALTIME, &ts);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
>From 8bf24f8022668d40a9cee10d81250d957bf0853d Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Wed, 8 May 2024 17:05:07 -0400
Subject: [PATCH 2/3] [libc] clean up more
---
libc/hdr/CMakeLists.txt | 9 +++
libc/hdr/time_macros.h | 22 ++++++
libc/hdr/types/CMakeLists.txt | 40 +++++++++++
libc/hdr/types/clock_t.h | 22 ++++++
libc/hdr/types/clockid_t.h | 22 ++++++
libc/hdr/types/struct_timeval.h | 21 ++++++
libc/hdr/types/suseconds_t.h | 22 ++++++
libc/hdr/types/time_t.h | 22 ++++++
libc/src/__support/CMakeLists.txt | 2 +
libc/src/__support/time/CMakeLists.txt | 19 +++++
libc/src/__support/time/clock_gettime.h | 23 ++++++
libc/src/__support/time/linux/CMakeLists.txt | 14 ++++
.../__support/time/linux/clock_gettime.cpp | 39 +++++++++++
libc/src/__support/time/units.h | 38 ++++++++++
libc/src/time/clock.h | 2 +-
libc/src/time/clock_gettime.h | 5 +-
libc/src/time/gettimeofday.h | 2 +-
libc/src/time/linux/CMakeLists.txt | 34 ++++-----
libc/src/time/linux/clock.cpp | 9 +--
libc/src/time/linux/clock_gettime.cpp | 5 +-
libc/src/time/linux/clock_gettime_impl.h | 70 -------------------
libc/src/time/linux/gettimeofday.cpp | 9 ++-
libc/src/time/linux/time.cpp | 6 +-
libc/src/time/nanosleep.h | 4 +-
libc/src/time/time_func.h | 2 +-
25 files changed, 354 insertions(+), 109 deletions(-)
create mode 100644 libc/hdr/time_macros.h
create mode 100644 libc/hdr/types/clock_t.h
create mode 100644 libc/hdr/types/clockid_t.h
create mode 100644 libc/hdr/types/struct_timeval.h
create mode 100644 libc/hdr/types/suseconds_t.h
create mode 100644 libc/hdr/types/time_t.h
create mode 100644 libc/src/__support/time/CMakeLists.txt
create mode 100644 libc/src/__support/time/clock_gettime.h
create mode 100644 libc/src/__support/time/linux/CMakeLists.txt
create mode 100644 libc/src/__support/time/linux/clock_gettime.cpp
create mode 100644 libc/src/__support/time/units.h
delete mode 100644 libc/src/time/linux/clock_gettime_impl.h
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 179b05e6ee966..7549342514304 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -68,4 +68,13 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.sys_epoll_macros
)
+add_proxy_header_library(
+ time_macros
+ HDRS
+ time_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.time
+ libc.include.llvm-libc-macros.time_macros
+)
+
add_subdirectory(types)
diff --git a/libc/hdr/time_macros.h b/libc/hdr/time_macros.h
new file mode 100644
index 0000000000000..dc36fe66f7a80
--- /dev/null
+++ b/libc/hdr/time_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from time.h ----------------------------------===//
+//
+// 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_HDR_TIME_MACROS_H
+#define LLVM_LIBC_HDR_TIME_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/time-macros.h"
+
+#else // Overlay mode
+
+#include <time.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TIME_MACROS_H
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 46a66ec590202..b9df8c0fd09e9 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -63,3 +63,43 @@ add_proxy_header_library(
libc.include.llvm-libc-types.fexcept_t
libc.include.fenv
)
+
+add_proxy_header_library(
+ time_t
+ HDRS
+ time_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.time_t
+)
+
+add_proxy_header_library(
+ clockid_t
+ HDRS
+ clockid_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.clockid_t
+)
+
+add_proxy_header_library(
+ clock_t
+ HDRS
+ clock_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.clock_t
+)
+
+add_proxy_header_library(
+ suseconds_t
+ HDRS
+ suseconds_t.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.suseconds_t
+)
+
+add_proxy_header_library(
+ struct_timeval
+ HDRS
+ struct_timeval.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_timeval
+)
diff --git a/libc/hdr/types/clock_t.h b/libc/hdr/types/clock_t.h
new file mode 100644
index 0000000000000..b0b658e96c3db
--- /dev/null
+++ b/libc/hdr/types/clock_t.h
@@ -0,0 +1,22 @@
+//===-- Proxy for clock_t -------------------------------------------------===//
+//
+// 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_HDR_TYPES_CLOCK_T_H
+#define LLVM_LIBC_HDR_TYPES_CLOCK_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/clock_t.h"
+
+#else // Overlay mode
+
+#include <sys/types.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_CLOCK_T_H
diff --git a/libc/hdr/types/clockid_t.h b/libc/hdr/types/clockid_t.h
new file mode 100644
index 0000000000000..333342072a2ff
--- /dev/null
+++ b/libc/hdr/types/clockid_t.h
@@ -0,0 +1,22 @@
+//===-- Proxy for clockid_t -----------------------------------------------===//
+//
+// 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_HDR_TYPES_CLOCKID_T_H
+#define LLVM_LIBC_HDR_TYPES_CLOCKID_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/clockid_t.h"
+
+#else // Overlay mode
+
+#include <sys/types.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_CLOCKID_T_H
diff --git a/libc/hdr/types/struct_timeval.h b/libc/hdr/types/struct_timeval.h
new file mode 100644
index 0000000000000..8fc321a52d711
--- /dev/null
+++ b/libc/hdr/types/struct_timeval.h
@@ -0,0 +1,21 @@
+//===-- Proxy for struct timeval ----------------------------------------===//
+//
+// 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_HDR_TYPES_STRUCT_TIMEVAL_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_TIMEVAL_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_timeval.h"
+
+#else
+
+#include <sys/time.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_TIMEVAL_H
diff --git a/libc/hdr/types/suseconds_t.h b/libc/hdr/types/suseconds_t.h
new file mode 100644
index 0000000000000..72e54a965f750
--- /dev/null
+++ b/libc/hdr/types/suseconds_t.h
@@ -0,0 +1,22 @@
+//===-- Proxy for suseconds_t ---------------------------------------------===//
+//
+// 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_HDR_TIMES_SUSECONDS_T_H
+#define LLVM_LIBC_HDR_TIMES_SUSECONDS_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/suseconds_t.h"
+
+#else // Overlay mode
+
+#include <sys/types.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // #ifndef LLVM_LIBC_HDR_TIMES_SUSECONDS_T_H
diff --git a/libc/hdr/types/time_t.h b/libc/hdr/types/time_t.h
new file mode 100644
index 0000000000000..fc9a1506a2cda
--- /dev/null
+++ b/libc/hdr/types/time_t.h
@@ -0,0 +1,22 @@
+//===-- Proxy for time_t --------------------------------------------------===//
+//
+// 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_HDR_TYPES_TIME_T_H
+#define LLVM_LIBC_HDR_TYPES_TIME_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/time_t.h"
+
+#else // Overlay mode
+
+#include <time.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_TIME_T_H
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index dcae55e050bf1..32d693ec6a268 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -281,3 +281,5 @@ add_subdirectory(File)
add_subdirectory(HashTable)
add_subdirectory(fixed_point)
+
+add_subdirectory(time)
diff --git a/libc/src/__support/time/CMakeLists.txt b/libc/src/__support/time/CMakeLists.txt
new file mode 100644
index 0000000000000..36ce4f9dadb2c
--- /dev/null
+++ b/libc/src/__support/time/CMakeLists.txt
@@ -0,0 +1,19 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
+
+add_object_library(
+ clock_gettime
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.clock_gettime
+)
+
+add_header_library(
+ units
+ HDRS
+ units.h
+ DEPENDS
+ libc.src.__support.common
+ libc.hdr.types.time_t
+)
diff --git a/libc/src/__support/time/clock_gettime.h b/libc/src/__support/time/clock_gettime.h
new file mode 100644
index 0000000000000..0655ccdc0028b
--- /dev/null
+++ b/libc/src/__support/time/clock_gettime.h
@@ -0,0 +1,23 @@
+//===--- clock_gettime internal implementation ------------------*- 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___SUPPORT_TIME_CLOCK_GETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/common.h"
+
+#include "src/__support/error_or.h"
+
+namespace LIBC_NAMESPACE {
+namespace internal {
+ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts);
+}
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
diff --git a/libc/src/__support/time/linux/CMakeLists.txt b/libc/src/__support/time/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..952fc8b6b323f
--- /dev/null
+++ b/libc/src/__support/time/linux/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_object_library(
+ clock_gettime
+ HDRS
+ ../clock_gettime.h
+ SRCS
+ clock_gettime.cpp
+ DEPENDS
+ libc.include.sys_syscall
+ libc.hdr.types.struct_timespec
+ libc.hdr.types.clockid_t
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.OSUtil.osutil
+)
\ No newline at end of file
diff --git a/libc/src/__support/time/linux/clock_gettime.cpp b/libc/src/__support/time/linux/clock_gettime.cpp
new file mode 100644
index 0000000000000..6a131df9ba593
--- /dev/null
+++ b/libc/src/__support/time/linux/clock_gettime.cpp
@@ -0,0 +1,39 @@
+//===--- clock_gettime linux implementation ---------------------*- 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___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/OSUtil/syscall.h"
+#include <sys/syscall.h>
+namespace LIBC_NAMESPACE {
+namespace internal {
+ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
+#if SYS_clock_gettime
+ int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime,
+ static_cast<long>(clockid),
+ reinterpret_cast<long>(ts));
+#elif defined(SYS_clock_gettime64)
+ static_assert(
+ sizeof(time_t) == sizeof(int64_t),
+ "SYS_clock_gettime64 requires struct timespec with 64-bit members.");
+ int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64,
+ static_cast<long>(clockid),
+ reinterpret_cast<long>(ts));
+#else
+#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
+#endif
+ if (ret < 0)
+ return Error(-ret);
+ return ret;
+}
+
+} // namespace internal
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
diff --git a/libc/src/__support/time/units.h b/libc/src/__support/time/units.h
new file mode 100644
index 0000000000000..f6bd19f9b1396
--- /dev/null
+++ b/libc/src/__support/time/units.h
@@ -0,0 +1,38 @@
+//===--- Time units conversion ----------------------------------*- 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___SUPPORT_TIME_UNITS_H
+#define LLVM_LIBC_SRC___SUPPORT_TIME_UNITS_H
+
+#include "hdr/types/time_t.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+namespace time_units {
+LIBC_INLINE constexpr time_t operator""_s_ns(unsigned long long s) {
+ return s * 1'000'000'000;
+}
+LIBC_INLINE constexpr time_t operator""_s_us(unsigned long long s) {
+ return s * 1'000'000;
+}
+LIBC_INLINE constexpr time_t operator""_s_ms(unsigned long long s) {
+ return s * 1'000;
+}
+LIBC_INLINE constexpr time_t operator""_ms_ns(unsigned long long ms) {
+ return ms * 1'000'000;
+}
+LIBC_INLINE constexpr time_t operator""_ms_us(unsigned long long ms) {
+ return ms * 1'000;
+}
+LIBC_INLINE constexpr time_t operator""_us_ns(unsigned long long us) {
+ return us * 1'000;
+}
+} // namespace time_units
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC___SUPPORT_TIME_UNITS_H
diff --git a/libc/src/time/clock.h b/libc/src/time/clock.h
index d4af7656644a0..f5d14d036e138 100644
--- a/libc/src/time/clock.h
+++ b/libc/src/time/clock.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_TIME_CLOCK_H
#define LLVM_LIBC_SRC_TIME_CLOCK_H
-#include <time.h>
+#include "hdr/types/clock_t.h"
namespace LIBC_NAMESPACE {
diff --git a/libc/src/time/clock_gettime.h b/libc/src/time/clock_gettime.h
index 72e2e1949feb6..48e81a3554291 100644
--- a/libc/src/time/clock_gettime.h
+++ b/libc/src/time/clock_gettime.h
@@ -9,11 +9,12 @@
#ifndef LLVM_LIBC_SRC_TIME_CLOCK_GETTIME_H
#define LLVM_LIBC_SRC_TIME_CLOCK_GETTIME_H
-#include <time.h>
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
namespace LIBC_NAMESPACE {
-int clock_gettime(clockid_t clockid, struct timespec *tp);
+int clock_gettime(clockid_t clockid, timespec *tp);
} // namespace LIBC_NAMESPACE
diff --git a/libc/src/time/gettimeofday.h b/libc/src/time/gettimeofday.h
index 880b94cee7311..62ee31edcad67 100644
--- a/libc/src/time/gettimeofday.h
+++ b/libc/src/time/gettimeofday.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_TIME_GETTIMEOFDAY_H
#define LLVM_LIBC_SRC_TIME_GETTIMEOFDAY_H
-#include <time.h>
+#include "hdr/types/struct_timeval.h"
namespace LIBC_NAMESPACE {
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index e0008ed7cdf1f..8a0e6b04b66e6 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -1,13 +1,3 @@
-add_header_library(
- clock_gettime_impl
- HDRS
- clock_gettime_impl.h
- DEPENDS
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
- libc.src.__support.error_or
-)
-
add_entrypoint_object(
time
SRCS
@@ -15,8 +5,9 @@ add_entrypoint_object(
HDRS
../time_func.h
DEPENDS
- .clock_gettime_impl
- libc.include.time
+ libc.hdr.time_macros
+ libc.hdr.types.time_t
+ libc.src.__support.time.clock_gettime
libc.src.errno.errno
)
@@ -27,8 +18,10 @@ add_entrypoint_object(
HDRS
../clock.h
DEPENDS
- .clock_gettime_impl
- libc.include.time
+ libc.hdr.time_macros
+ libc.hdr.types.clock_t
+ libc.src.__support.time.units
+ libc.src.__support.time.clock_gettime
libc.src.__support.CPP.limits
libc.src.errno.errno
)
@@ -40,9 +33,9 @@ add_entrypoint_object(
HDRS
../nanosleep.h
DEPENDS
+ libc.hdr.types.struct_timespec
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
- libc.include.time
libc.src.__support.CPP.limits
libc.src.errno.errno
)
@@ -54,8 +47,9 @@ add_entrypoint_object(
HDRS
../clock_gettime.h
DEPENDS
- .clock_gettime_impl
- libc.include.time
+ libc.hdr.types.clockid_t
+ libc.hdr.types.struct_timespec
+ libc.src.__support.time.clock_gettime
libc.src.errno.errno
)
@@ -66,7 +60,9 @@ add_entrypoint_object(
HDRS
../gettimeofday.h
DEPENDS
- .clock_gettime_impl
- libc.include.time
+ libc.hdr.time_macros
+ libc.hdr.types.suseconds_t
+ libc.src.__support.time.clock_gettime
+ libc.src.__support.time.units
libc.src.errno.errno
)
diff --git a/libc/src/time/linux/clock.cpp b/libc/src/time/linux/clock.cpp
index 39606f6d4e988..fc48e2792747d 100644
--- a/libc/src/time/linux/clock.cpp
+++ b/libc/src/time/linux/clock.cpp
@@ -7,18 +7,19 @@
//===----------------------------------------------------------------------===//
#include "src/time/clock.h"
+#include "hdr/time_macros.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/time/units.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clock_gettime_impl.h"
-#include <time.h>
namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
- using namespace internal::time_units;
+ using namespace time_units;
struct timespec ts;
- auto result = internal::clock_gettime_impl(CLOCK_PROCESS_CPUTIME_ID, &ts);
+ auto result = internal::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
diff --git a/libc/src/time/linux/clock_gettime.cpp b/libc/src/time/linux/clock_gettime.cpp
index f2943955bc78b..920363e85e06c 100644
--- a/libc/src/time/linux/clock_gettime.cpp
+++ b/libc/src/time/linux/clock_gettime.cpp
@@ -8,16 +8,15 @@
#include "src/time/clock_gettime.h"
#include "src/__support/common.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clock_gettime_impl.h"
-#include <time.h>
namespace LIBC_NAMESPACE {
// TODO(michaelrj): Move this into time/linux with the other syscalls.
LLVM_LIBC_FUNCTION(int, clock_gettime,
(clockid_t clockid, struct timespec *ts)) {
- auto result = internal::clock_gettime_impl(clockid, ts);
+ auto result = internal::clock_gettime(clockid, ts);
// A negative return value indicates an error with the magnitude of the
// value being the error code.
diff --git a/libc/src/time/linux/clock_gettime_impl.h b/libc/src/time/linux/clock_gettime_impl.h
deleted file mode 100644
index 752434018e261..0000000000000
--- a/libc/src/time/linux/clock_gettime_impl.h
+++ /dev/null
@@ -1,70 +0,0 @@
-//===- Linux implementation of the POSIX clock_gettime function -*- 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_LINUX_CLOCK_GETTIME_IMPL_H
-#define LLVM_LIBC_SRC_TIME_LINUX_CLOCK_GETTIME_IMPL_H
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-#include "src/__support/common.h"
-#include "src/__support/error_or.h"
-#include "src/__support/macros/attributes.h"
-
-#include <stdint.h> // For int64_t.
-#include <sys/syscall.h> // For syscall numbers.
-#include <time.h>
-
-namespace LIBC_NAMESPACE {
-namespace internal {
-
-namespace time_units {
-LIBC_INLINE constexpr time_t operator""_s_ns(unsigned long long s) {
- return s * 1'000'000'000;
-}
-LIBC_INLINE constexpr time_t operator""_s_us(unsigned long long s) {
- return s * 1'000'000;
-}
-LIBC_INLINE constexpr time_t operator""_s_ms(unsigned long long s) {
- return s * 1'000;
-}
-LIBC_INLINE constexpr time_t operator""_ms_ns(unsigned long long ms) {
- return ms * 1'000'000;
-}
-LIBC_INLINE constexpr time_t operator""_ms_us(unsigned long long ms) {
- return ms * 1'000;
-}
-LIBC_INLINE constexpr time_t operator""_us_ns(unsigned long long us) {
- return us * 1'000;
-}
-} // namespace time_units
-// namespace time_units
-
-LIBC_INLINE ErrorOr<int> clock_gettime_impl(clockid_t clockid,
- struct timespec *ts) {
-#if SYS_clock_gettime
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime,
- static_cast<long>(clockid),
- reinterpret_cast<long>(ts));
-#elif defined(SYS_clock_gettime64)
- static_assert(
- sizeof(time_t) == sizeof(int64_t),
- "SYS_clock_gettime64 requires struct timespec with 64-bit members.");
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64,
- static_cast<long>(clockid),
- reinterpret_cast<long>(ts));
-#else
-#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
-#endif
- if (ret < 0)
- return Error(-ret);
- return ret;
-}
-
-} // namespace internal
-} // namespace LIBC_NAMESPACE
-
-#endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCK_GETTIME_IMPL_H
diff --git a/libc/src/time/linux/gettimeofday.cpp b/libc/src/time/linux/gettimeofday.cpp
index bf388a656441f..c7bcd45e01fa9 100644
--- a/libc/src/time/linux/gettimeofday.cpp
+++ b/libc/src/time/linux/gettimeofday.cpp
@@ -7,21 +7,24 @@
//===----------------------------------------------------------------------===//
#include "src/time/gettimeofday.h"
+#include "hdr/time_macros.h"
+#include "hdr/types/suseconds_t.h"
#include "src/__support/common.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/time/units.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clock_gettime_impl.h"
namespace LIBC_NAMESPACE {
// TODO(michaelrj): Move this into time/linux with the other syscalls.
LLVM_LIBC_FUNCTION(int, gettimeofday,
(struct timeval * tv, [[maybe_unused]] void *unused)) {
- using namespace internal::time_units;
+ using namespace time_units;
if (tv == nullptr)
return 0;
struct timespec ts;
- auto result = internal::clock_gettime_impl(CLOCK_REALTIME, &ts);
+ auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);
// A negative return value indicates an error with the magnitude of the
// value being the error code.
diff --git a/libc/src/time/linux/time.cpp b/libc/src/time/linux/time.cpp
index 336c723356805..80fda1c3f5bfa 100644
--- a/libc/src/time/linux/time.cpp
+++ b/libc/src/time/linux/time.cpp
@@ -6,18 +6,18 @@
//
//===----------------------------------------------------------------------===//
+#include "hdr/time_macros.h"
#include "src/__support/common.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/errno/libc_errno.h"
-#include "src/time/linux/clock_gettime_impl.h"
#include "src/time/time_func.h"
-#include <time.h>
namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(time_t, time, (time_t *tp)) {
// TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
struct timespec ts;
- auto result = internal::clock_gettime_impl(CLOCK_REALTIME, &ts);
+ auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
diff --git a/libc/src/time/nanosleep.h b/libc/src/time/nanosleep.h
index 757394232c079..2309666b2304b 100644
--- a/libc/src/time/nanosleep.h
+++ b/libc/src/time/nanosleep.h
@@ -9,11 +9,11 @@
#ifndef LLVM_LIBC_SRC_TIME_NANOSLEEP_H
#define LLVM_LIBC_SRC_TIME_NANOSLEEP_H
-#include <time.h>
+#include "hdr/types/struct_timespec.h"
namespace LIBC_NAMESPACE {
-int nanosleep(const struct timespec *req, struct timespec *rem);
+int nanosleep(const timespec *req, timespec *rem);
} // namespace LIBC_NAMESPACE
diff --git a/libc/src/time/time_func.h b/libc/src/time/time_func.h
index beb02020b5759..2a52392209424 100644
--- a/libc/src/time/time_func.h
+++ b/libc/src/time/time_func.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_TIME_TIME_FUNC_H
#define LLVM_LIBC_SRC_TIME_TIME_FUNC_H
-#include <time.h>
+#include "hdr/types/time_t.h"
// Note this header file is named time_func.h to avoid conflicts with the
// public header file time.h.
>From 9724ea6d0441c58c1bb42f479f6c93fd37fb6b1e Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Thu, 9 May 2024 15:23:24 -0400
Subject: [PATCH 3/3] address CR
---
libc/hdr/types/CMakeLists.txt | 5 +++++
libc/src/__support/time/linux/CMakeLists.txt | 2 +-
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index b9df8c0fd09e9..3a1bb2f3c340f 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -70,6 +70,7 @@ add_proxy_header_library(
time_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.time_t
+ libc.include.time
)
add_proxy_header_library(
@@ -78,6 +79,7 @@ add_proxy_header_library(
clockid_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.clockid_t
+ libc.include.sys_types
)
add_proxy_header_library(
@@ -86,6 +88,7 @@ add_proxy_header_library(
clock_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.clock_t
+ libc.include.time
)
add_proxy_header_library(
@@ -94,6 +97,7 @@ add_proxy_header_library(
suseconds_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.suseconds_t
+ libc.include.sys_time
)
add_proxy_header_library(
@@ -102,4 +106,5 @@ add_proxy_header_library(
struct_timeval.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_timeval
+ libc.include.sys_time
)
diff --git a/libc/src/__support/time/linux/CMakeLists.txt b/libc/src/__support/time/linux/CMakeLists.txt
index 952fc8b6b323f..034fa317ff6df 100644
--- a/libc/src/__support/time/linux/CMakeLists.txt
+++ b/libc/src/__support/time/linux/CMakeLists.txt
@@ -11,4 +11,4 @@ add_object_library(
libc.src.__support.common
libc.src.__support.error_or
libc.src.__support.OSUtil.osutil
-)
\ No newline at end of file
+)
More information about the libc-commits
mailing list