-
Notifications
You must be signed in to change notification settings - Fork 316
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
Sendable
support
#1795
Sendable
support
#1795
Changes from all commits
3dbf5eb
18bfc99
ff0b7cb
be3b3c1
b03899a
347d2e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,27 +16,52 @@ import Foundation | |
|
||
class OperationDispatcher { | ||
|
||
private let mainQueue = DispatchQueue.main | ||
private let workerQueue = DispatchQueue(label: "OperationDispatcherWorkerQueue") | ||
private let mainQueue: DispatchQueue = .main | ||
private let workerQueue: DispatchQueue = .init(label: "OperationDispatcherWorkerQueue") | ||
private let maxJitterInSeconds: Double = 5 | ||
|
||
static let `default`: OperationDispatcher = .init() | ||
|
||
func dispatchOnMainThread(_ block: @escaping () -> Void) { | ||
func dispatchOnMainThread(_ block: @escaping @Sendable () -> Void) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be made private now? Is there any scenario where we should choose to go with main thread and not main actor even when Swift Concurrency is available? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually we can get rid of this, but that seems like a bigger change. For existing code that does not use the new Swift "structured" concurrency, we might not want to change the semantics if we don't need to. There's still a few places using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know what you think, we can migrate more of these to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
if Thread.isMainThread { | ||
block() | ||
} else { | ||
mainQueue.async(execute: block) | ||
self.mainQueue.async(execute: block) | ||
} | ||
} | ||
|
||
func dispatchOnWorkerThread(withRandomDelay: Bool = false, block: @escaping () -> Void) { | ||
func dispatchOnMainActor(_ block: @MainActor @escaping @Sendable () -> Void) { | ||
Self.dispatchOnMainActor(block) | ||
} | ||
|
||
func dispatchOnWorkerThread(withRandomDelay: Bool = false, block: @escaping @Sendable () -> Void) { | ||
if withRandomDelay { | ||
let delay = Double.random(in: 0..<maxJitterInSeconds) | ||
workerQueue.asyncAfter(deadline: .now() + delay, execute: block) | ||
let delay = Double.random(in: 0..<self.maxJitterInSeconds) | ||
self.workerQueue.asyncAfter(deadline: .now() + delay, execute: block) | ||
} else { | ||
self.workerQueue.async(execute: block) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension OperationDispatcher { | ||
|
||
static func dispatchOnMainActor(_ block: @MainActor @escaping @Sendable () -> Void) { | ||
if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { | ||
Comment on lines
+48
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is cool |
||
_ = Task { | ||
await MainActor.run { | ||
block() | ||
} | ||
} | ||
} else { | ||
workerQueue.async(execute: block) | ||
DispatchQueue.main.async(execute: block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah good call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohhh right, that makes sense. Could you add a comment for the future? |
||
} | ||
} | ||
|
||
} | ||
|
||
#if swift(<5.8) | ||
// `DispatchQueue` is not `Sendable` as of Swift 5.7, but this class performs no mutations. | ||
extension OperationDispatcher: @unchecked Sendable {} | ||
#endif |
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.
We'll see if we keep this or not.
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 think it's not a bad idea to keep this so we ensure we maintain this level of correctness prior to Swift 6.0