Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use platform shims for clock_gettime to support wasi-libc #781

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/FoundationEssentials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_library(FoundationEssentials
SortComparator.swift
UUID_Wrappers.swift
UUID.swift
WASILibc+Extensions.swift
WinSDK+Extensions.swift)

add_subdirectory(AttributedString)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions Sources/FoundationEssentials/WASILibc+Extensions.swift
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions Sources/_FoundationCShims/include/platform_shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <time.h>
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 */