-
Notifications
You must be signed in to change notification settings - Fork 50
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
Adopt Mutex where available #70
Conversation
private final class _StorageWithMutex: _Storage, @unchecked Sendable { | ||
let mutex = Mutex<Void>(()) | ||
|
||
override func withLock<Result>(_ body: () throws -> Result) rethrows -> Result { |
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.
Does this actually compile without warnings under strict concurrency? The closure that Mutex
takes is sending
whereas ours is not. This should produce a warning/error.
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.
Needing dynamic dispatch is also a little gnarly.
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 tried .enableExperimentalFeature("StrictConcurrency")
and didn't get any errors, but added sending
in 948fe6c to be safe.
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 dynamic dispatch is still better than an extra allocation for the lock?
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.
Had to revert 948fe6c due to it breaking Swift 5.9/5.10
948fe6c
to
c82e407
Compare
@@ -99,7 +109,40 @@ public struct HTTPFields: Sendable, Hashable { | |||
} | |||
} | |||
|
|||
private var _storage = _Storage() | |||
#if canImport(Synchronization) | |||
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
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.
Not sure how to fix this on Swift 5.9/5.10.
/__w/swift-http-types/swift-http-types/Sources/HTTPTypes/HTTPFields.swift:113:63: error: unrecognized platform name 'visionOS'
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
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.
Fixed by switching to a compiler version check instead of a canImport check.
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 this is honestly probably fine, we just need to be aware that one day we'll want to remove this.
Mutex should eliminate the extra allocation. Subclassing is the only way I found to work. I tried enums (enum case cannot be unavailable) and class-bound protocols (
isKnownUniquelyReferenced
doesn't work). @FranzBusch