Skip to content

Commit

Permalink
[libc][NFC] adjust time related implementations (#91485)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed May 9, 2024
1 parent 75ebcbf commit 8ac928f
Show file tree
Hide file tree
Showing 24 changed files with 335 additions and 71 deletions.
9 changes: 9 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 22 additions & 0 deletions libc/hdr/time_macros.h
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,48 @@ 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
libc.include.time
)

add_proxy_header_library(
clockid_t
HDRS
clockid_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.clockid_t
libc.include.sys_types
)

add_proxy_header_library(
clock_t
HDRS
clock_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.clock_t
libc.include.time
)

add_proxy_header_library(
suseconds_t
HDRS
suseconds_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.suseconds_t
libc.include.sys_time
)

add_proxy_header_library(
struct_timeval
HDRS
struct_timeval.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_timeval
libc.include.sys_time
)
22 changes: 22 additions & 0 deletions libc/hdr/types/clock_t.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/hdr/types/clockid_t.h
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions libc/hdr/types/struct_timeval.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/hdr/types/suseconds_t.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/hdr/types/time_t.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,5 @@ add_subdirectory(File)
add_subdirectory(HashTable)

add_subdirectory(fixed_point)

add_subdirectory(time)
19 changes: 19 additions & 0 deletions libc/src/__support/time/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
23 changes: 23 additions & 0 deletions libc/src/__support/time/clock_gettime.h
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions libc/src/__support/time/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
//===- Linux implementation of the POSIX clock_gettime function -*- C++ -*-===//
//===--- 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_TIME_LINUX_CLOCKGETTIMEIMPL_H
#define LLVM_LIBC_SRC_TIME_LINUX_CLOCKGETTIMEIMPL_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 <stdint.h> // For int64_t.
#include <sys/syscall.h> // For syscall numbers.
#include <time.h>

#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 {

LIBC_INLINE ErrorOr<int> clock_gettimeimpl(clockid_t clockid,
struct timespec *ts) {
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),
Expand All @@ -45,4 +36,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___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
38 changes: 38 additions & 0 deletions libc/src/__support/time/units.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion libc/src/time/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
5 changes: 3 additions & 2 deletions libc/src/time/clock_gettime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion libc/src/time/gettimeofday.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Loading

1 comment on commit 8ac928f

@zeroomega
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, this patch is not an NFC. It broke the runtimes build for baremetal target armv6m-unknown-eabi
See failure at: https://lab.llvm.org/buildbot/#/builders/98/builds/37674

Could you revert it and reland it after fixing it please?

C.C. @petrhosek

Please sign in to comment.