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

GraphIdentifier threadsafety optimization & patch interleaving race condition in Lazy resolve #523

Merged
merged 12 commits into from
Nov 2, 2022
Prev Previous commit
Next Next commit
id on Linux can be an uint value
maxim-chipeev committed Oct 17, 2022
commit 471eb594fafbc77c28393a23e20246d5e12d11e5
20 changes: 7 additions & 13 deletions Sources/GraphIdentifier.swift
Original file line number Diff line number Diff line change
@@ -2,24 +2,18 @@
// Copyright © 2019 Swinject Contributors. All rights reserved.
//

#if os(iOS) || os(macOS)
// no universal UUID impl on Linux

#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
/// Unique identifier of an object graph during a resolution process.
public struct GraphIdentifier: Identifiable {
public let id = UUID()
}

extension GraphIdentifier: Equatable, Hashable {}
#else
/// Unique identifier of an object graph during a resolution process.
public final class GraphIdentifier {}

extension GraphIdentifier: Equatable, Hashable {
public static func == (lhs: GraphIdentifier, rhs: GraphIdentifier) -> Bool {
return lhs === rhs
}

public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self).hashValue)
}
public struct GraphIdentifier: Identifiable {
public let id = UInt.random(in: 0..<UInt.max)
}
#endif

extension GraphIdentifier: Equatable, Hashable {}
2 changes: 0 additions & 2 deletions Tests/SwinjectTests/SynchronizedTests.swift
Original file line number Diff line number Diff line change
@@ -130,7 +130,6 @@ class SynchronizedResolverTests: XCTestCase {
}
}

#if os(iOS) || os(macOS)
func testSynchronizedResolverSafelyDereferencesLazyTypes() {
var graphs = Set<GraphIdentifier>()
let container = Container()
@@ -156,7 +155,6 @@ class SynchronizedResolverTests: XCTestCase {
}
}
}
#endif
}

private final class Counter {