Skip to content

Commit 22ea0e5

Browse files
author
Siva Chandra Reddy
committed
[libc] Add Linux implementations of time and clock functions.
Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D136666
1 parent 20204db commit 22ea0e5

File tree

16 files changed

+276
-4
lines changed

16 files changed

+276
-4
lines changed

libc/config/linux/api.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def StdlibAPI : PublicAPI<"stdlib.h"> {
172172
}
173173

174174
def TimeAPI : PublicAPI<"time.h"> {
175-
let Types = ["time_t", "struct tm", "struct timespec", "clockid_t",];
175+
let Types = ["clock_t", "time_t", "struct tm", "struct timespec", "clockid_t",];
176176
}
177177

178178
def ErrnoAPI : PublicAPI<"errno.h"> {

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,14 @@ if(LLVM_LIBC_FULL_BUILD)
443443
# time.h entrypoints
444444
libc.src.time.asctime
445445
libc.src.time.asctime_r
446+
libc.src.time.clock_gettime
447+
libc.src.time.clock
446448
libc.src.time.difftime
447449
libc.src.time.gmtime
448450
libc.src.time.gmtime_r
449451
libc.src.time.mktime
450452
libc.src.time.nanosleep
451-
libc.src.time.clock_gettime
453+
libc.src.time.time
452454

453455
# unistd.h entrypoints
454456
libc.src.unistd.environ

libc/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ add_gen_header(
9393
.llvm_libc_common_h
9494
.llvm-libc-macros.time_macros
9595
.llvm-libc-types.time_t
96+
.llvm-libc-types.clock_t
9697
.llvm-libc-types.clockid_t
9798
.llvm-libc-types.struct_tm
9899
.llvm-libc-types.struct_timespec

libc/include/llvm-libc-macros/linux/time-macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
#define CLOCK_REALTIME_ALARM 8
2222
#define CLOCK_BOOTTIME_ALARM 9
2323

24+
#define CLOCKS_PER_SEC 1000000
25+
2426
#endif //__LLVM_LIBC_MACROS_LINUX_TIME_MACROS_H

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_header(__thread_type HDR __thread_type.h)
1616
add_header(blkcnt_t HDR blkcnt_t.h)
1717
add_header(blksize_t HDR blksize_t.h)
1818
add_header(cc_t HDR cc_t.h)
19+
add_header(clock_t HDR clock_t.h)
1920
add_header(clockid_t HDR clockid_t.h)
2021
add_header(cnd_t HDR cnd_t.h)
2122
add_header(cookie_io_functions_t HDR cookie_io_functions_t.h DEPENDS .off64_t)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Definition of clock_t type ----------------------------------------===//
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_TYPES_CLOCK_T_H__
10+
#define __LLVM_LIBC_TYPES_CLOCK_T_H__
11+
12+
typedef long clock_t;
13+
14+
#endif // __LLVM_LIBC_TYPES_CLOCK_T_H__

libc/spec/stdc.td

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ def StdC : StandardSpec<"stdc"> {
33
NamedType StructTmType = NamedType<"struct tm">;
44
PtrType StructTmPtr = PtrType<StructTmType>;
55
PtrType TimeTTypePtr = PtrType<TimeTType>;
6+
NamedType ClockT = NamedType<"clock_t">;
67

78
NamedType DivTType = NamedType<"div_t">;
89
NamedType LDivTType = NamedType<"ldiv_t">;
@@ -917,8 +918,9 @@ def StdC : StandardSpec<"stdc"> {
917918
"time.h",
918919
[], // Macros
919920
[ // Types
920-
StructTmType,
921-
TimeTType,
921+
ClockT,
922+
StructTmType,
923+
TimeTType,
922924
],
923925
[], // Enumerations
924926
[
@@ -935,6 +937,11 @@ def StdC : StandardSpec<"stdc"> {
935937
ArgSpec<CharPtr>,
936938
]
937939
>,
940+
FunctionSpec<
941+
"clock",
942+
RetValSpec<ClockT>,
943+
[ArgSpec<VoidType>]
944+
>,
938945
FunctionSpec<
939946
"difftime",
940947
RetValSpec<DoubleType>,
@@ -961,6 +968,11 @@ def StdC : StandardSpec<"stdc"> {
961968
RetValSpec<TimeTType>,
962969
[ArgSpec<StructTmPtr>]
963970
>,
971+
FunctionSpec<
972+
"time",
973+
RetValSpec<TimeTType>,
974+
[ArgSpec<TimeTTypePtr>]
975+
>,
964976
]
965977
>;
966978

libc/src/time/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3+
endif()
4+
15
add_object_library(
26
time_utils
37
SRCS
@@ -103,3 +107,17 @@ add_entrypoint_object(
103107
libc.src.__support.OSUtil.osutil
104108
libc.src.errno.errno
105109
)
110+
111+
add_entrypoint_object(
112+
time
113+
ALIAS
114+
DEPENDS
115+
.${LIBC_TARGET_OS}.time
116+
)
117+
118+
add_entrypoint_object(
119+
clock
120+
ALIAS
121+
DEPENDS
122+
.${LIBC_TARGET_OS}.clock
123+
)

libc/src/time/clock.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header of clock --------------------------*- 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_TIME_CLOCK_H
10+
#define LLVM_LIBC_SRC_TIME_CLOCK_H
11+
12+
#include <time.h>
13+
14+
namespace __llvm_libc {
15+
16+
clock_t clock();
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_TIME_CLOCK_H

libc/src/time/linux/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
add_entrypoint_object(
2+
time
3+
SRCS
4+
time.cpp
5+
HDRS
6+
../time_func.h
7+
DEPENDS
8+
libc.include.time
9+
libc.include.sys_syscall
10+
libc.src.__support.OSUtil.osutil
11+
libc.src.errno.errno
12+
)
13+
14+
add_entrypoint_object(
15+
clock
16+
SRCS
17+
clock.cpp
18+
HDRS
19+
../clock.h
20+
DEPENDS
21+
libc.include.time
22+
libc.include.sys_syscall
23+
libc.src.__support.CPP.limits
24+
libc.src.__support.OSUtil.osutil
25+
libc.src.errno.errno
26+
)

0 commit comments

Comments
 (0)