mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-22 04:10:40 +00:00
Add shims and adjust availability to allow for swift 5.6 toolchains to build until source stability is achieved (#100)
* Add shims and adjust availability to allow for swift 5.6 toolchains to build until source stability is achieved * Restore availability again
This commit is contained in:
1 parent
f9d0911c0c
commit
98d4229317
15 files changed
+2247
-23
No files matched your search
+6
-13
@@ -11,32 +11,25 @@ let package = Package(
|
||||
.watchOS("6.0")
|
||||
],
|
||||
products: [
|
||||
.library(
|
||||
name: "AsyncAlgorithms",
|
||||
targets: ["AsyncAlgorithms"]),
|
||||
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"]),
|
||||
.library(name: "ClockShims", type: .static, targets: ["ClockShims"]),
|
||||
.library(name: "AsyncSequenceValidation", targets: ["AsyncSequenceValidation"]),
|
||||
.library(name: "_CAsyncSequenceValidationSupport", type: .static, targets: ["AsyncSequenceValidation"])
|
||||
],
|
||||
dependencies: [],
|
||||
targets: [
|
||||
.target(
|
||||
name: "AsyncAlgorithms"),
|
||||
.target(name: "AsyncAlgorithms", dependencies: ["ClockShims"]),
|
||||
.target(name: "ClockShims"),
|
||||
.target(
|
||||
name: "AsyncSequenceValidation",
|
||||
dependencies: ["_CAsyncSequenceValidationSupport"],
|
||||
swiftSettings: [
|
||||
.unsafeFlags([
|
||||
"-Xfrontend", "-enable-experimental-pairwise-build-block"
|
||||
])
|
||||
]),
|
||||
dependencies: ["_CAsyncSequenceValidationSupport", "AsyncAlgorithms"]),
|
||||
.systemLibrary(name: "_CAsyncSequenceValidationSupport"),
|
||||
.testTarget(
|
||||
name: "AsyncAlgorithmsTests",
|
||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation"],
|
||||
swiftSettings: [
|
||||
.unsafeFlags([
|
||||
"-Xfrontend", "-disable-availability-checking",
|
||||
"-Xfrontend", "-enable-experimental-pairwise-build-block"
|
||||
"-Xfrontend", "-disable-availability-checking"
|
||||
])
|
||||
]),
|
||||
]
|
||||
|
||||
@@ -86,21 +86,18 @@ Finally, add `import AsyncAlgorithms` to your source code.
|
||||
|
||||
## Getting Started
|
||||
|
||||
⚠️ Please note that this package currently requires a recent [Swift Trunk Development toolchain](https://www.swift.org/download/#trunk-development-main). More information on how to use custom toolchains with Xcode can be viewed [here](https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/AlternativeToolchains.html).
|
||||
|
||||
### Building/Testing Using Xcode on macOS
|
||||
|
||||
1. Download the most recent development Xcode toolchain.
|
||||
2. Install the package
|
||||
4. Select the development toolchain in Xcode
|
||||
4. Open the `swift-async-algorithms` package directory in Xcode
|
||||
5. Build or Test in Xcode as normal
|
||||
1. Open the `swift-async-algorithms` package directory in Xcode
|
||||
2. Build or Test in Xcode as normal
|
||||
|
||||
### Building/Testing Using Swift Package Manager
|
||||
|
||||
⚠️ Note: `swift test` does not currently work properly with custom toolchains for this package.
|
||||
1. In the `swift-async-algorithms` directory run `swift build` or `swift test` accordingly
|
||||
|
||||
### Building/Testing on Linux
|
||||
|
||||
1. Download the most recent development toolchain for your Linux distribution
|
||||
1. Download the most recent toolchain for your Linux distribution
|
||||
2. Decompress the archive to a path in which the `swift` executable is in the binary search path environment variable (`$PATH`)
|
||||
3. In the `swift-async-algorithms` directory run `swift build` or `swift test` accordingly
|
||||
|
||||
@@ -108,6 +105,8 @@ Finally, add `import AsyncAlgorithms` to your source code.
|
||||
|
||||
The Swift Async Algorithms package has a goal of being source stable as soon as possible; version numbers will follow [Semantic Versioning](https://semver.org/). Source breaking changes to public API can only land in a new major version.
|
||||
|
||||
Before the version 1.0 the `swift-async-algorithms` package will not be source or ABI stable. Particularly the shims associated with `Clock`, `Instant` and `Duration` are present just to provide backwards compatability to older toolchains. As soon as this is available widely the shims will be removed; which will be an ABI breaking change.
|
||||
|
||||
The public API of version 1.0 of the `swift-async-algorithms` package will consist of non-underscored declarations that are marked `public` in the `AsyncAlgorithms` module. Interfaces that aren't part of the public API may continue to change in any release, including patch releases.
|
||||
|
||||
Future minor versions of the package may introduce changes to these rules as needed.
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_exported import ClockShims
|
||||
@@ -56,6 +56,46 @@ public struct AsyncSequenceValidationDiagram : Sendable {
|
||||
return Test(inputs: accumulated.inputs, sequence: accumulated.operation, output: Specification(specification: output.component, location: output.location))
|
||||
}
|
||||
|
||||
public static func buildBlock<Operation: AsyncSequence>(_ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||
let part1 = buildPartialBlock(first: sequence)
|
||||
let part2 = buildPartialBlock(accumulated: part1, next: output)
|
||||
return part2
|
||||
}
|
||||
|
||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||
let part1 = buildPartialBlock(first: input1)
|
||||
let part2 = buildPartialBlock(accumulated: part1, next: sequence)
|
||||
let part3 = buildPartialBlock(accumulated: part2, next: output)
|
||||
return part3
|
||||
}
|
||||
|
||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ input2: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||
let part1 = buildPartialBlock(first: input1)
|
||||
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
||||
let part3 = buildPartialBlock(accumulated: part2, next: sequence)
|
||||
let part4 = buildPartialBlock(accumulated: part3, next: output)
|
||||
return part4
|
||||
}
|
||||
|
||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ input2: Component<String>, _ input3: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||
let part1 = buildPartialBlock(first: input1)
|
||||
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
||||
let part3 = buildPartialBlock(accumulated: part2, next: input3)
|
||||
let part4 = buildPartialBlock(accumulated: part3, next: sequence)
|
||||
let part5 = buildPartialBlock(accumulated: part4, next: output)
|
||||
return part5
|
||||
}
|
||||
|
||||
public static func buildBlock<Operation: AsyncSequence>(_ input1: Component<String>, _ input2: Component<String>, _ input3: Component<String>, _ input4: Component<String>, _ sequence: Component<Operation>, _ output: Component<String>) -> some AsyncSequenceValidationTest where Operation.Element == String {
|
||||
let part1 = buildPartialBlock(first: input1)
|
||||
let part2 = buildPartialBlock(accumulated: part1, next: input2)
|
||||
let part3 = buildPartialBlock(accumulated: part2, next: input3)
|
||||
let part4 = buildPartialBlock(accumulated: part3, next: input3)
|
||||
let part5 = buildPartialBlock(accumulated: part4, next: sequence)
|
||||
let part6 = buildPartialBlock(accumulated: part5, next: output)
|
||||
return part6
|
||||
}
|
||||
|
||||
let queue: WorkQueue
|
||||
|
||||
public var inputs: InputList
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import AsyncAlgorithms
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension AsyncSequenceValidationDiagram {
|
||||
public struct Clock {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import _CAsyncSequenceValidationSupport
|
||||
import AsyncAlgorithms
|
||||
|
||||
@_silgen_name("swift_job_run")
|
||||
@usableFromInline
|
||||
@@ -33,7 +34,7 @@ extension AsyncSequenceValidationDiagram {
|
||||
let sequence: Operation
|
||||
let output: Specification
|
||||
|
||||
func test<C: _Concurrency.Clock>(with clock: C, activeTicks: [C.Instant], output: Specification, _ event: (String) -> Void) async throws {
|
||||
func test<C: ClockShims.Clock>(with clock: C, activeTicks: [C.Instant], output: Specification, _ event: (String) -> Void) async throws {
|
||||
var iterator = sequence.makeAsyncIterator()
|
||||
do {
|
||||
for tick in activeTicks {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 mechanism in which to measure time, and delay work until a given point
|
||||
/// in time.
|
||||
///
|
||||
/// Types that conform to the `Clock` protocol define a concept of "now" which
|
||||
/// is the specific instant in time that property is accessed. Any pair of calls
|
||||
/// to the `now` property may have a minimum duration between them - this
|
||||
/// minimum resolution is exposed by the `minimumResolution` property to inform
|
||||
/// any user of the type the expected granularity of accuracy.
|
||||
///
|
||||
/// One of the primary uses for clocks is to schedule task sleeping. This method
|
||||
/// resumes the calling task after a given deadline has been met or passed with
|
||||
/// a given tolerance value. The tolerance is expected as a leeway around the
|
||||
/// deadline. The clock may reschedule tasks within the tolerance to ensure
|
||||
/// efficient execution of resumptions by reducing potential operating system
|
||||
/// wake-ups. If no tolerance is specified (i.e. nil is passed in) the sleep
|
||||
/// function is expected to schedule with a default tolerance strategy.
|
||||
///
|
||||
/// For more information about specific clocks see `ContinuousClock` and
|
||||
/// `SuspendingClock`.
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public protocol Clock: Sendable {
|
||||
associatedtype Instant: InstantProtocol
|
||||
|
||||
var now: Instant { get }
|
||||
var minimumResolution: Instant.Duration { get }
|
||||
|
||||
func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
|
||||
}
|
||||
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Clock {
|
||||
/// Measure the elapsed time to execute a closure.
|
||||
///
|
||||
/// let clock = ContinuousClock()
|
||||
/// let elapsed = clock.measure {
|
||||
/// someWork()
|
||||
/// }
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public func measure(_ work: () throws -> Void) rethrows -> Instant.Duration {
|
||||
let start = now
|
||||
try work()
|
||||
let end = now
|
||||
return start.duration(to: end)
|
||||
}
|
||||
|
||||
/// Measure the elapsed time to execute an asynchronous closure.
|
||||
///
|
||||
/// let clock = ContinuousClock()
|
||||
/// let elapsed = await clock.measure {
|
||||
/// await someWork()
|
||||
/// }
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public func measure(
|
||||
_ work: () async throws -> Void
|
||||
) async rethrows -> Instant.Duration {
|
||||
let start = now
|
||||
try await work()
|
||||
let end = now
|
||||
return start.duration(to: end)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
#if canImport(Darwin)
|
||||
@_implementationOnly import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@_implementationOnly import Glibc
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
|
||||
/// 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(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public struct ContinuousClock {
|
||||
/// A continuous point in time used for `ContinuousClock`.
|
||||
public struct Instant: Codable, Sendable {
|
||||
internal var _value: Duration
|
||||
|
||||
internal init(_value: Duration) {
|
||||
self._value = _value
|
||||
}
|
||||
}
|
||||
|
||||
public init() { }
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
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(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public static var continuous: ContinuousClock { return ContinuousClock() }
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
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: Duration {
|
||||
var ts = timespec()
|
||||
#if canImport(Darwin)
|
||||
clock_getres(CLOCK_MONOTONIC, &ts)
|
||||
#elseif canImport(Glibc)
|
||||
clock_getres(CLOCK_BOOTTIME, &ts)
|
||||
#endif
|
||||
return .seconds(ts.tv_sec) + .nanoseconds(ts.tv_nsec)
|
||||
}
|
||||
|
||||
/// The current continuous instant.
|
||||
public static var now: ContinuousClock.Instant {
|
||||
var ts = timespec()
|
||||
#if canImport(Darwin)
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts)
|
||||
#elseif canImport(Glibc)
|
||||
clock_gettime(CLOCK_BOOTTIME, &ts)
|
||||
#endif
|
||||
return ContinuousClock.Instant(_value:
|
||||
.seconds(ts.tv_sec) + .nanoseconds(ts.tv_nsec))
|
||||
}
|
||||
|
||||
/// 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: Duration? = nil
|
||||
) async throws {
|
||||
let duration = deadline - .now
|
||||
let (seconds, attoseconds) = duration.components
|
||||
let nanoseconds = attoseconds / 1_000_000_000 + seconds * 1_000_000_000
|
||||
if nanoseconds > 0 {
|
||||
try await Task.sleep(nanoseconds: UInt64(nanoseconds))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension ContinuousClock.Instant: InstantProtocol {
|
||||
public static var now: ContinuousClock.Instant { ContinuousClock.now }
|
||||
|
||||
public func advanced(by duration: Duration) -> ContinuousClock.Instant {
|
||||
return ContinuousClock.Instant(_value: _value + duration)
|
||||
}
|
||||
|
||||
public func duration(to other: ContinuousClock.Instant) -> 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: Duration
|
||||
) -> ContinuousClock.Instant {
|
||||
lhs.advanced(by: rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func += (
|
||||
_ lhs: inout ContinuousClock.Instant, _ rhs: Duration
|
||||
) {
|
||||
lhs = lhs.advanced(by: rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func - (
|
||||
_ lhs: ContinuousClock.Instant, _ rhs: Duration
|
||||
) -> ContinuousClock.Instant {
|
||||
lhs.advanced(by: .zero - rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func -= (
|
||||
_ lhs: inout ContinuousClock.Instant, _ rhs: Duration
|
||||
) {
|
||||
lhs = lhs.advanced(by: .zero - rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func - (
|
||||
_ lhs: ContinuousClock.Instant, _ rhs: ContinuousClock.Instant
|
||||
) -> Duration {
|
||||
rhs.duration(to: lhs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// A representation of high precision time.
|
||||
///
|
||||
/// `Duration` represents an elapsed time value with high precision in an
|
||||
/// integral form. It may be used for measurements of varying clock sources. In
|
||||
/// those cases it represents the elapsed time measured by that clock.
|
||||
/// Calculations using `Duration` may span from a negative value to a positive
|
||||
/// value and have a suitable range to at least cover attosecond scale for both
|
||||
/// small elapsed durations like sub-second precision to durations that span
|
||||
/// centuries.
|
||||
///
|
||||
/// Typical construction of `Duration` values should be created via the
|
||||
/// static methods for specific time values.
|
||||
///
|
||||
/// var d: Duration = .seconds(3)
|
||||
/// d += .milliseconds(33)
|
||||
/// print(d) // 3.033 seconds
|
||||
///
|
||||
/// `Duration` itself does not ferry any additional information other than the
|
||||
/// temporal measurement component; specifically leap seconds should be
|
||||
/// represented as an additional accessor since that is specific only to certain
|
||||
/// clock implementations.
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
@frozen
|
||||
public struct Duration: Sendable {
|
||||
/// The low 64 bits of a 128-bit signed integer value counting attoseconds.
|
||||
@usableFromInline
|
||||
internal var _low: UInt64
|
||||
|
||||
/// The high 64 bits of a 128-bit signed integer value counting attoseconds.
|
||||
@usableFromInline
|
||||
internal var _high: Int64
|
||||
|
||||
@inlinable
|
||||
internal init(_high: Int64, low: UInt64) {
|
||||
self._low = low
|
||||
self._high = _high
|
||||
}
|
||||
|
||||
internal init(_attoseconds: _Int128) {
|
||||
self.init(_high: _attoseconds.high, low: _attoseconds.low)
|
||||
}
|
||||
|
||||
/// Construct a `Duration` by adding attoseconds to a seconds value.
|
||||
///
|
||||
/// This is useful for when an external decomposed components of a `Duration`
|
||||
/// has been stored and needs to be reconstituted. Since the values are added
|
||||
/// no precondition is expressed for the attoseconds being limited to 1e18.
|
||||
///
|
||||
/// let d1 = Duration(
|
||||
/// secondsComponent: 3,
|
||||
/// attosecondsComponent: 123000000000000000)
|
||||
/// print(d1) // 3.123 seconds
|
||||
///
|
||||
/// let d2 = Duration(
|
||||
/// secondsComponent: 3,
|
||||
/// attosecondsComponent: -123000000000000000)
|
||||
/// print(d2) // 2.877 seconds
|
||||
///
|
||||
/// let d3 = Duration(
|
||||
/// secondsComponent: -3,
|
||||
/// attosecondsComponent: -123000000000000000)
|
||||
/// print(d3) // -3.123 seconds
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - secondsComponent: The seconds component portion of the `Duration`
|
||||
/// value.
|
||||
/// - attosecondsComponent: The attosecond component portion of the
|
||||
/// `Duration` value.
|
||||
public init(secondsComponent: Int64, attosecondsComponent: Int64) {
|
||||
self = Duration.seconds(secondsComponent) +
|
||||
Duration(_attoseconds: _Int128(attosecondsComponent))
|
||||
}
|
||||
|
||||
internal var _attoseconds: _Int128 {
|
||||
_Int128(high: _high, low: _low)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration {
|
||||
/// The composite components of the `Duration`.
|
||||
///
|
||||
/// This is intended for facilitating conversions to existing time types. The
|
||||
/// attoseconds value will not exceed 1e18 or be lower than -1e18.
|
||||
|
||||
public var components: (seconds: Int64, attoseconds: Int64) {
|
||||
let (seconds, attoseconds) = _attoseconds.dividedBy1e18()
|
||||
return (Int64(seconds), Int64(attoseconds))
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration {
|
||||
/// Construct a `Duration` given a number of seconds represented as a
|
||||
/// `BinaryInteger`.
|
||||
///
|
||||
/// let d: Duration = .seconds(77)
|
||||
///
|
||||
/// - Returns: A `Duration` representing a given number of seconds.
|
||||
|
||||
public static func seconds<T: BinaryInteger>(_ seconds: T) -> Duration {
|
||||
return Duration(_attoseconds:
|
||||
_Int128(seconds).multiplied(by: 1_000_000_000_000_000_000 as UInt64))
|
||||
}
|
||||
|
||||
/// Construct a `Duration` given a number of seconds represented as a
|
||||
/// `Double` by converting the value into the closest attosecond scale value.
|
||||
///
|
||||
/// let d: Duration = .seconds(22.93)
|
||||
///
|
||||
/// - Returns: A `Duration` representing a given number of seconds.
|
||||
|
||||
public static func seconds(_ seconds: Double) -> Duration {
|
||||
return Duration(_attoseconds: _Int128(seconds * 1_000_000_000_000_000_000))
|
||||
}
|
||||
|
||||
/// Construct a `Duration` given a number of milliseconds represented as a
|
||||
/// `BinaryInteger`.
|
||||
///
|
||||
/// let d: Duration = .milliseconds(645)
|
||||
///
|
||||
/// - Returns: A `Duration` representing a given number of milliseconds.
|
||||
|
||||
public static func milliseconds<T: BinaryInteger>(
|
||||
_ milliseconds: T
|
||||
) -> Duration {
|
||||
return Duration(_attoseconds:
|
||||
_Int128(milliseconds).multiplied(by: 1_000_000_000_000_000 as UInt64))
|
||||
}
|
||||
|
||||
/// Construct a `Duration` given a number of seconds milliseconds as a
|
||||
/// `Double` by converting the value into the closest attosecond scale value.
|
||||
///
|
||||
/// let d: Duration = .milliseconds(88.3)
|
||||
///
|
||||
/// - Returns: A `Duration` representing a given number of milliseconds.
|
||||
|
||||
public static func milliseconds(_ milliseconds: Double) -> Duration {
|
||||
return Duration(_attoseconds:
|
||||
_Int128(milliseconds * 1_000_000_000_000_000))
|
||||
}
|
||||
|
||||
/// Construct a `Duration` given a number of microseconds represented as a
|
||||
/// `BinaryInteger`.
|
||||
///
|
||||
/// let d: Duration = .microseconds(12)
|
||||
///
|
||||
/// - Returns: A `Duration` representing a given number of microseconds.
|
||||
|
||||
public static func microseconds<T: BinaryInteger>(
|
||||
_ microseconds: T
|
||||
) -> Duration {
|
||||
return Duration(_attoseconds:
|
||||
_Int128(microseconds).multiplied(by: 1_000_000_000_000 as UInt64))
|
||||
}
|
||||
|
||||
/// Construct a `Duration` given a number of seconds microseconds as a
|
||||
/// `Double` by converting the value into the closest attosecond scale value.
|
||||
///
|
||||
/// let d: Duration = .microseconds(382.9)
|
||||
///
|
||||
/// - Returns: A `Duration` representing a given number of microseconds.
|
||||
|
||||
public static func microseconds(_ microseconds: Double) -> Duration {
|
||||
return Duration(_attoseconds:
|
||||
_Int128(microseconds * 1_000_000_000_000))
|
||||
}
|
||||
|
||||
/// Construct a `Duration` given a number of nanoseconds represented as a
|
||||
/// `BinaryInteger`.
|
||||
///
|
||||
/// let d: Duration = .nanoseconds(1929)
|
||||
///
|
||||
/// - Returns: A `Duration` representing a given number of nanoseconds.
|
||||
|
||||
public static func nanoseconds<T: BinaryInteger>(
|
||||
_ nanoseconds: T
|
||||
) -> Duration {
|
||||
return Duration(_attoseconds:
|
||||
_Int128(nanoseconds).multiplied(by: 1_000_000_000))
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration: Codable {
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
var container = try decoder.unkeyedContainer()
|
||||
let high = try container.decode(Int64.self)
|
||||
let low = try container.decode(UInt64.self)
|
||||
self.init(_high: high, low: low)
|
||||
}
|
||||
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.unkeyedContainer()
|
||||
try container.encode(_high)
|
||||
try container.encode(_low)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration: Hashable {
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(_attoseconds)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration: Equatable {
|
||||
public static func == (_ lhs: Duration, _ rhs: Duration) -> Bool {
|
||||
return lhs._attoseconds == rhs._attoseconds
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration: Comparable {
|
||||
public static func < (_ lhs: Duration, _ rhs: Duration) -> Bool {
|
||||
return lhs._attoseconds < rhs._attoseconds
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration: AdditiveArithmetic {
|
||||
|
||||
public static var zero: Duration { Duration(_attoseconds: 0) }
|
||||
|
||||
|
||||
public static func + (_ lhs: Duration, _ rhs: Duration) -> Duration {
|
||||
return Duration(_attoseconds: lhs._attoseconds + rhs._attoseconds)
|
||||
}
|
||||
|
||||
|
||||
public static func - (_ lhs: Duration, _ rhs: Duration) -> Duration {
|
||||
return Duration(_attoseconds: lhs._attoseconds - rhs._attoseconds)
|
||||
}
|
||||
|
||||
|
||||
public static func += (_ lhs: inout Duration, _ rhs: Duration) {
|
||||
lhs = lhs + rhs
|
||||
}
|
||||
|
||||
|
||||
public static func -= (_ lhs: inout Duration, _ rhs: Duration) {
|
||||
lhs = lhs - rhs
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration {
|
||||
|
||||
public static func / (_ lhs: Duration, _ rhs: Double) -> Duration {
|
||||
return Duration(_attoseconds:
|
||||
_Int128(Double(lhs._attoseconds) / rhs))
|
||||
}
|
||||
|
||||
|
||||
public static func /= (_ lhs: inout Duration, _ rhs: Double) {
|
||||
lhs = lhs / rhs
|
||||
}
|
||||
|
||||
|
||||
public static func / <T: BinaryInteger>(
|
||||
_ lhs: Duration, _ rhs: T
|
||||
) -> Duration {
|
||||
Duration(_attoseconds: lhs._attoseconds / _Int128(rhs))
|
||||
}
|
||||
|
||||
|
||||
public static func /= <T: BinaryInteger>(_ lhs: inout Duration, _ rhs: T) {
|
||||
lhs = lhs / rhs
|
||||
}
|
||||
|
||||
|
||||
public static func / (_ lhs: Duration, _ rhs: Duration) -> Double {
|
||||
Double(lhs._attoseconds) / Double(rhs._attoseconds)
|
||||
}
|
||||
|
||||
|
||||
public static func * (_ lhs: Duration, _ rhs: Double) -> Duration {
|
||||
Duration(_attoseconds: _Int128(Double(lhs._attoseconds) * rhs))
|
||||
}
|
||||
|
||||
|
||||
public static func * <T: BinaryInteger>(
|
||||
_ lhs: Duration, _ rhs: T
|
||||
) -> Duration {
|
||||
Duration(_attoseconds: lhs._attoseconds * _Int128(rhs))
|
||||
}
|
||||
|
||||
|
||||
public static func *= <T: BinaryInteger>(_ lhs: inout Duration, _ rhs: T) {
|
||||
lhs = lhs * rhs
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration: CustomStringConvertible {
|
||||
|
||||
public var description: String {
|
||||
return (Double(_attoseconds) / 1e18).description + " seconds"
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Duration: DurationProtocol { }
|
||||
@@ -0,0 +1,35 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// A type that defines a duration for a given `InstantProtocol` type.
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public protocol DurationProtocol: Comparable, AdditiveArithmetic, Sendable {
|
||||
static func / (_ lhs: Self, _ rhs: Int) -> Self
|
||||
static func /= (_ lhs: inout Self, _ rhs: Int)
|
||||
static func * (_ lhs: Self, _ rhs: Int) -> Self
|
||||
static func *= (_ lhs: inout Self, _ rhs: Int)
|
||||
|
||||
static func / (_ lhs: Self, _ rhs: Self) -> Double
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension DurationProtocol {
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public static func /= (_ lhs: inout Self, _ rhs: Int) {
|
||||
lhs = lhs / rhs
|
||||
}
|
||||
|
||||
|
||||
public static func *= (_ lhs: inout Self, _ rhs: Int) {
|
||||
lhs = lhs * rhs
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// A type that defines a specific point in time for a given `Clock`.
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public protocol InstantProtocol: Comparable, Hashable, Sendable {
|
||||
associatedtype Duration: DurationProtocol
|
||||
func advanced(by duration: Duration) -> Self
|
||||
func duration(to other: Self) -> Duration
|
||||
}
|
||||
|
||||
/*
|
||||
disabled for now - this perturbs operator resolution
|
||||
extension InstantProtocol {
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func + (_ lhs: Self, _ rhs: Duration) -> Self {
|
||||
lhs.advanced(by: rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func += (_ lhs: inout Self, _ rhs: Duration) {
|
||||
lhs = lhs.advanced(by: rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func - (_ lhs: Self, _ rhs: Duration) -> Self {
|
||||
lhs.advanced(by: .zero - rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func -= (_ lhs: inout Self, _ rhs: Duration) {
|
||||
lhs = lhs.advanced(by: .zero - rhs)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inlinable
|
||||
public static func - (_ lhs: Self, _ rhs: Self) -> Duration {
|
||||
rhs.duration(to: lhs)
|
||||
}
|
||||
}
|
||||
*/
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,162 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
#if canImport(Darwin)
|
||||
@_implementationOnly import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@_implementationOnly import Glibc
|
||||
#else
|
||||
#error("Unsupported platform")
|
||||
#endif
|
||||
|
||||
/// A clock that measures time that always increments but stops incrementing
|
||||
/// while the system is asleep.
|
||||
///
|
||||
/// `SuspendingClock` can be considered as a system awake time clock. The frame
|
||||
/// of reference of the `Instant` may be bound machine boot or some other
|
||||
/// locally defined reference point. This means that the instants are
|
||||
/// only comparable on the same machine in the same booted session.
|
||||
///
|
||||
/// This clock is suitable for high resolution measurements of execution.
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
public struct SuspendingClock {
|
||||
public struct Instant: Codable, Sendable {
|
||||
internal var _value: Duration
|
||||
|
||||
internal init(_value: Duration) {
|
||||
self._value = _value
|
||||
}
|
||||
}
|
||||
|
||||
public init() { }
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension Clock where Self == SuspendingClock {
|
||||
/// A clock that measures time that always increments but stops incrementing
|
||||
/// while the system is asleep.
|
||||
///
|
||||
/// try await Task.sleep(until: .now + .seconds(3), clock: .suspending)
|
||||
///
|
||||
public static var suspending: SuspendingClock { return SuspendingClock() }
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension SuspendingClock: Clock {
|
||||
/// The current instant accounting for machine suspension.
|
||||
public var now: SuspendingClock.Instant {
|
||||
SuspendingClock.now
|
||||
}
|
||||
|
||||
/// The current instant accounting for machine suspension.
|
||||
public static var now: SuspendingClock.Instant {
|
||||
var ts = timespec()
|
||||
#if canImport(Darwin)
|
||||
clock_gettime(CLOCK_UPTIME_RAW, &ts)
|
||||
#elseif canImport(Glibc)
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
|
||||
#endif
|
||||
return SuspendingClock.Instant(_value:
|
||||
.seconds(ts.tv_sec) + .nanoseconds(ts.tv_nsec))
|
||||
}
|
||||
|
||||
/// The minimum non-zero resolution between any two calls to `now`.
|
||||
public var minimumResolution: Duration {
|
||||
var ts = timespec()
|
||||
#if canImport(Darwin)
|
||||
clock_getres(CLOCK_UPTIME_RAW, &ts)
|
||||
#elseif canImport(Glibc)
|
||||
clock_getres(CLOCK_MONOTONIC, &ts)
|
||||
#endif
|
||||
return .seconds(ts.tv_sec) + .nanoseconds(ts.tv_nsec)
|
||||
}
|
||||
|
||||
/// 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: Duration? = nil
|
||||
) async throws {
|
||||
let duration = deadline - .now
|
||||
let (seconds, attoseconds) = duration.components
|
||||
let nanoseconds = attoseconds / 1_000_000_000 + seconds * 1_000_000_000
|
||||
if nanoseconds > 0 {
|
||||
try await Task.sleep(nanoseconds: UInt64(nanoseconds))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
extension SuspendingClock.Instant: InstantProtocol {
|
||||
public static var now: SuspendingClock.Instant { SuspendingClock().now }
|
||||
|
||||
public func advanced(by duration: Duration) -> SuspendingClock.Instant {
|
||||
SuspendingClock.Instant(_value: _value + duration)
|
||||
}
|
||||
|
||||
public func duration(to other: SuspendingClock.Instant) -> Duration {
|
||||
other._value - _value
|
||||
}
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(_value)
|
||||
}
|
||||
|
||||
public static func == (
|
||||
_ lhs: SuspendingClock.Instant, _ rhs: SuspendingClock.Instant
|
||||
) -> Bool {
|
||||
return lhs._value == rhs._value
|
||||
}
|
||||
|
||||
public static func < (
|
||||
_ lhs: SuspendingClock.Instant, _ rhs: SuspendingClock.Instant
|
||||
) -> Bool {
|
||||
return lhs._value < rhs._value
|
||||
}
|
||||
|
||||
public static func + (
|
||||
_ lhs: SuspendingClock.Instant, _ rhs: Duration
|
||||
) -> SuspendingClock.Instant {
|
||||
lhs.advanced(by: rhs)
|
||||
}
|
||||
|
||||
public static func += (
|
||||
_ lhs: inout SuspendingClock.Instant, _ rhs: Duration
|
||||
) {
|
||||
lhs = lhs.advanced(by: rhs)
|
||||
}
|
||||
|
||||
public static func - (
|
||||
_ lhs: SuspendingClock.Instant, _ rhs: Duration
|
||||
) -> SuspendingClock.Instant {
|
||||
lhs.advanced(by: .zero - rhs)
|
||||
}
|
||||
|
||||
public static func -= (
|
||||
_ lhs: inout SuspendingClock.Instant, _ rhs: Duration
|
||||
) {
|
||||
lhs = lhs.advanced(by: .zero - rhs)
|
||||
}
|
||||
|
||||
public static func - (
|
||||
_ lhs: SuspendingClock.Instant, _ rhs: SuspendingClock.Instant
|
||||
) -> Duration {
|
||||
rhs.duration(to: lhs)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension Task where Success == Never, Failure == Never {
|
||||
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
|
||||
@_disfavoredOverload
|
||||
public static func sleep<C: Clock>(
|
||||
until deadine: C.Instant,
|
||||
tolerance: C.Instant.Duration? = nil,
|
||||
clock: C
|
||||
) async throws {
|
||||
try await clock.sleep(until: deadine, tolerance: tolerance)
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import AsyncAlgorithms
|
||||
|
||||
public struct ManualClock: Clock {
|
||||
public struct Step: DurationProtocol {
|
||||
fileprivate var rawValue: Int
|
||||
|
||||
Reference in new issue
Block a user