From 4e06bcd6ff381c271fab535449dcd0d1291c83e7 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 26 Jul 2024 07:03:21 +0000 Subject: [PATCH] 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. --- Sources/FoundationEssentials/CMakeLists.txt | 1 + .../ProcessInfo/ProcessInfo.swift | 2 +- .../WASILibc+Extensions.swift | 32 +++++++++++++++++++ .../include/platform_shims.h | 13 ++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 Sources/FoundationEssentials/WASILibc+Extensions.swift diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt index d9ebc2ebb..836d849ae 100644 --- a/Sources/FoundationEssentials/CMakeLists.txt +++ b/Sources/FoundationEssentials/CMakeLists.txt @@ -30,6 +30,7 @@ add_library(FoundationEssentials SortComparator.swift UUID_Wrappers.swift UUID.swift + WASILibc+Extensions.swift WinSDK+Extensions.swift) add_subdirectory(AttributedString) diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift index 302d3c62d..78b86c266 100644 --- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift @@ -129,7 +129,7 @@ final class _ProcessInfo: Sendable { #else var ts: timespec = timespec() clock_gettime(CLOCK_MONOTONIC_RAW, &ts) - let time: UInt64 = UInt64(ts.tv_sec * 1000000000 + ts.tv_nsec) + let time: UInt64 = UInt64(ts.tv_sec) * 1000000000 + UInt64(ts.tv_nsec) #endif let timeString = String(time, radix: 16, uppercase: true) let padding = String(repeating: "0", count: 16 - timeString.count) diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift new file mode 100644 index 000000000..1c05f9958 --- /dev/null +++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if os(WASI) + +import WASILibc +internal import _FoundationCShims + +// MARK: - Clock + +internal var CLOCK_REALTIME: clockid_t { + return _platform_shims_clock_realtime() +} + +internal var CLOCK_MONOTONIC: clockid_t { + return _platform_shims_clock_monotonic() +} + +internal var CLOCK_MONOTONIC_RAW: clockid_t { + // WASI does not have a raw monotonic clock, so we use the monotonic clock instead. + return CLOCK_MONOTONIC +} + +#endif // os(WASI) diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h index 911fc9e7a..9c7e95925 100644 --- a/Sources/_FoundationCShims/include/platform_shims.h +++ b/Sources/_FoundationCShims/include/platform_shims.h @@ -68,4 +68,17 @@ typedef enum { INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(); #endif +#if TARGET_OS_WASI +// Define clock id getter shims so that we can use them in Swift +// even if clock id macros can't be imported through ClangImporter. + +#include +static inline _Nonnull clockid_t _platform_shims_clock_monotonic(void) { + return CLOCK_MONOTONIC; +} +static inline _Nonnull clockid_t _platform_shims_clock_realtime(void) { + return CLOCK_REALTIME; +} +#endif + #endif /* CSHIMS_PLATFORM_SHIMS */