Skip to content

[libc] Enable setitimer and getitimer functions on riscv #139182

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 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.uio.readv

# sys/time.h entrypoints
# libc.src.sys.time.setitimer
# libc.src.sys.time.getitimer
libc.src.sys.time.setitimer
libc.src.sys.time.getitimer
)

if(LLVM_LIBC_INCLUDE_SCUDO)
Expand Down
18 changes: 16 additions & 2 deletions libc/src/sys/time/linux/getitimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@
namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, getitimer, (int which, struct itimerval *curr_value)) {
long ret =
LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value);
long ret = 0;
if constexpr (sizeof(time_t) > sizeof(long)) {
// There is no SYS_getitimer_time64 call, so we can't use time_t directly.
long curr_value32[4];
Copy link
Member Author

Choose a reason for hiding this comment

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

I created an array of long because it was less verbose than creating a local struct just for this, but please let me know if you have other suggestions.

ret =
LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value32);
if (!ret) {
curr_value->it_interval.tv_sec = curr_value32[0];
curr_value->it_interval.tv_usec = curr_value32[1];
curr_value->it_value.tv_sec = curr_value32[2];
curr_value->it_value.tv_usec = curr_value32[3];
}
} else {
ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value);
}

// On failure, return -1 and set errno.
if (ret < 0) {
libc_errno = static_cast<int>(-ret);
Expand Down
26 changes: 24 additions & 2 deletions libc/src/sys/time/linux/setitimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,30 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, setitimer,
(int which, const struct itimerval *new_value,
struct itimerval *old_value)) {
long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value,
old_value);
long ret = 0;
if constexpr (sizeof(time_t) > sizeof(long)) {
// There is no SYS_setitimer_time64 call, so we can't use time_t directly,
// and need to convert it to long first.
long new_value32[4] = {static_cast<long>(new_value->it_interval.tv_sec),
new_value->it_interval.tv_usec,
static_cast<long>(new_value->it_value.tv_sec),
new_value->it_value.tv_usec};
long old_value32[4];

ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value32,
old_value32);

if (!ret && old_value) {
old_value->it_interval.tv_sec = old_value32[0];
old_value->it_interval.tv_usec = old_value32[1];
old_value->it_value.tv_sec = old_value32[2];
old_value->it_value.tv_usec = old_value32[3];
}
} else {
ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value,
old_value);
}

// On failure, return -1 and set errno.
if (ret < 0) {
libc_errno = static_cast<int>(-ret);
Expand Down
Loading