Skip to content

Commit 79601ce

Browse files
authored
[libc][POSIX] Add clock_settime() function for Linux (#161729)
Closes #161461 - This is my first time contributing to libc's POSIX, so for reference I used `clock_gettime` implementation for Linux. For convenience, here is the description of `clock_settime` function [behavior](https://www.man7.org/linux/man-pages/man3/clock_settime.3.html)
1 parent 497dc10 commit 79601ce

File tree

19 files changed

+257
-8
lines changed

19 files changed

+257
-8
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ if(LLVM_LIBC_FULL_BUILD)
11441144
libc.src.time.ctime_r
11451145
libc.src.time.clock
11461146
libc.src.time.clock_gettime
1147+
libc.src.time.clock_settime
11471148
libc.src.time.difftime
11481149
libc.src.time.gettimeofday
11491150
libc.src.time.gmtime

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,7 @@ if(LLVM_LIBC_FULL_BUILD)
12731273
libc.src.time.ctime_r
12741274
libc.src.time.clock
12751275
libc.src.time.clock_gettime
1276+
libc.src.time.clock_settime
12761277
libc.src.time.difftime
12771278
libc.src.time.gettimeofday
12781279
libc.src.time.gmtime

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,7 @@ if(LLVM_LIBC_FULL_BUILD)
13141314
libc.src.time.ctime_r
13151315
libc.src.time.clock
13161316
libc.src.time.clock_gettime
1317+
libc.src.time.clock_settime
13171318
libc.src.time.difftime
13181319
libc.src.time.gettimeofday
13191320
libc.src.time.gmtime

libc/docs/headers/time.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ Implementation Status
6767
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
6868
| clock_getres | | | | | | | | | | | | | |
6969
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
70-
| clock_gettime | |check| | |check| | | |check| | | | | | | | | | |
70+
| clock_gettime | |check| | |check| | | |check| | | | | | | | | |check| | |check| |
7171
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
7272
| clock_nanosleep | | | | | | | | | | | | | |
7373
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
74-
| clock_settime | | | | | | | | | | | | | |
74+
| clock_settime | |check| | |check| | | |check| | | | | | | | | | |
7575
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
7676
| ctime | |check| | |check| | | |check| | | | | | | | | | |
7777
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+

libc/include/time.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ functions:
6767
arguments:
6868
- type: clockid_t
6969
- type: struct timespec *
70+
- name: clock_settime
71+
standard:
72+
- POSIX
73+
return_type: int
74+
arguments:
75+
- type: clockid_t
76+
- type: const struct timespec *
7077
- name: difftime
7178
standard:
7279
- stdc

libc/src/__support/time/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ add_object_library(
1919
DEPENDS
2020
libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
2121
)
22+
23+
if(TARGET libc.src.__support.time.${LIBC_TARGET_OS}.clock_settime)
24+
add_object_library(
25+
clock_settime
26+
ALIAS
27+
DEPENDS
28+
libc.src.__support.time.${LIBC_TARGET_OS}.clock_settime
29+
)
30+
endif()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===--- clock_settime linux implementation ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_SETTIME_H
10+
#define LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_SETTIME_H
11+
12+
#include "hdr/types/clockid_t.h"
13+
#include "hdr/types/struct_timespec.h"
14+
#include "src/__support/error_or.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
namespace internal {
18+
ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts);
19+
} // namespace internal
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_SETTIME_H

libc/src/__support/time/linux/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ add_object_library(
1414
libc.src.__support.OSUtil.linux.vdso
1515
)
1616

17+
add_object_library(
18+
clock_settime
19+
HDRS
20+
../clock_settime.h
21+
SRCS
22+
clock_settime.cpp
23+
DEPENDS
24+
libc.include.sys_syscall
25+
libc.hdr.types.struct_timespec
26+
libc.hdr.types.clockid_t
27+
libc.src.__support.common
28+
libc.src.__support.error_or
29+
libc.src.__support.OSUtil.osutil
30+
)
31+
1732
add_header_library(
1833
clock_conversion
1934
HDRS
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===--- clock_settime linux implementation ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/time/clock_settime.h"
10+
#include "hdr/types/clockid_t.h"
11+
#include "hdr/types/struct_timespec.h"
12+
#include "src/__support/OSUtil/syscall.h"
13+
#include "src/__support/common.h"
14+
#include "src/__support/error_or.h"
15+
#include "src/__support/macros/config.h"
16+
#include <sys/syscall.h>
17+
18+
#if defined(SYS_clock_settime64)
19+
#include <linux/time_types.h>
20+
#endif
21+
22+
namespace LIBC_NAMESPACE_DECL {
23+
namespace internal {
24+
ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) {
25+
int ret;
26+
#if defined(SYS_clock_settime)
27+
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime,
28+
static_cast<long>(clockid),
29+
reinterpret_cast<long>(ts));
30+
#elif defined(SYS_clock_settime64)
31+
static_assert(
32+
sizeof(time_t) == sizeof(int64_t),
33+
"SYS_clock_settime64 requires struct timespec with 64-bit members.");
34+
35+
__kernel_timespec ts64{};
36+
37+
// Populate the 64-bit kernel structure from the user-provided timespec
38+
ts64.tv_sec = static_cast<decltype(ts64.tv_sec)>(ts->tv_sec);
39+
ts64.tv_nsec = static_cast<decltype(ts64.tv_nsec)>(ts->tv_nsec);
40+
41+
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64,
42+
static_cast<long>(clockid),
43+
reinterpret_cast<long>(&ts64));
44+
#else
45+
#error "SYS_clock_settime and SYS_clock_settime64 syscalls not available."
46+
#endif
47+
if (ret < 0)
48+
return Error(-ret);
49+
return ret;
50+
}
51+
52+
} // namespace internal
53+
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,11 @@ add_entrypoint_object(
245245
DEPENDS
246246
.${LIBC_TARGET_OS}.clock_getres
247247
)
248+
249+
add_entrypoint_object(
250+
clock_settime
251+
ALIAS
252+
DEPENDS
253+
.${LIBC_TARGET_OS}.clock_settime
254+
)
255+

0 commit comments

Comments
 (0)