-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from swiftwasm/katei/clock-protocols
Add `WallClock` and `MonotonicClock` protocols
- Loading branch information
Showing
3 changed files
with
119 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import SystemExtras | ||
|
||
/// WASI wall clock interface based on WASI Preview 2 `wall-clock` interface. | ||
/// | ||
/// See also https://github.com/WebAssembly/wasi-clocks/blob/v0.2.0/wit/wall-clock.wit | ||
public protocol WallClock { | ||
/// An instant in time, in seconds and nanoseconds. | ||
typealias Duration = ( | ||
seconds: UInt64, | ||
nanoseconds: UInt32 | ||
) | ||
|
||
/// Read the current value of the clock. | ||
func now() throws -> Duration | ||
|
||
/// Query the resolution of the clock. | ||
/// | ||
/// The nanoseconds field of the output is always less than 1000000000. | ||
func resolution() throws -> Duration | ||
} | ||
|
||
/// A wall clock that uses the system's wall clock. | ||
public struct SystemWallClock: WallClock { | ||
private var underlying: SystemExtras.Clock { | ||
#if os(Linux) | ||
return .boottime | ||
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
return .rawMonotonic | ||
#elseif os(OpenBSD) || os(FreeBSD) || os(WASI) | ||
return .monotonic | ||
#else | ||
#error("Unsupported platform") | ||
#endif | ||
} | ||
|
||
public init() {} | ||
|
||
public func now() throws -> WallClock.Duration { | ||
let timeSpec = try WASIAbi.Errno.translatingPlatformErrno { | ||
try underlying.currentTime() | ||
} | ||
return (seconds: UInt64(timeSpec.seconds), nanoseconds: UInt32(timeSpec.nanoseconds)) | ||
} | ||
|
||
public func resolution() throws -> WallClock.Duration { | ||
let timeSpec = try WASIAbi.Errno.translatingPlatformErrno { | ||
try underlying.resolution() | ||
} | ||
return (seconds: UInt64(timeSpec.seconds), nanoseconds: UInt32(timeSpec.nanoseconds)) | ||
} | ||
} | ||
|
||
/// WASI monotonic clock interface based on WASI Preview 2 `monotonic-clock` interface. | ||
/// | ||
/// See also https://github.com/WebAssembly/wasi-clocks/blob/v0.2.0/wit/monotonic-clock.wit | ||
public protocol MonotonicClock { | ||
/// An instant in time, in nanoseconds. | ||
typealias Instant = UInt64 | ||
/// A duration of time, in nanoseconds. | ||
typealias Duration = UInt64 | ||
|
||
/// Read the current value of the clock. | ||
func now() throws -> Instant | ||
|
||
/// Query the resolution of the clock. Returns the duration of time | ||
/// corresponding to a clock tick. | ||
func resolution() throws -> Duration | ||
} | ||
|
||
/// A monotonic clock that uses the system's monotonic clock. | ||
public struct SystemMonotonicClock: MonotonicClock { | ||
private var underlying: SystemExtras.Clock { | ||
#if os(Linux) | ||
return .monotonic | ||
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
return .rawUptime | ||
#elseif os(WASI) | ||
return .monotonic | ||
#elseif os(OpenBSD) || os(FreeBSD) | ||
return .uptime | ||
#else | ||
#error("Unsupported platform") | ||
#endif | ||
} | ||
|
||
public init() {} | ||
|
||
public func now() throws -> MonotonicClock.Instant { | ||
let timeSpec = try WASIAbi.Errno.translatingPlatformErrno { | ||
try underlying.currentTime() | ||
} | ||
return WASIAbi.Timestamp(platformTimeSpec: timeSpec) | ||
} | ||
|
||
public func resolution() throws -> MonotonicClock.Duration { | ||
let timeSpec = try WASIAbi.Errno.translatingPlatformErrno { | ||
try underlying.resolution() | ||
} | ||
return WASIAbi.Timestamp(platformTimeSpec: timeSpec) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters