Skip to content
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

5.9 sendable issues for the 1.x branch #1463

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions Sources/SwiftProtobuf/Google_Protobuf_Any+Registry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import Foundation
#if canImport(Dispatch)
import Dispatch
fileprivate var knownTypesQueue =
fileprivate let knownTypesQueue =
DispatchQueue(label: "org.swift.protobuf.typeRegistry",
attributes: .concurrent)
#endif
Expand Down Expand Up @@ -52,8 +52,22 @@ internal func typeName(fromURL s: String) -> String {
return String(s[typeStart..<s.endIndex])
}

// This is adapted from SwiftNIO so sendable checks don't flag issues with
// `knownTypes`. Another options would be something like NIO's `LockedValueBox`
// or moving the entire handling to a Task.
fileprivate final class UnsafeMutableTransferBox<Wrapped> {
var wrappedValue: Wrapped
init(_ wrappedValue: Wrapped) {
self.wrappedValue = wrappedValue
}
}

#if swift(>=5.5) && canImport(_Concurrency)
extension UnsafeMutableTransferBox: @unchecked Sendable {}
#endif

// All access to this should be done on `knownTypesQueue`.
fileprivate var knownTypes: [String:Message.Type] = [
fileprivate let knownTypes: UnsafeMutableTransferBox<[String:Message.Type]> = .init([
// Seeded with the Well Known Types.
"google.protobuf.Any": Google_Protobuf_Any.self,
"google.protobuf.BoolValue": Google_Protobuf_BoolValue.self,
Expand All @@ -72,7 +86,7 @@ fileprivate var knownTypes: [String:Message.Type] = [
"google.protobuf.UInt32Value": Google_Protobuf_UInt32Value.self,
"google.protobuf.UInt64Value": Google_Protobuf_UInt64Value.self,
"google.protobuf.Value": Google_Protobuf_Value.self,
]
])

extension Google_Protobuf_Any {

Expand Down Expand Up @@ -107,13 +121,13 @@ extension Google_Protobuf_Any {
let messageTypeName = messageType.protoMessageName
var result: Bool = false
execute(flags: .barrier) {
if let alreadyRegistered = knownTypes[messageTypeName] {
if let alreadyRegistered = knownTypes.wrappedValue[messageTypeName] {
// Success/failure when something was already registered is
// based on if they are registering the same class or trying
// to register a different type
result = alreadyRegistered == messageType
} else {
knownTypes[messageTypeName] = messageType
knownTypes.wrappedValue[messageTypeName] = messageType
result = true
}
}
Expand All @@ -131,7 +145,7 @@ extension Google_Protobuf_Any {
public static func messageType(forMessageName name: String) -> Message.Type? {
var result: Message.Type?
execute(flags: .none) {
result = knownTypes[name]
result = knownTypes.wrappedValue[name]
}
return result
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftProtobufTests/Test_AllTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,7 @@ class Test_AllTypes: XCTestCase, PBTestHelpers {
}

func testWithFactoryHelperRethrows() {
class TestWithFactoryHelperRethrows_Error : Error {}
struct TestWithFactoryHelperRethrows_Error : Error {}

let pNoThrow: (inout ProtobufUnittest_ForeignMessage) -> () = { $0.c = 1 }
let m1 = ProtobufUnittest_ForeignMessage.with(pNoThrow)
Expand Down