Skip to content

Commit e7fc160

Browse files
authored
Add Clock.sleep(for:). (#61222)
1 parent 5e59444 commit e7fc160

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

stdlib/public/Concurrency/Clock.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ extension Clock {
7777
}
7878
}
7979

80+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
81+
@available(SwiftStdlib 5.7, *)
82+
extension Clock {
83+
/// Suspends for the given duration.
84+
///
85+
/// Prefer to use the `sleep(until:tolerance:)` method on `Clock` if you have
86+
/// access to an absolute instant.
87+
@available(SwiftStdlib 5.7, *)
88+
@_alwaysEmitIntoClient
89+
public func sleep(
90+
for duration: Instant.Duration,
91+
tolerance: Instant.Duration? = nil
92+
) async throws {
93+
try await sleep(until: now.advanced(by: duration), tolerance: tolerance)
94+
}
95+
}
96+
#endif
97+
8098
enum _ClockID: Int32 {
8199
case continuous = 1
82100
case suspending = 2

stdlib/public/Concurrency/TaskSleepDuration.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,18 @@ extension Task where Success == Never, Failure == Never {
133133
///
134134
/// This function doesn't block the underlying thread.
135135
///
136-
/// try await Task.sleep(until: .now + .seconds(3), clock: .continuous)
136+
/// try await Task.sleep(until: .now + .seconds(3))
137137
///
138138
@available(SwiftStdlib 5.7, *)
139139
public static func sleep<C: Clock>(
140140
until deadline: C.Instant,
141141
tolerance: C.Instant.Duration? = nil,
142-
clock: C
142+
clock: C = ContinuousClock()
143143
) async throws {
144144
try await clock.sleep(until: deadline, tolerance: tolerance)
145145
}
146146

147-
/// Suspends the current task for the given duration on a continuous clock.
147+
/// Suspends the current task for the given duration.
148148
///
149149
/// If the task is cancelled before the time ends, this function throws
150150
/// `CancellationError`.
@@ -153,11 +153,14 @@ extension Task where Success == Never, Failure == Never {
153153
///
154154
/// try await Task.sleep(for: .seconds(3))
155155
///
156-
/// - Parameter duration: The duration to wait.
157156
@available(SwiftStdlib 5.7, *)
158157
@_alwaysEmitIntoClient
159-
public static func sleep(for duration: Duration) async throws {
160-
try await sleep(until: .now + duration, clock: .continuous)
158+
public static func sleep<C: Clock>(
159+
for duration: C.Instant.Duration,
160+
tolerance: C.Instant.Duration? = nil,
161+
clock: C = ContinuousClock()
162+
) async throws {
163+
try await clock.sleep(for: duration, tolerance: tolerance)
161164
}
162165
}
163166
#else
@@ -169,13 +172,18 @@ extension Task where Success == Never, Failure == Never {
169172
public static func sleep<C: Clock>(
170173
until deadline: C.Instant,
171174
tolerance: C.Instant.Duration? = nil,
172-
clock: C
175+
clock: C = ContinuousClock()
173176
) async throws {
174177
fatalError("Unavailable in task-to-thread concurrency model")
175178
}
176179
@available(SwiftStdlib 5.7, *)
177180
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
178-
public static func sleep(for duration: Duration) async throws {
181+
@_alwaysEmitIntoClient
182+
public static func sleep<C: Clock>(
183+
for duration: C.Instant.Duration,
184+
tolerance: C.Instant.Duration? = nil,
185+
clock: C = ContinuousClock()
186+
) async throws {
179187
fatalError("Unavailable in task-to-thread concurrency model")
180188
}
181189
}

0 commit comments

Comments
 (0)