-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] Add clock_gettime for Darwin #167160
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
Open
bojle
wants to merge
15
commits into
llvm:main
Choose a base branch
from
bojle:users/bojle/clock_gettime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ab85f49
[libc] allow UnitTest suite to be compiled on darwin
bojle 9a5455d
[libc] fix EXPECT_EXIT suspend/timeout for darwin
bojle 34f4b8b
[libc] fix formatting
bojle 3d60307
[libc][darwin] add syscall numbers from macos sdk
bojle 0bba851
[libc] add reference for syscall numbers
bojle b0ab286
[libc] use syscall.h
bojle 4a1140a
[libc] use <sys/syscall.h> as its cleaner and license compliant
bojle 5f99c9f
[libc] include sdk path
bojle e718db1
[libc] make include guards explicit
bojle 53b21d2
[libc][test] split exit tests into two separate tests
bojle 7fe0c00
[libc] remove _Exit mentions from exit_test.cpp
bojle f3cf53a
[libc][darwin] add internal::exit
bojle f97aaca
[libc] use cpp11 attribute syntax
bojle d281b97
[libc] Add clock_gettime for Darwin
bojle 21e2f2b
[libc] fix minor nits
bojle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| add_header( | ||
| time_macros | ||
| HDR | ||
| time-macros.h | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //===-- 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_MACROS_LINUX_TIME_MACROS_H | ||
| #define LLVM_LIBC_MACROS_LINUX_TIME_MACROS_H | ||
|
|
||
| #include <_time.h> | ||
|
|
||
| #endif // LLVM_LIBC_MACROS_LINUX_TIME_MACROS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| add_header_library( | ||
| darwin_util | ||
| darwin_aarch64_util | ||
| HDRS | ||
| syscall.h | ||
| DEPENDS | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===------------ MacOS implementation of an exit function ------*- 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 "src/__support/OSUtil/darwin/syscall.h" // syscall_impl | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "sys/syscall.h" // For syscall numbers. | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
|
|
||
| [[noreturn]] void exit(int status) { | ||
| for (;;) { | ||
| LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, status); | ||
| } | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| add_object_library( | ||
| clock_gettime | ||
| SRCS | ||
| clock_gettime.cpp | ||
| HDRS | ||
| ../clock_gettime.h | ||
| DEPENDS | ||
| libc.src.__support.common | ||
| libc.src.__support.error_or | ||
| libc.hdr.types.struct_timeval | ||
| libc.hdr.types.struct_timespec | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| //===-- Darwin implementation of internal clock_gettime -------------------===// | ||
| // | ||
| // 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/time/clock_gettime.h" | ||
| #include "hdr/errno_macros.h" // For EINVAL | ||
| #include "hdr/time_macros.h" | ||
| #include "hdr/types/struct_timespec.h" | ||
| #include "hdr/types/struct_timeval.h" | ||
| #include "src/__support/OSUtil/syscall.h" // For syscall_impl | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/error_or.h" | ||
| #include <sys/syscall.h> // For SYS_gettimeofday | ||
| #include <sys/time.h> // For struct timezone | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
|
|
||
| ErrorOr<int> clock_gettime(clockid_t clockid, struct timespec *ts) { | ||
bojle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (clockid != CLOCK_REALTIME) { | ||
| return Error(EINVAL); | ||
| } | ||
| struct timeval tv; | ||
| // The second argument to gettimeofday is a timezone pointer | ||
| // The third argument is mach_absolute_time | ||
| // Both of these, we don't need here, so they are 0 | ||
| long ret = LIBC_NAMESPACE::syscall_impl<long>( | ||
| SYS_gettimeofday, reinterpret_cast<long>(&tv), 0, 0); | ||
| if (ret != 0) { | ||
| // The syscall returns -1 on error and sets errno. | ||
| return Error(EINVAL); | ||
| } | ||
|
|
||
| ts->tv_sec = tv.tv_sec; | ||
| ts->tv_nsec = tv.tv_usec * 1000; | ||
| return 0; | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| add_entrypoint_object( | ||
| clock_gettime | ||
| SRCS | ||
| clock_gettime.cpp | ||
| HDRS | ||
| # The public header is part of the parent directory's library. | ||
| DEPENDS | ||
| libc.src.__support.time.clock_gettime | ||
| libc.src.errno.errno | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //===---------- Darwin implementation of the POSIX clock_gettime function | ||
| //--===// | ||
bojle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // | ||
| // 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/clock_gettime.h" | ||
|
|
||
| #include "src/__support/common.h" | ||
| #include "src/__support/libc_errno.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/time/clock_gettime.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, clock_gettime, | ||
| (clockid_t clockid, struct timespec *ts)) { | ||
| auto result = internal::clock_gettime(clockid, ts); | ||
| if (!result.has_value()) { | ||
| libc_errno = result.error(); | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| add_libc_test( | ||
| clock_gettime | ||
| SUITE libc-support-time-tests | ||
| SRCS clock_gettime.cpp | ||
| DEPENDS | ||
| libc.src.__support.CPP.expected | ||
| libc.src.__support.time.darwin.clock_gettime | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //===-- unit tests for darwin's time utilities --------------------------===// | ||
| // | ||
| // 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/time/clock_gettime.h" | ||
| #include "src/__support/CPP/expected.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| template <class T, class E> | ||
| using expected = LIBC_NAMESPACE::cpp::expected<T, E>; | ||
|
|
||
| TEST(LlvmLibcSupportDarwinClockGetTime, BasicGetTime) { | ||
| struct timespec ts; | ||
| auto result = LIBC_NAMESPACE::internal::clock_gettime(CLOCK_REALTIME, &ts); | ||
| ASSERT_TRUE(result.has_value()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.