Skip to content

Commit 8ac928f

Browse files
[libc][NFC] adjust time related implementations (#91485)
1 parent 75ebcbf commit 8ac928f

24 files changed

+335
-71
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,13 @@ add_proxy_header_library(
6868
libc.include.llvm-libc-macros.sys_epoll_macros
6969
)
7070

71+
add_proxy_header_library(
72+
time_macros
73+
HDRS
74+
time_macros.h
75+
FULL_BUILD_DEPENDS
76+
libc.include.time
77+
libc.include.llvm-libc-macros.time_macros
78+
)
79+
7180
add_subdirectory(types)

libc/hdr/time_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from time.h ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TIME_MACROS_H
10+
#define LLVM_LIBC_HDR_TIME_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/time-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <time.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TIME_MACROS_H

libc/hdr/types/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,48 @@ add_proxy_header_library(
6363
libc.include.llvm-libc-types.fexcept_t
6464
libc.include.fenv
6565
)
66+
67+
add_proxy_header_library(
68+
time_t
69+
HDRS
70+
time_t.h
71+
FULL_BUILD_DEPENDS
72+
libc.include.llvm-libc-types.time_t
73+
libc.include.time
74+
)
75+
76+
add_proxy_header_library(
77+
clockid_t
78+
HDRS
79+
clockid_t.h
80+
FULL_BUILD_DEPENDS
81+
libc.include.llvm-libc-types.clockid_t
82+
libc.include.sys_types
83+
)
84+
85+
add_proxy_header_library(
86+
clock_t
87+
HDRS
88+
clock_t.h
89+
FULL_BUILD_DEPENDS
90+
libc.include.llvm-libc-types.clock_t
91+
libc.include.time
92+
)
93+
94+
add_proxy_header_library(
95+
suseconds_t
96+
HDRS
97+
suseconds_t.h
98+
FULL_BUILD_DEPENDS
99+
libc.include.llvm-libc-types.suseconds_t
100+
libc.include.sys_time
101+
)
102+
103+
add_proxy_header_library(
104+
struct_timeval
105+
HDRS
106+
struct_timeval.h
107+
FULL_BUILD_DEPENDS
108+
libc.include.llvm-libc-types.struct_timeval
109+
libc.include.sys_time
110+
)

libc/hdr/types/clock_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Proxy for clock_t -------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TYPES_CLOCK_T_H
10+
#define LLVM_LIBC_HDR_TYPES_CLOCK_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/clock_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sys/types.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_CLOCK_T_H

libc/hdr/types/clockid_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Proxy for clockid_t -----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TYPES_CLOCKID_T_H
10+
#define LLVM_LIBC_HDR_TYPES_CLOCKID_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/clockid_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sys/types.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_CLOCKID_T_H

libc/hdr/types/struct_timeval.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for struct timeval ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_TIMEVAL_H
9+
#define LLVM_LIBC_HDR_TYPES_STRUCT_TIMEVAL_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/struct_timeval.h"
14+
15+
#else
16+
17+
#include <sys/time.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_TIMEVAL_H

libc/hdr/types/suseconds_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Proxy for suseconds_t ---------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TIMES_SUSECONDS_T_H
10+
#define LLVM_LIBC_HDR_TIMES_SUSECONDS_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/suseconds_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sys/types.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // #ifndef LLVM_LIBC_HDR_TIMES_SUSECONDS_T_H

libc/hdr/types/time_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Proxy for time_t --------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TYPES_TIME_T_H
10+
#define LLVM_LIBC_HDR_TYPES_TIME_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/time_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include <time.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_TIME_T_H

libc/src/__support/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,5 @@ add_subdirectory(File)
281281
add_subdirectory(HashTable)
282282

283283
add_subdirectory(fixed_point)
284+
285+
add_subdirectory(time)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${LIBC_TARGET_OS})
3+
endif()
4+
5+
add_object_library(
6+
clock_gettime
7+
ALIAS
8+
DEPENDS
9+
.${LIBC_TARGET_OS}.clock_gettime
10+
)
11+
12+
add_header_library(
13+
units
14+
HDRS
15+
units.h
16+
DEPENDS
17+
libc.src.__support.common
18+
libc.hdr.types.time_t
19+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--- clock_gettime internal implementation ------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
10+
#define LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
11+
#include "hdr/types/clockid_t.h"
12+
#include "hdr/types/struct_timespec.h"
13+
#include "src/__support/common.h"
14+
15+
#include "src/__support/error_or.h"
16+
17+
namespace LIBC_NAMESPACE {
18+
namespace internal {
19+
ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts);
20+
}
21+
} // namespace LIBC_NAMESPACE
22+
23+
#endif // LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_object_library(
2+
clock_gettime
3+
HDRS
4+
../clock_gettime.h
5+
SRCS
6+
clock_gettime.cpp
7+
DEPENDS
8+
libc.include.sys_syscall
9+
libc.hdr.types.struct_timespec
10+
libc.hdr.types.clockid_t
11+
libc.src.__support.common
12+
libc.src.__support.error_or
13+
libc.src.__support.OSUtil.osutil
14+
)
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
//===- Linux implementation of the POSIX clock_gettime function -*- C++ -*-===//
1+
//===--- clock_gettime linux implementation ---------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
10-
#define LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
11-
12-
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
13-
#include "src/__support/common.h"
14-
#include "src/__support/error_or.h"
15-
#include "src/errno/libc_errno.h"
16-
17-
#include <stdint.h> // For int64_t.
18-
#include <sys/syscall.h> // For syscall numbers.
19-
#include <time.h>
20-
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
10+
#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
11+
#include "src/__support/time/clock_gettime.h"
12+
#include "src/__support/OSUtil/syscall.h"
13+
#include <sys/syscall.h>
2114
namespace LIBC_NAMESPACE {
2215
namespace internal {
23-
24-
LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
25-
struct timespec *ts) {
16+
ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
2617
#if SYS_clock_gettime
2718
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime,
2819
static_cast<long>(clockid),
@@ -45,4 +36,4 @@ LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
4536
} // namespace internal
4637
} // namespace LIBC_NAMESPACE
4738

