Skip to content

Add Task.sleep(for: Duration) #59203

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 7 commits into from
Jun 27, 2022
Merged
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
15 changes: 15 additions & 0 deletions stdlib/public/Concurrency/TaskSleepDuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,19 @@ extension Task where Success == Never, Failure == Never {
) async throws {
try await clock.sleep(until: deadline, tolerance: tolerance)
}

/// Suspends the current task for the given duration on a continuous clock.
///
/// If the task is cancelled before the time ends, this function throws
/// `CancellationError`.
///
/// This function doesn't block the underlying thread.
///
/// try await Task.sleep(for: .seconds(3))
///
/// - Parameter duration: The duration to wait.
@available(SwiftStdlib 5.7, *)
public static func sleep(for duration: Duration) async throws {
try await sleep(until: .now + duration, clock: .continuous)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not clear to me from the docs when one would reach for a suspending clock, so I went with .continuous, but I may just not understand their uses well enough.

Copy link
Contributor

@phausler phausler Jun 1, 2022

Choose a reason for hiding this comment

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

so suspending will mean that if the machine is asleep then the clock makes no forward progress so for example

sleep for 5 seconds
3 seconds later the machine is suspended
10 seconds later the machine is resumed
sleep continues for 2 seconds

for the continuous clock it would work as such:

sleep for 5 seconds
3 seconds later the machine is suspended
10 seconds later the machine is resumed
sleep resumes because 5 seconds has elapsed

Copy link
Contributor

Choose a reason for hiding this comment

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

my guess is that more often than not, this particular variant would be the latter (continuous as you had a intuition for). If developers want the alternate version then they can use the slightly more verbose one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so suspending will mean that if the machine is asleep then the clock makes no forward progress so for example

That was my understanding, too, I just can't think through when a suspending clock would be preferred :)

Copy link

Choose a reason for hiding this comment

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

@phausler You said Continuous in the review thread: https://forums.swift.org/t/se-0329-second-review-clock-instant-and-duration/54509/3 . Is it possible to clarify this in the text of SE-0329?

}
}