-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] Implement timespec_get
#116102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] Implement timespec_get
#116102
Changes from 3 commits
f0f442d
9fc2d09
5560670
d7c77be
4c65922
0d52071
f4edc9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,6 @@ | |
| #include "linux/time-macros.h" | ||
| #endif | ||
|
|
||
| #define TIME_UTC 1 | ||
|
|
||
| #endif // LLVM_LIBC_MACROS_TIME_MACROS_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| //===---------- Baremetal implementation of time utils ----------*- 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "time.h" | ||
|
|
||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
|
|
||
| extern "C" int __llvm_libc_timespec_get_utc(struct timespec *ts); | ||
|
|
||
| bool timespec_get_utc(struct timespec *ts) { | ||
| return __llvm_libc_timespec_get_utc(ts); | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| //===---------- Baremetal implementation of time utils ----------*- 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_OSUTIL_BAREMETAL_TIME_H | ||
| #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_TIME_H | ||
|
|
||
| #include "include/llvm-libc-types/struct_timespec.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
|
|
||
| bool timespec_get_utc(struct timespec *ts); | ||
|
|
||
| } // namespace internal | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| add_entrypoint_object( | ||
| timespec_get | ||
| SRCS | ||
| timespec_get.cpp | ||
| HDRS | ||
| ../timespec_get.h | ||
| DEPENDS | ||
| libc.hdr.time_macros | ||
| libc.hdr.types.struct_timespec | ||
| libc.src.__support.OSUtil.osutil | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //===-- Implementation of timespec_get for baremetal ----------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/time/timespec_get.h" | ||
| #include "hdr/time_macros.h" | ||
| #include "src/__support/OSUtil/baremetal/time.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) { | ||
| if (base != TIME_UTC) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (!internal::timespec_get_utc(ts)) { | ||
| return 0; | ||
| } | ||
| return base; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===-- Implementation of timespec_get for Linux --------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/time/timespec_get.h" | ||
| #include "hdr/time_macros.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/time/linux/clock_gettime.h" | ||
| #include "src/errno/libc_errno.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) { | ||
| if (base != TIME_UTC) { | ||
|
||
| return 0; | ||
| } | ||
petrhosek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| auto result = internal::clock_gettime(CLOCK_REALTIME, ts); | ||
| if (!result.has_value()) { | ||
| libc_errno = result.error(); | ||
| return 0; | ||
| } | ||
| return base; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| //===-- Implementation header of timespec_get -------------------*- 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_TIMESPEC_GET_H | ||
| #define LLVM_LIBC_SRC_TIME_TIMESPEC_GET_H | ||
|
|
||
| #include "src/__support/macros/config.h" | ||
| #include <time.h> | ||
petrhosek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| int timespec_get(struct timespec *ts, int base); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_TIME_TIMESPEC_GET_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //===-- Unittests for timespec_get ----------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/__support/macros/properties/architectures.h" | ||
| #include "src/time/timespec_get.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| #include <time.h> | ||
petrhosek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| TEST(LlvmLibcTimespecGet, Utc) { | ||
| #ifndef LIBC_TARGET_ARCH_IS_GPU | ||
| timespec ts; | ||
| int result; | ||
| result = LIBC_NAMESPACE::timespec_get(&ts, TIME_UTC); | ||
| ASSERT_EQ(result, TIME_UTC); | ||
| ASSERT_GT(ts.tv_sec, time_t(0)); | ||
| #endif | ||
| } | ||
|
|
||
| TEST(LlvmLibcTimespecGet, Unknown) { | ||
| #ifndef LIBC_TARGET_ARCH_IS_GPU | ||
| timespec ts; | ||
| int result; | ||
| result = LIBC_NAMESPACE::timespec_get(&ts, 0); | ||
| ASSERT_EQ(result, 0); | ||
| #endif | ||
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.