Skip to content

Commit 4e06bcd

Browse files
Use platform shims for clock ids to support wasi-libc
This change adds platform shims for clock ids so that we can use them in Swift code because the clock id macro definitions in wasi-libc can't be imported through ClangImporter. Also wasi-libc's `timespec.tv_sec` and `timespec.tv_nsec` are not imported as `Int` but as `Int64` and `Int32` respectively, so we need to cast them to `UInt64` before doing arithmetic operations on them.
1 parent 5463e8e commit 4e06bcd

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

Sources/FoundationEssentials/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_library(FoundationEssentials
3030
SortComparator.swift
3131
UUID_Wrappers.swift
3232
UUID.swift
33+
WASILibc+Extensions.swift
3334
WinSDK+Extensions.swift)
3435

3536
add_subdirectory(AttributedString)

Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ final class _ProcessInfo: Sendable {
129129
#else
130130
var ts: timespec = timespec()
131131
clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
132-
let time: UInt64 = UInt64(ts.tv_sec * 1000000000 + ts.tv_nsec)
132+
let time: UInt64 = UInt64(ts.tv_sec) * 1000000000 + UInt64(ts.tv_nsec)
133133
#endif
134134
let timeString = String(time, radix: 16, uppercase: true)
135135
let padding = String(repeating: "0", count: 16 - timeString.count)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#if os(WASI)
13+
14+
import WASILibc
15+
internal import _FoundationCShims
16+
17+
// MARK: - Clock
18+
19+
internal var CLOCK_REALTIME: clockid_t {
20+
return _platform_shims_clock_realtime()
21+
}
22+
23+
internal var CLOCK_MONOTONIC: clockid_t {
24+
return _platform_shims_clock_monotonic()
25+
}
26+
27+
internal var CLOCK_MONOTONIC_RAW: clockid_t {
28+
// WASI does not have a raw monotonic clock, so we use the monotonic clock instead.
29+
return CLOCK_MONOTONIC
30+
}
31+
32+
#endif // os(WASI)

Sources/_FoundationCShims/include/platform_shims.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@ typedef enum {
6868
INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName();
6969
#endif
7070

71+
#if TARGET_OS_WASI
72+
// Define clock id getter shims so that we can use them in Swift
73+
// even if clock id macros can't be imported through ClangImporter.
74+
75+
#include <time.h>
76+
static inline _Nonnull clockid_t _platform_shims_clock_monotonic(void) {
77+
return CLOCK_MONOTONIC;
78+
}
79+
static inline _Nonnull clockid_t _platform_shims_clock_realtime(void) {
80+
return CLOCK_REALTIME;
81+
}
82+
#endif
83+
7184
#endif /* CSHIMS_PLATFORM_SHIMS */

0 commit comments

Comments
 (0)