-
-
Notifications
You must be signed in to change notification settings - Fork 12
Rename AsyncQueue -> FIFOQueue. Create ActorQueue #3
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
Conversation
|
||
// MARK: - Semaphore | ||
|
||
private actor Semaphore { |
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.
This Semaphore
implementation was moved to its own file without changes beyond its accessibility level.
|
||
// MARK: - Counter | ||
|
||
private actor Counter { |
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.
This Counter
implementation was moved to its own file without changes beyond its accessibility level.
dd1819e
to
0315eca
Compare
Codecov Report
@@ Coverage Diff @@
## main #3 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 2 +1
Lines 39 95 +56
=========================================
+ Hits 39 95 +56
|
efc1dd3
to
2957e19
Compare
54db140
to
bd3a337
Compare
a458835
to
ec8838a
Compare
3be98e5
to
dfd4e80
Compare
dfd4e80
to
67910a7
Compare
Force push was rebasing |
|
||
@testable import AsyncQueue | ||
|
||
final class SemaphoreTests: XCTestCase { |
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.
These tests are no longer required as part of this PR since Semaphore
is no longer living in the Sources
directory. I'll remove them shortly and put up a separate PR to add them. They're explicitly a nice-to-have now, though I think it's helpful to have examples of async tests like the below.
Apologies for the multiple rounds of revisions here. @bachand @Gabardone this PR is once again ready for another review! |
|
||
@testable import AsyncQueue | ||
|
||
final class ActorQueueTests: XCTestCase { |
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.
When reviewing this file, it's worth comparing these tests to the ones in FIFOQueueTests
…on context" This reverts commit 139f874.
…n ActorQueueTests to use an ActorQueue
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.
Nice work @dfed !
README.md
Outdated
|
||
```swift | ||
let asyncQueue = AsyncQueue() | ||
``` |
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.
Very useful. Thanks for including this.
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.
Thanks for the prompt to do so!
/* | ||
`async` context that can return a value or throw an error. | ||
Executes after all other enqueued work has begun executing. | ||
Work enqueued after this task will wait for this task to complete or suspend. |
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.
👍
Sources/AsyncQueue/ActorQueue.swift
Outdated
/// | ||
/// An `ActorQueue` is used to ensure tasks sent from a nonisolated context to a single `actor`'s isolated context begin execution in order. | ||
/// Here is an example of how an `ActorQueue` should be utilized within an `actor`: | ||
/// ``` |
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.
/// ``` | |
/// ```swift |
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.
Good catch! bf3a0c2
/// The scheduled task will not execute until all prior tasks have completed or suspended. | ||
/// - Parameter task: The task to enqueue. | ||
/// - Returns: The value returned from the enqueued task. | ||
public func await<T>(_ task: @escaping @Sendable () async throws -> T) async throws -> T { |
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.
If this function is accepts a throwing task should the above one accept a throwing task too?
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.
Ideally we'd use rethrows
here so we only need to declare one method, however I couldn't get rethrows
to work here: there's no way today (that I know of) to signal to the compiler that we're only going to throw
within withUnsafeThrowingContinuation
if and only if the task
throws
. So I created one method that can throw
and one that can't.
// If the actor queue were FIFO, this test would hang since this code would never execute: | ||
// we'd still be waiting for the prior `wait()` tasks to finish. |
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.
👍
I realized that our
AsyncQueue
is really aFIFOQueue
, and that consumers may desire a queue which maintains task execution order when interacting with anactor
but allows for reentrancy and re-ordering of tasks due to suspension.This realization led to the creation of an
ActorQueue
. This queue is purpose-built to send ordered, asynchronous work toactor
instances from a non-isolated context/Reviewers should feel free to question the type and method names within this PR. I could use another brain here.