48-
#endif // LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_H
39+
#endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H

libc/src/__support/time/units.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- Time units conversion ----------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_UNITS_H
10+
#define LLVM_LIBC_SRC___SUPPORT_TIME_UNITS_H
11+
12+
#include "hdr/types/time_t.h"
13+
#include "src/__support/common.h"
14+
15+
namespace LIBC_NAMESPACE {
16+
namespace time_units {
17+
LIBC_INLINE constexpr time_t operator""_s_ns(unsigned long long s) {
18+
return s * 1'000'000'000;
19+
}
20+
LIBC_INLINE constexpr time_t operator""_s_us(unsigned long long s) {
21+
return s * 1'000'000;
22+
}
23+
LIBC_INLINE constexpr time_t operator""_s_ms(unsigned long long s) {
24+
return s * 1'000;
25+
}
26+
LIBC_INLINE constexpr time_t operator""_ms_ns(unsigned long long ms) {
27+
return ms * 1'000'000;
28+
}
29+
LIBC_INLINE constexpr time_t operator""_ms_us(unsigned long long ms) {
30+
return ms * 1'000;
31+
}
32+
LIBC_INLINE constexpr time_t operator""_us_ns(unsigned long long us) {
33+
return us * 1'000;
34+
}
35+
} // namespace time_units
36+
} // namespace LIBC_NAMESPACE
37+
38+
#endif // LLVM_LIBC_SRC___SUPPORT_TIME_UNITS_H

libc/src/time/clock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_TIME_CLOCK_H
1010
#define LLVM_LIBC_SRC_TIME_CLOCK_H
1111

12-
#include <time.h>
12+
#include "hdr/types/clock_t.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

libc/src/time/clock_gettime.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
#ifndef LLVM_LIBC_SRC_TIME_CLOCK_GETTIME_H
1010
#define LLVM_LIBC_SRC_TIME_CLOCK_GETTIME_H
1111

12-
#include <time.h>
12+
#include "hdr/types/clockid_t.h"
13+
#include "hdr/types/struct_timespec.h"
1314

1415
namespace LIBC_NAMESPACE {
1516

16-
int clock_gettime(clockid_t clockid, struct timespec *tp);
17+
int clock_gettime(clockid_t clockid, timespec *tp);
1718

1819
} // namespace LIBC_NAMESPACE
1920

libc/src/time/gettimeofday.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_TIME_GETTIMEOFDAY_H
1010
#define LLVM_LIBC_SRC_TIME_GETTIMEOFDAY_H
1111

12-
#include <time.h>
12+
#include "hdr/types/struct_timeval.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

0 commit comments

Comments
 (0)