Skip to content

Commit 871a062

Browse files
committed
[Concurrency] Remove canonicalization code.
I don't think we actually need this. If you have a non-canonical (i.e. derived) clock, you can just implement `enqueue` and/or `run` and call those methods on the clock you're wrapping.
1 parent 7af5b8d commit 871a062

File tree

3 files changed

+2
-135
lines changed

3 files changed

+2
-135
lines changed

stdlib/public/Concurrency/Clock.swift

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import Swift
3434
public protocol Clock<Duration>: Sendable {
3535
associatedtype Duration
3636
associatedtype Instant: InstantProtocol where Instant.Duration == Duration
37-
associatedtype CanonicalClock: Clock = Self
3837

3938
var now: Instant { get }
4039
var minimumResolution: Instant.Duration { get }
@@ -76,58 +75,6 @@ public protocol Clock<Duration>: Sendable {
7675
on executor: some Executor,
7776
at instant: Instant, tolerance: Duration?)
7877
#endif
79-
80-
/// Obtain the equivalent, canonical clock, or `nil` if this clock
81-
/// is already canonical.
82-
///
83-
/// A non-canonical clock is a clock with some relationship to a base,
84-
/// canonical, clock, to which it can be converted.
85-
@available(StdlibDeploymentTarget 6.2, *)
86-
var canonicalClock: CanonicalClock? { get }
87-
88-
/// Convert an Instant to the canonical clock's equivalent Instant.
89-
///
90-
/// Parameters:
91-
///
92-
/// - instant: The `Instant` to convert.
93-
///
94-
/// Returns:
95-
///
96-
/// The equivalent `CanonicalClock.Instant`.
97-
func convertToCanonical(instant: Instant) -> CanonicalClock.Instant
98-
99-
/// Convert a Duration to the canonical clock's equivalent Duration.
100-
///
101-
/// Parameters:
102-
///
103-
/// - duration: The `Duration` to convert.
104-
///
105-
/// Returns:
106-
///
107-
/// The equivalent `CanonicalClock.Duration`.
108-
func convertToCanonical(duration: Duration) -> CanonicalClock.Duration
109-
110-
/// Convert an Instant to the canonical clock's equivalent Instant.
111-
///
112-
/// Parameters:
113-
///
114-
/// - instant: The `Instant` to convert, or `nil`.
115-
///
116-
/// Returns:
117-
///
118-
/// The equivalent `CanonicalClock.Instant`, or `nil` if `instant` was `nil`.
119-
func maybeConvertToCanonical(instant: Instant?) -> CanonicalClock.Instant?
120-
121-
/// Convert a Duration to the canonical clock's equivalent Duration.
122-
///
123-
/// Parameters:
124-
///
125-
/// - duration: The `Duration` to convert, or `nil`.
126-
///
127-
/// Returns:
128-
///
129-
/// The equivalent `CanonicalClock.Duration`, or `nil` if `duration` was `nil`.
130-
func maybeConvertToCanonical(duration: Duration?) -> CanonicalClock.Duration?
13178
}
13279

13380
extension Clock {
@@ -150,40 +97,6 @@ extension Clock {
15097
}
15198
}
15299

153-
// Default implementations for canonicalization support
154-
extension Clock where CanonicalClock == Self {
155-
public var canonicalClock: CanonicalClock? { return nil }
156-
157-
public func convertToCanonical(duration: Duration) -> CanonicalClock.Duration {
158-
return duration
159-
}
160-
161-
public func convertToCanonical(instant: Instant) -> CanonicalClock.Instant {
162-
return instant
163-
}
164-
}
165-
166-
// nil-propagating versions of convertToCanonical()
167-
extension Clock {
168-
public func maybeConvertToCanonical(duration: Duration?)
169-
-> CanonicalClock.Duration?
170-
{
171-
if let duration {
172-
return convertToCanonical(duration: duration)
173-
}
174-
return nil
175-
}
176-
177-
public func maybeConvertToCanonical(instant: Instant?)
178-
-> CanonicalClock.Instant?
179-
{
180-
if let instant {
181-
return convertToCanonical(instant: instant)
182-
}
183-
return nil
184-
}
185-
}
186-
187100
@available(StdlibDeploymentTarget 5.7, *)
188101
extension Clock {
189102
/// Measure the elapsed time to execute a closure.
@@ -258,6 +171,7 @@ extension Clock {
258171
enum _ClockID: Int32 {
259172
case continuous = 1
260173
case suspending = 2
174+
case walltime = 3
261175
}
262176

263177
@available(StdlibDeploymentTarget 5.7, *)

stdlib/public/Concurrency/CooperativeExecutor.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,6 @@ extension CooperativeExecutor: SchedulingExecutor {
202202
after delay: C.Duration,
203203
tolerance: C.Duration? = nil,
204204
clock: C) {
205-
// Convert the clock to its canonical equivalent, if any
206-
if let canonical = clock.canonicalClock {
207-
enqueueImpl(job,
208-
after: clock.convertToCanonical(duration: delay),
209-
tolerance: clock.maybeConvertToCanonical(duration: tolerance),
210-
clock: canonical)
211-
return
212-
}
213-
214-
enqueueImpl(job, after: delay, tolerance: tolerance, clock: clock)
215-
}
216-
217-
private func enqueueImpl<C: Clock>(_ job: consuming ExecutorJob,
218-
after delay: C.Duration,
219-
tolerance: C.Duration? = nil,
220-
clock: C) {
221205
// If it's a clock we know, get the duration to wait
222206
let duration: Duration
223207
if let _ = clock as? ContinuousClock {

stdlib/public/Concurrency/DispatchExecutor.swift

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public class DispatchGlobalTaskExecutor: TaskExecutor, SchedulingExecutor,
105105
enum DispatchClockID: CInt {
106106
case continuous = 1
107107
case suspending = 2
108+
case walltime = 3
108109
}
109110

110111
fileprivate func clamp(_ components: (seconds: Int64, attoseconds: Int64))
@@ -153,38 +154,6 @@ fileprivate func _dispatchEnqueue<C: Clock, E: Executor>(
153154
clock: C,
154155
executor: E,
155156
global: Bool
156-
) {
157-
// Convert the clock to its canonical equivalent, if any
158-
if let canonical = clock.canonicalClock {
159-
_dispatchEnqueueImpl(
160-
job,
161-
at: clock.convertToCanonical(instant: instant),
162-
tolerance: clock.maybeConvertToCanonical(duration: tolerance),
163-
clock: canonical,
164-
executor: executor,
165-
global: global
166-
)
167-
return
168-
}
169-
170-
_dispatchEnqueueImpl(
171-
job,
172-
at: instant,
173-
tolerance: tolerance,
174-
clock: clock,
175-
executor: executor,
176-
global: global
177-
)
178-
}
179-
180-
@available(StdlibDeploymentTarget 6.2, *)
181-
fileprivate func _dispatchEnqueueImpl<C: Clock, E: Executor>(
182-
_ job: consuming ExecutorJob,
183-
at instant: C.Instant,
184-
tolerance: C.Duration?,
185-
clock: C,
186-
executor: E,
187-
global: Bool
188157
) {
189158
// If it's a clock we know, convert it to something we can use; otherwise,
190159
// call the clock's `enqueue` function to schedule the enqueue of the job.

0 commit comments

Comments
 (0)