Skip to content

Adds sleep(for:) to clock #61222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions stdlib/public/Concurrency/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ extension Clock {
}
}

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@available(SwiftStdlib 5.7, *)
extension Clock {
/// Suspends for the given duration.
///
/// Prefer to use the `sleep(until:tolerance:)` method on `Clock` if you have
/// access to an absolute instant.
@available(SwiftStdlib 5.7, *)
@_alwaysEmitIntoClient
public func sleep(
for duration: Instant.Duration,
tolerance: Instant.Duration? = nil
) async throws {
try await sleep(until: now.advanced(by: duration), tolerance: tolerance)
}
}
#endif

enum _ClockID: Int32 {
case continuous = 1
case suspending = 2
Expand Down
24 changes: 16 additions & 8 deletions stdlib/public/Concurrency/TaskSleepDuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ extension Task where Success == Never, Failure == Never {
///
/// This function doesn't block the underlying thread.
///
/// try await Task.sleep(until: .now + .seconds(3), clock: .continuous)
/// try await Task.sleep(until: .now + .seconds(3))
///
@available(SwiftStdlib 5.7, *)
public static func sleep<C: Clock>(
until deadline: C.Instant,
tolerance: C.Instant.Duration? = nil,
clock: C
clock: C = ContinuousClock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here w.r.t. symbols in the library.

) async throws {
try await clock.sleep(until: deadline, tolerance: tolerance)
}

/// Suspends the current task for the given duration on a continuous clock.
/// Suspends the current task for the given duration.
///
/// If the task is cancelled before the time ends, this function throws
/// `CancellationError`.
Expand All @@ -153,11 +153,14 @@ extension Task where Success == Never, Failure == Never {
///
/// try await Task.sleep(for: .seconds(3))
///
/// - Parameter duration: The duration to wait.
@available(SwiftStdlib 5.7, *)
@_alwaysEmitIntoClient
public static func sleep(for duration: Duration) async throws {
try await sleep(until: .now + duration, clock: .continuous)
public static func sleep<C: Clock>(
for duration: C.Instant.Duration,
tolerance: C.Instant.Duration? = nil,
clock: C = ContinuousClock()
) async throws {
try await clock.sleep(for: duration, tolerance: tolerance)
}
}
#else
Expand All @@ -169,13 +172,18 @@ extension Task where Success == Never, Failure == Never {
public static func sleep<C: Clock>(
until deadline: C.Instant,
tolerance: C.Instant.Duration? = nil,
clock: C
clock: C = ContinuousClock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that adding a default argument is not actually possible here, because the symbol that provides the default will not be available in library versions that predate the appearance of the default. @slavapestov, is that right?

If so, you should instead add an @_aEIC overload that does not take a clock parameter and calls this function with clock: ContinuousClock()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misremembering, but I'd thought that it was the longstanding case that default arguments are emitted into the client and (thus) ABI stable to add?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, here we go: #56859 (comment)

) async throws {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.7, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public static func sleep(for duration: Duration) async throws {
@_alwaysEmitIntoClient
public static func sleep<C: Clock>(
for duration: C.Instant.Duration,
tolerance: C.Instant.Duration? = nil,
clock: C = ContinuousClock()
) async throws {
fatalError("Unavailable in task-to-thread concurrency model")
}
}
Expand Down