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

Add Task.sleep(for: Duration) #59203

merged 7 commits into from
Jun 27, 2022

Conversation

stephencelis
Copy link
Contributor

This PR introduces missing functionality from SE-0329, namely the Task.sleep helper that takes a Duration.

I PRed against main but this should also be pulled into 5.7.

/cc @phausler

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?

Copy link
Contributor

@phausler phausler left a comment

Choose a reason for hiding this comment

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

Overall this looks correct to me

@phausler
Copy link
Contributor

phausler commented Jun 1, 2022

@swift-ci please smoke test

@MaxDesiatov
Copy link
Contributor

@swift-ci please smoke test

Copy link
Member

@amartini51 amartini51 left a comment

Choose a reason for hiding this comment

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

Thanks for adding this and for filling in documentation comments. The docs mostly look good, but left a couple minor changes.

stephencelis and others added 3 commits June 2, 2022 00:07
Co-authored-by: Alex Martini <amartini@apple.com>
Co-authored-by: Alex Martini <amartini@apple.com>
@amartini51
Copy link
Member

@swift-ci Please smoke test.

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
@ktoso
Copy link
Contributor

ktoso commented Jun 8, 2022

@swift-ci Please smoke test

@stephencelis
Copy link
Contributor Author

Is anything in particular blocking this? Or did it get lost in the WWDC shuffle? 😅

@xwu
Copy link
Collaborator

xwu commented Jun 27, 2022

Seems everyone approved and all that's waiting is for it to be merged...

@phausler phausler merged commit 8be672a into swiftlang:main Jun 27, 2022
@stephencelis stephencelis deleted the patch-6 branch June 27, 2022 17:04
hborla pushed a commit to hborla/swift that referenced this pull request Jun 28, 2022
* Add Task.sleep(for: Duration)

* Update TaskSleepDuration.swift

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Alex Martini <amartini@apple.com>

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Alex Martini <amartini@apple.com>

* Fix suggestion.

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Alex Martini <amartini@apple.com>
Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants