Skip to content

Commit 0adff0a

Browse files
[libc][windows] start time API implementation (llvm#117775)
Add a `clock_gettime` emulation layer and use it to implement the `time` entrypoint. For windows, the monotonic clock is emulated using `QPC`. The realtime clock is emulated using `GetSystemTimePreciseAsFileTime`.
1 parent 51a5b77 commit 0adff0a

File tree

26 files changed

+248
-47
lines changed

26 files changed

+248
-47
lines changed

libc/config/windows/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ set(TARGET_LIBC_ENTRYPOINTS
9595

9696
# errno.h entrypoints
9797
libc.src.errno.errno
98+
99+
# time.h entrypoints
100+
libc.src.time.time
98101
)
99102

100103
set(TARGET_LIBM_ENTRYPOINTS

libc/hdr/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,18 @@ add_proxy_header_library(
135135
libc.include.llvm-libc-macros.unistd_macros
136136
)
137137

138+
if (WIN32)
139+
set(windows_addtional_time_macros libc.include.llvm-libc-macros.windows.time_macros_ext)
140+
else()
141+
set(windows_addtional_time_macros "")
142+
endif()
143+
138144
add_proxy_header_library(
139145
time_macros
140146
HDRS
141147
time_macros.h
148+
DEPENDS
149+
${windows_addtional_time_macros}
142150
FULL_BUILD_DEPENDS
143151
libc.include.time
144152
libc.include.llvm-libc-macros.time_macros

libc/hdr/time_macros.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@
1919

2020
#endif // LLVM_LIBC_FULL_BUILD
2121

22+
// TODO: For now, on windows, let's always include the extension header.
23+
// We will need to decide how to export this header.
24+
#ifdef _WIN32
25+
#include "include/llvm-libc-macros/windows/time-macros-ext.h"
26+
#endif // _WIN32
27+
2228
#endif // LLVM_LIBC_HDR_TIME_MACROS_H

libc/hdr/types/clockid_t.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#ifndef LLVM_LIBC_HDR_TYPES_CLOCKID_T_H
1010
#define LLVM_LIBC_HDR_TYPES_CLOCKID_T_H
1111

12-
#ifdef LIBC_FULL_BUILD
12+
// TODO: we will need to decide how to export extension to windows.
13+
#if defined(LIBC_FULL_BUILD) || defined(_WIN32)
1314

1415
#include "include/llvm-libc-types/clockid_t.h"
1516

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_header(
2+
time_macros_ext
3+
HDR
4+
time-macros-ext.h
5+
)
6+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Windows Time Macros Extension -------------------------------------===//
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_MACROS_WINDOWS_TIME_MACROS_EXT_H
10+
#define LLVM_LIBC_MACROS_WINDOWS_TIME_MACROS_EXT_H
11+
12+
#define CLOCK_MONOTONIC 0
13+
#define CLOCK_REALTIME 1
14+
#define CLOCK_PROCESS_CPUTIME_ID 2
15+
#define CLOCK_THREAD_CPUTIME_ID 3
16+
17+
#endif // LLVM_LIBC_MACROS_WINDOWS_TIME_MACROS_EXT_H

libc/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_subdirectory(stdio)
1313
add_subdirectory(stdlib)
1414
add_subdirectory(string)
1515
add_subdirectory(wchar)
16+
add_subdirectory(time)
1617

1718
if(${LIBC_TARGET_OS} STREQUAL "linux")
1819
add_subdirectory(dirent)
@@ -40,5 +41,4 @@ add_subdirectory(setjmp)
4041
add_subdirectory(signal)
4142
add_subdirectory(spawn)
4243
add_subdirectory(threads)
43-
add_subdirectory(time)
4444
add_subdirectory(locale)

libc/src/__support/time/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
22
add_subdirectory(${LIBC_TARGET_OS})
33
endif()
44

5+
add_object_library(
6+
clock_gettime
7+
ALIAS
8+
DEPENDS
9+
libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
10+
)
11+
512
add_header_library(
613
units
714
HDRS

libc/src/__support/time/linux/clock_gettime.h renamed to libc/src/__support/time/clock_gettime.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
10-
#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
10+
#define LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
1111

1212
#include "hdr/types/clockid_t.h"
1313
#include "hdr/types/struct_timespec.h"
1414
#include "src/__support/error_or.h"
1515

16-
#if defined(SYS_clock_gettime64)
17-
#include <linux/time_types.h>
18-
#endif
19-
2016
namespace LIBC_NAMESPACE_DECL {
2117
namespace internal {
2218
ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts);
2319
} // namespace internal
2420
} // namespace LIBC_NAMESPACE_DECL
2521

26-
#endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
22+
#endif // LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H

libc/src/__support/time/linux/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
add_object_library(
22
clock_gettime
33
HDRS
4-
clock_gettime.h
4+
../clock_gettime.h
55
SRCS
66
clock_gettime.cpp
77
DEPENDS

0 commit comments

Comments
 (0)