11
11
//===----------------------------------------------------------------------===//
12
12
import Swift
13
13
14
- /// A mechanism in which to measure time, and delay work until a given point
14
+ /// A mechanism in which to measure time, and delay work until a given point
15
15
/// in time.
16
16
///
17
- /// Types that conform to the `Clock` protocol define a concept of "now" which
17
+ /// Types that conform to the `Clock` protocol define a concept of "now" which
18
18
/// is the specific instant in time that property is accessed. Any pair of calls
19
19
/// to the `now` property may have a minimum duration between them - this
20
20
/// minimum resolution is exposed by the `minimumResolution` property to inform
21
- /// any user of the type the expected granularity of accuracy.
21
+ /// any user of the type the expected granularity of accuracy.
22
22
///
23
23
/// One of the primary uses for clocks is to schedule task sleeping. This method
24
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
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
27
/// efficient execution of resumptions by reducing potential operating system
28
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.
29
+ /// function is expected to schedule with a default tolerance strategy.
30
30
///
31
- /// For more information about specific clocks see `ContinuousClock` and
31
+ /// For more information about specific clocks see `ContinuousClock` and
32
32
/// `SuspendingClock`.
33
33
@available ( SwiftStdlib 5 . 7 , * )
34
34
public protocol Clock < Duration> : Sendable {
@@ -41,8 +41,56 @@ public protocol Clock<Duration>: Sendable {
41
41
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
42
42
func sleep( until deadline: Instant , tolerance: Instant . Duration ? ) async throws
43
43
#endif
44
- }
45
44
45
+ #if !$Embedded
46
+ /// Choose which Dispatch clock to use with DispatchExecutor
47
+ ///
48
+ /// This controls which Dispatch clock is used to enqueue delayed jobs
49
+ /// when using this Clock.
50
+ @available ( SwiftStdlib 6 . 2 , * )
51
+ var dispatchClockID : DispatchClockID { get }
52
+ #endif
53
+
54
+ /// Convert a Clock-specific Duration to a Swift Duration
55
+ ///
56
+ /// Some clocks may define `C.Duration` to be something other than a
57
+ /// `Swift.Duration`, but that makes it tricky to convert timestamps
58
+ /// between clocks, which is something we want to be able to support.
59
+ /// This method will convert whatever `C.Duration` is to a `Swift.Duration`.
60
+ ///
61
+ /// Parameters:
62
+ ///
63
+ /// - from duration: The `Duration` to convert
64
+ ///
65
+ /// Returns: A `Swift.Duration` representing the equivalent duration, or
66
+ /// `nil` if this function is not supported.
67
+ @available ( SwiftStdlib 6 . 2 , * )
68
+ func convert( from duration: Duration ) -> Swift . Duration ?
69
+
70
+ /// Convert a Swift Duration to a Clock-specific Duration
71
+ ///
72
+ /// Parameters:
73
+ ///
74
+ /// - from duration: The `Swift.Duration` to convert.
75
+ ///
76
+ /// Returns: A `Duration` representing the equivalent duration, or
77
+ /// `nil` if this function is not supported.
78
+ @available ( SwiftStdlib 6 . 2 , * )
79
+ func convert( from duration: Swift . Duration ) -> Duration ?
80
+
81
+ /// Convert an `Instant` from some other clock's `Instant`
82
+ ///
83
+ /// Parameters:
84
+ ///
85
+ /// - instant: The instant to convert.
86
+ // - from clock: The clock to convert from.
87
+ ///
88
+ /// Returns: An `Instant` representing the equivalent instant, or
89
+ /// `nil` if this function is not supported.
90
+ @available ( SwiftStdlib 6 . 2 , * )
91
+ func convert< OtherClock: Clock > ( instant: OtherClock . Instant ,
92
+ from clock: OtherClock ) -> Instant ?
93
+ }
46
94
47
95
@available ( SwiftStdlib 5 . 7 , * )
48
96
extension Clock {
@@ -88,6 +136,50 @@ extension Clock {
88
136
}
89
137
}
90
138
139
+ @available ( SwiftStdlib 6 . 2 , * )
140
+ extension Clock {
141
+ #if !$Embedded
142
+ public var dispatchClockID : DispatchClockID {
143
+ return . suspending
144
+ }
145
+ #endif
146
+
147
+ // For compatibility, return `nil` if this is not implemented
148
+ public func convert( from duration: Duration ) -> Swift . Duration ? {
149
+ return nil
150
+ }
151
+
152
+ public func convert( from duration: Swift . Duration ) -> Duration ? {
153
+ return nil
154
+ }
155
+
156
+ public func convert< OtherClock: Clock > ( instant: OtherClock . Instant ,
157
+ from clock: OtherClock ) -> Instant ? {
158
+ let ourNow = now
159
+ let otherNow = clock. now
160
+ let otherDuration = otherNow. duration ( to: instant)
161
+
162
+ // Convert to `Swift.Duration`
163
+ guard let duration = clock. convert ( from: otherDuration) else {
164
+ return nil
165
+ }
166
+
167
+ // Convert from `Swift.Duration`
168
+ guard let ourDuration = convert ( from: duration) else {
169
+ return nil
170
+ }
171
+
172
+ return ourNow. advanced ( by: ourDuration)
173
+ }
174
+ }
175
+
176
+ @available ( SwiftStdlib 6 . 2 , * )
177
+ extension Clock where Duration == Swift . Duration {
178
+ public func convert( from duration: Duration ) -> Duration ? {
179
+ return duration
180
+ }
181
+ }
182
+
91
183
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
92
184
@available ( SwiftStdlib 5 . 7 , * )
93
185
extension Clock {
0 commit comments