-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc][POSIX] Add clock_settime() function for Linux #161729
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2c0ef10
- Implemented a clock_settime syscall wrapper for Linux
amemov 06ac55c
- Fixed CMake to include the implementation only on Linux for now
amemov 7105bef
- Changed CMake for clock_settime() to be more flexible for future ta…
amemov 366ad9a
- Removed redundant 'struct' keyword
amemov bd265d2
clang-format
amemov 36a3f80
- Added positive path for clock_settime test
amemov 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
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===--- clock_settime 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___SUPPORT_TIME_CLOCK_SETTIME_H | ||
| #define LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_SETTIME_H | ||
|
|
||
| #include "hdr/types/clockid_t.h" | ||
| #include "hdr/types/struct_timespec.h" | ||
| #include "src/__support/error_or.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
| ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts); | ||
| } // namespace internal | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_SETTIME_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //===--- clock_settime 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/__support/time/clock_settime.h" | ||
| #include "hdr/types/clockid_t.h" | ||
| #include "hdr/types/struct_timespec.h" | ||
| #include "src/__support/OSUtil/syscall.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/error_or.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include <sys/syscall.h> | ||
|
|
||
| #if defined(SYS_clock_settime64) | ||
| #include <linux/time_types.h> | ||
| #endif | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
| ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) { | ||
| int ret; | ||
| #if defined(SYS_clock_settime) | ||
| ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime, | ||
| static_cast<long>(clockid), | ||
| reinterpret_cast<long>(ts)); | ||
| #elif defined(SYS_clock_settime64) | ||
| static_assert( | ||
| sizeof(time_t) == sizeof(int64_t), | ||
| "SYS_clock_settime64 requires struct timespec with 64-bit members."); | ||
|
|
||
| __kernel_timespec ts64{}; | ||
|
|
||
| ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64, | ||
| static_cast<long>(clockid), | ||
| reinterpret_cast<long>(&ts64)); | ||
| if (ret == 0) { | ||
| ts->tv_sec = static_cast<decltype(ts->tv_sec)>(ts64.tv_sec); | ||
| ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(ts64.tv_nsec); | ||
| } | ||
| #else | ||
| #error "SYS_clock_settime and SYS_clock_settime64 syscalls not available." | ||
| #endif | ||
| if (ret < 0) | ||
| return Error(-ret); | ||
| return ret; | ||
| } | ||
|
|
||
| } // 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
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,22 @@ | ||
| //===-- Implementation header for clock_settime 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_TIME_CLOCK_SETTIME_H | ||
| #define LLVM_LIBC_SRC_TIME_CLOCK_SETTIME_H | ||
|
|
||
| #include "hdr/types/clockid_t.h" | ||
| #include "hdr/types/struct_timespec.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| int clock_settime(clockid_t clockid, const timespec *tp); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_TIME_CLOCK_SETTIME_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //===---------- Linux implementation of the POSIX clock_settime function --===// | ||
| // | ||
| // 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_settime.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/libc_errno.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/time/clock_settime.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, clock_settime, | ||
| (clockid_t clockid, const timespec *ts)) { | ||
| auto result = internal::clock_settime(clockid, ts); | ||
|
|
||
| // A negative return value indicates an error with the magnitude of the | ||
| // value being the error code. | ||
| 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
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,31 @@ | ||
| //===-- Unittests for clock_settime ---------------------------------------===// | ||
| // | ||
| // 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 "hdr/time_macros.h" | ||
| #include "hdr/types/struct_timespec.h" | ||
| #include "src/time/clock_settime.h" | ||
| #include "test/UnitTest/ErrnoCheckingTest.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| using LlvmLibcClockSetTime = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | ||
|
|
||
amemov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #ifdef CLOCK_MONOTONIC | ||
| TEST_F(LlvmLibcClockSetTime, MonotonicIsNotSettable) { | ||
| timespec ts = {0, 0}; | ||
| int result = LIBC_NAMESPACE::clock_settime(CLOCK_MONOTONIC, &ts); | ||
| ASSERT_EQ(result, -1); | ||
| ASSERT_ERRNO_EQ(EINVAL); | ||
| } | ||
| #endif // CLOCK_MONOTONIC | ||
|
|
||
| TEST_F(LlvmLibcClockSetTime, InvalidClockId) { | ||
| timespec ts = {0, 0}; | ||
| int result = LIBC_NAMESPACE::clock_settime(static_cast<clockid_t>(-1), &ts); | ||
| ASSERT_EQ(result, -1); | ||
| ASSERT_ERRNO_EQ(EINVAL); | ||
| } | ||
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.