Skip to content

Commit 67e357e

Browse files
[libc][gpu] unify time implementation style and fix build
1 parent 4dee4c9 commit 67e357e

File tree

9 files changed

+88
-46
lines changed

9 files changed

+88
-46
lines changed

libc/src/__support/time/CMakeLists.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
add_header_library(
2+
units
3+
HDRS
4+
units.h
5+
DEPENDS
6+
libc.src.__support.common
7+
libc.hdr.types.time_t
8+
)
9+
110
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
211
add_subdirectory(${LIBC_TARGET_OS})
12+
else()
13+
return()
314
endif()
415

516
add_object_library(
@@ -8,12 +19,3 @@ add_object_library(
819
DEPENDS
920
libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
1021
)
11-
12-
add_header_library(
13-
units
14-
HDRS
15-
units.h
16-
DEPENDS
17-
libc.src.__support.common
18-
libc.hdr.types.time_t
19-
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
add_object_library(
2+
time_utils
3+
SRCS
4+
time_utils.cpp
5+
HDRS
6+
time_utils.h
7+
DEPENDS
8+
libc.hdr.types.clock_t
9+
libc.hdr.time_macros
10+
)
11+
12+
add_entrypoint_object(
13+
clock_gettime
14+
SRCS
15+
clock_gettime.cpp
16+
HDRS
17+
../clock_gettime.h
18+
DEPENDS
19+
libc.hdr.types.clockid_t
20+
libc.hdr.types.struct_timespec
21+
.time_utils
22+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===---------- GPU implementation of the clock_gettime function ----------===//
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/time/clock_gettime.h"
10+
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/time/clock_gettime.h"
14+
#include "src/__support/time/gpu/time_utils.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
namespace internal {
18+
constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
19+
20+
ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
21+
if (clockid != CLOCK_MONOTONIC || !ts)
22+
return cpp::unexpected(-1);
23+
24+
uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
25+
uint64_t ticks = gpu::fixed_frequency_clock();
26+
27+
ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
28+
ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
29+
30+
return 0;
31+
}
32+
} // namespace internal
33+
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/gpu/CMakeLists.txt

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
add_object_library(
2-
time_utils
3-
SRCS
4-
time_utils.cpp
5-
HDRS
6-
time_utils.h
7-
DEPENDS
8-
libc.hdr.types.clock_t
9-
libc.hdr.time_macros
10-
)
11-
121
add_entrypoint_object(
132
clock
143
SRCS
@@ -18,7 +7,8 @@ add_entrypoint_object(
187
DEPENDS
198
libc.include.time
209
libc.src.__support.GPU.utils
21-
.time_utils
10+
libc.src.__support.time.clock_gettime
11+
libc.src.__support.time.gpu.time_utils
2212
)
2313

2414
add_entrypoint_object(
@@ -30,29 +20,31 @@ add_entrypoint_object(
3020
DEPENDS
3121
libc.include.time
3222
libc.src.__support.GPU.utils
33-
.time_utils
23+
libc.src.__support.time.gpu.time_utils
3424
)
3525

3626
add_entrypoint_object(
37-
clock_gettime
27+
timespec_get
3828
SRCS
39-
clock_gettime.cpp
29+
timespec_get.cpp
4030
HDRS
41-
../clock_gettime.h
31+
../timespec_get.h
4232
DEPENDS
43-
libc.hdr.types.clockid_t
33+
libc.hdr.time_macros
4434
libc.hdr.types.struct_timespec
45-
.time_utils
35+
libc.src.__support.time.gpu.time_utils
4636
)
4737

4838
add_entrypoint_object(
49-
timespec_get
39+
clock_gettime
5040
SRCS
51-
timespec_get.cpp
41+
clock_gettime.cpp
5242
HDRS
53-
../timespec_get.h
43+
../clock_gettime.h
5444
DEPENDS
5545
libc.hdr.time_macros
46+
libc.hdr.types.clockid_t
5647
libc.hdr.types.struct_timespec
57-
.time_utils
48+
libc.src.__support.time.gpu.time_utils
49+
libc.src.__support.time.clock_gettime
5850
)

libc/src/time/gpu/clock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/time/clock.h"
1010
#include "src/__support/macros/config.h"
11-
#include "src/time/gpu/time_utils.h"
11+
#include "src/__support/time/gpu/time_utils.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/time/gpu/clock_gettime.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,16 @@
1010

1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13-
#include "time_utils.h"
13+
#include "src/__support/time/clock_gettime.h"
14+
#include "src/__support/time/gpu/time_utils.h"
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

17-
constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
18-
1918
LLVM_LIBC_FUNCTION(int, clock_gettime, (clockid_t clockid, timespec *ts)) {
20-
if (clockid != CLOCK_MONOTONIC || !ts)
21-
return -1;
22-
23-
uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
24-
uint64_t ticks = gpu::fixed_frequency_clock();
25-
26-
ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
27-
ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
28-
29-
return 0;
19+
ErrorOr<int> result = internal::clock_gettime(clockid, ts);
20+
if (result)
21+
return result.value();
22+
return result.error();
3023
}
3124

3225
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/gpu/nanosleep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "src/time/nanosleep.h"
1010

1111
#include "src/__support/macros/config.h"
12-
#include "time_utils.h"
12+
#include "src/__support/time/gpu/time_utils.h"
1313

1414
namespace LIBC_NAMESPACE_DECL {
1515

0 commit comments

Comments
 (0)