Skip to content

Commit 4440638

Browse files
Use platform shims for clock ids to support wasi-libc (#781)
1 parent 8bf4cb0 commit 4440638

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
@@ -131,7 +131,7 @@ final class _ProcessInfo: Sendable {
131131
#else
132132
var ts: timespec = timespec()
133133
clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
134-
let time: UInt64 = UInt64(ts.tv_sec * 1000000000 + ts.tv_nsec)
134+
let time: UInt64 = UInt64(ts.tv_sec) * 1000000000 + UInt64(ts.tv_nsec)
135135
#endif
136136
let timeString = String(time, radix: 16, uppercase: true)
137137
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(void);
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)