Files
swift-mirror/stdlib/public/Concurrency/ContinuousClock.swift
Philippe Hausler e675b310f8 [SE-0329] Clock/Instant/Duration (#40609)
* [WIP] Initial draft at v2 Clock/Instant/Duration

* Ensure the literal types for _DoubleWide are able to be at least 64 bits on 32 bit platforms

* static cast timespec members to long

* Remove runtime exports from clock functions

* Export clock functions in implementations as they are in headers

* Clean up internal properties by adding leading underscores, refine availability to a TBD marker macro, and break at 80 lines to match style

* Shift operators to concrete Instant types to avoid complexity in solver resolution

* Adjust diagnostic note and error expectation of ambiguities to reflect new potential solver (perhaps incorrect) solutions

* Update stdlib/public/Concurrency/TaskSleep.swift

Co-authored-by: Karoy Lorentey <klorentey@apple.com>

* [stdlib][NFC] Remove trailing whitespace

* [stdlib] Remove _DoubleWidth from stdlib's ABI

* [stdlib] Strip downd _DoubleWidth to _[U]Int128

* Additional adjustments to diagnostic notes and errors expectation of ambiguities to reflect new potential solver (perhaps incorrect) solutions

* Disable type checker performance validation for operator overload inferences (rdar://33958047)

* Decorate Duration, DurationProtocol, Instant and clocks with @available(SwiftStdlib 9999, *)

* Restore diagnostic ambiguity test assertion (due to availability)

* Add a rough attempt at implementing time accessors on win32

* Remove unused clock id, rename SPI for swift clock ids and correct a few more missing availabilities

* remove obsolete case of realtime clock for dispatch after callout

* Use the default implementation of ~ for Int128 and UInt128

* Ensure diagnostic ambiguitiy applies evenly to all platforms and their resolved types

* Restore the simd vector build modifications (merge damage)

* Update to latest naming results for Instant.Duration

* Updates to latest proposal initializers and accessors and adjust encoding/decoding to string based serialization

* Update availability for Clock/Instant/Duration methods and types to be 5.7

* Correct *Clock.now to report via the correct runtime API

* Ensure the hashing of Duration is based upon the attoseconds hashing

* Avoid string based encoding and resort back to high and low bit encoding/decoding but as unkeyed

* Adjust naming of component initializer to use suffixes on parameters

* Duration decoding should use a mutable container for decoding

* fix up components initializer and decode access

* Add platform base initializers for timespec and tiemval to and from Duration

* Add some first draft documentation for standard library types Duration, DurationProtocol and InstantProtocol

* Another round of documentation prose and some drive-by availability fixes

* InstantProtocol availability should be 5.7

* Correct linux timeval creation to be Int and not Int32

Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2022-02-17 09:32:46 -08:00

166 lines
5.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Swift
/// A clock that measures time that always increments but does not stop
/// incrementing while the system is asleep.
///
/// `ContinuousClock` can be considered as a stopwatch style time. The frame of
/// reference of the `Instant` may be bound to process launch, machine boot or
/// some other locally defined reference point. This means that the instants are
/// only comparable locally during the execution of a program.
///
/// This clock is suitable for high resolution measurements of execution.
@available(SwiftStdlib 5.7, *)
public struct ContinuousClock {
/// A continuous point in time used for `ContinuousClock`.
public struct Instant: Codable, Sendable {
internal var _value: Swift.Duration
internal init(_value: Swift.Duration) {
self._value = _value
}
}
public init() { }
}
@available(SwiftStdlib 5.7, *)
extension Clock where Self == ContinuousClock {
/// A clock that measures time that always increments but does not stop
/// incrementing while the system is asleep.
///
/// try await Task.sleep(until: .now + .seconds(3), clock: .continuous)
///
@available(SwiftStdlib 5.7, *)
public static var continuous: ContinuousClock { return ContinuousClock() }
}
@available(SwiftStdlib 5.7, *)
extension ContinuousClock: Clock {
/// The current continuous instant.
public var now: ContinuousClock.Instant {
ContinuousClock.now
}
/// The minimum non-zero resolution between any two calls to `now`.
public var minimumResolution: Swift.Duration {
var seconds = Int64(0)
var nanoseconds = Int64(0)
_getClockRes(
seconds: &seconds,
nanoseconds: &nanoseconds,
clock: .continuous)
return .seconds(seconds) + .nanoseconds(nanoseconds)
}
/// The current continuous instant.
public static var now: ContinuousClock.Instant {
var seconds = Int64(0)
var nanoseconds = Int64(0)
_getTime(
seconds: &seconds,
nanoseconds: &nanoseconds,
clock: .continuous)
return ContinuousClock.Instant(_value:
.seconds(seconds) + .nanoseconds(nanoseconds))
}
/// Suspend task execution until a given deadline within a tolerance.
/// If no tolerance is specified then the system may adjust the deadline
/// to coalesce CPU wake-ups to more efficiently process the wake-ups in
/// a more power efficient manner.
///
/// If the task is canceled before the time ends, this function throws
/// `CancellationError`.
///
/// This function doesn't block the underlying thread.
public func sleep(
until deadline: Instant, tolerance: Swift.Duration? = nil
) async throws {
let (seconds, attoseconds) = deadline._value.components
let nanoseconds = attoseconds / 1_000_000_000
try await Task._sleep(until:seconds, nanoseconds,
tolerance: tolerance,
clock: .continuous)
}
}
@available(SwiftStdlib 5.7, *)
extension ContinuousClock.Instant: InstantProtocol {
public static var now: ContinuousClock.Instant { ContinuousClock.now }
public func advanced(by duration: Swift.Duration) -> ContinuousClock.Instant {
return ContinuousClock.Instant(_value: _value + duration)
}
public func duration(to other: ContinuousClock.Instant) -> Swift.Duration {
other._value - _value
}
public func hash(into hasher: inout Hasher) {
hasher.combine(_value)
}
public static func == (
_ lhs: ContinuousClock.Instant, _ rhs: ContinuousClock.Instant
) -> Bool {
return lhs._value == rhs._value
}
public static func < (
_ lhs: ContinuousClock.Instant, _ rhs: ContinuousClock.Instant
) -> Bool {
return lhs._value < rhs._value
}
@_alwaysEmitIntoClient
@inlinable
public static func + (
_ lhs: ContinuousClock.Instant, _ rhs: Swift.Duration
) -> ContinuousClock.Instant {
lhs.advanced(by: rhs)
}
@_alwaysEmitIntoClient
@inlinable
public static func += (
_ lhs: inout ContinuousClock.Instant, _ rhs: Swift.Duration
) {
lhs = lhs.advanced(by: rhs)
}
@_alwaysEmitIntoClient
@inlinable
public static func - (
_ lhs: ContinuousClock.Instant, _ rhs: Swift.Duration
) -> ContinuousClock.Instant {
lhs.advanced(by: .zero - rhs)
}
@_alwaysEmitIntoClient
@inlinable
public static func -= (
_ lhs: inout ContinuousClock.Instant, _ rhs: Swift.Duration
) {
lhs = lhs.advanced(by: .zero - rhs)
}
@_alwaysEmitIntoClient
@inlinable
public static func - (
_ lhs: ContinuousClock.Instant, _ rhs: ContinuousClock.Instant
) -> Swift.Duration {
rhs.duration(to: lhs)
}
}