-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Concurrency] Add asynchronous Task.sleep function #35983
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af4b6bc
[Concurrency] Add asynchronous Task.sleep function
drexin 28ce3d9
Use async sleep in concurrency tests
drexin 14ab1a2
Cleanup after rebase
drexin 4bb52cc
Workaround for rdar://74957357
drexin 54e78cc
Fix after rebase
drexin 54c7c76
Require libdispatch in async_task_sleep.swift
drexin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library) | %FileCheck %s --dump-input always | ||
// REQUIRES: executable_test | ||
// REQUIRES: concurrency | ||
// REQUIRES: libdispatch | ||
|
||
import _Concurrency | ||
// FIXME: should not depend on Dispatch | ||
import Dispatch | ||
|
||
@main struct Main { | ||
static let pause = 500_000_000 // 500ms | ||
|
||
static func main() async { | ||
await testSleepDuration() | ||
await testSleepDoesNotBlock() | ||
} | ||
|
||
static func testSleepDuration() async { | ||
let start = DispatchTime.now() | ||
|
||
await Task.sleep(UInt64(pause)) | ||
|
||
let stop = DispatchTime.now() | ||
|
||
// assert that at least the specified time passed since calling `sleep` | ||
assert(stop >= (start + .nanoseconds(pause))) | ||
} | ||
|
||
static func testSleepDoesNotBlock() async { | ||
// FIXME: Should run on main executor | ||
let task = Task.runDetached { | ||
print("Run first") | ||
drexin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
await Task.sleep(UInt64(pause)) | ||
|
||
print("Run second") | ||
|
||
// CHECK: Run first | ||
// CHECK: Run second | ||
await task.get() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I wonder if implementing this without a detached implicitly created task might be better?
It'd need much more boilerplate to introduce a new "awaitable" function like I did for
swift_task_group_wait_next
for groups... It'd save some allocations (the task allocates, and if we wanted to make this task a child that's another allocation and hitting a lock in the parent too).What do @DougGregor @jckarter think?