|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +import Swift |
| 13 | + |
| 14 | +/// A mechanism in which to measure time, and delay work until a given point |
| 15 | +/// in time. |
| 16 | +/// |
| 17 | +/// Types that conform to the `Clock` protocol define a concept of "now" which |
| 18 | +/// is the specific instant in time that property is accessed. Any pair of calls |
| 19 | +/// to the `now` property may have a minimum duration between them - this |
| 20 | +/// minimum resolution is exposed by the `minimumResolution` property to inform |
| 21 | +/// any user of the type the expected granularity of accuracy. |
| 22 | +/// |
| 23 | +/// One of the primary uses for clocks is to schedule task sleeping. This method |
| 24 | +/// resumes the calling task after a given deadline has been met or passed with |
| 25 | +/// a given tolerance value. The tolerance is expected as a leeway around the |
| 26 | +/// deadline. The clock may reschedule tasks within the tolerance to ensure |
| 27 | +/// efficient execution of resumptions by reducing potential operating system |
| 28 | +/// wake-ups. If no tolerance is specified (i.e. nil is passed in) the sleep |
| 29 | +/// function is expected to schedule with a default tolerance strategy. |
| 30 | +/// |
| 31 | +/// For more information about specific clocks see `ContinuousClock` and |
| 32 | +/// `SuspendingClock`. |
| 33 | +@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) |
| 34 | +public protocol Clock: Sendable { |
| 35 | + associatedtype Instant: InstantProtocol |
| 36 | + |
| 37 | + var now: Instant { get } |
| 38 | + var minimumResolution: Instant.Duration { get } |
| 39 | + |
| 40 | + func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) |
| 45 | +extension Clock { |
| 46 | + /// Measure the elapsed time to execute a closure. |
| 47 | + /// |
| 48 | + /// let clock = ContinuousClock() |
| 49 | + /// let elapsed = clock.measure { |
| 50 | + /// someWork() |
| 51 | + /// } |
| 52 | + @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) |
| 53 | + public func measure(_ work: () throws -> Void) rethrows -> Instant.Duration { |
| 54 | + let start = now |
| 55 | + try work() |
| 56 | + let end = now |
| 57 | + return start.duration(to: end) |
| 58 | + } |
| 59 | + |
| 60 | + /// Measure the elapsed time to execute an asynchronous closure. |
| 61 | + /// |
| 62 | + /// let clock = ContinuousClock() |
| 63 | + /// let elapsed = await clock.measure { |
| 64 | + /// await someWork() |
| 65 | + /// } |
| 66 | + @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) |
| 67 | + public func measure( |
| 68 | + _ work: () async throws -> Void |
| 69 | + ) async rethrows -> Instant.Duration { |
| 70 | + let start = now |
| 71 | + try await work() |
| 72 | + let end = now |
| 73 | + return start.duration(to: end) |
| 74 | + } |
| 75 | +} |
| 76 | + |
0 commit comments