Skip to content

Commit 2c0ef10

Browse files
committed
- Implemented a clock_settime syscall wrapper for Linux
- Implemented unit-tests for the feature
1 parent b241cc9 commit 2c0ef10

File tree

15 files changed

+227
-1
lines changed

15 files changed

+227
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ if(LLVM_LIBC_FULL_BUILD)
11421142
libc.src.time.ctime_r
11431143
libc.src.time.clock
11441144
libc.src.time.clock_gettime
1145+
libc.src.time.clock_settime
11451146
libc.src.time.difftime
11461147
libc.src.time.gettimeofday
11471148
libc.src.time.gmtime

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ if(LLVM_LIBC_FULL_BUILD)
12671267
libc.src.time.ctime_r
12681268
libc.src.time.clock
12691269
libc.src.time.clock_gettime
1270+
libc.src.time.clock_settime
12701271
libc.src.time.difftime
12711272
libc.src.time.gettimeofday
12721273
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
@@ -1305,6 +1305,7 @@ if(LLVM_LIBC_FULL_BUILD)
13051305
libc.src.time.ctime_r
13061306
libc.src.time.clock
13071307
libc.src.time.clock_gettime
1308+
libc.src.time.clock_settime
13081309
libc.src.time.difftime
13091310
libc.src.time.gettimeofday
13101311
libc.src.time.gmtime

libc/docs/headers/time.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Implementation Status
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ add_object_library(
1919
DEPENDS
2020
libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
2121
)
22+
23+
add_object_library(
24+
clock_settime
25+
ALIAS
26+
DEPENDS
27+
libc.src.__support.time.${LIBC_TARGET_OS}.clock_settime
28+
)
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+
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64,
38+
static_cast<long>(clockid),
39+
reinterpret_cast<long>(&ts64));
40+
if (ret == 0) {
41+
ts->tv_sec = static_cast<decltype(ts->tv_sec)>(ts64.tv_sec);
42+
ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(ts64.tv_nsec);
43+
}
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)