From 61defc9e3996cbf71e375ae961346bb04651760c Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Tue, 29 Oct 2024 12:45:51 +1300 Subject: [PATCH] remove swift-atomics (#203) * remove swift-atomics * fix test --- Blockchain/Package.resolved | 11 +---------- .../Blockchain/Validator/ServiceBase2.swift | 1 - JAMTests/Package.resolved | 13 ++----------- PolkaVM/Package.resolved | 13 ++----------- Utils/Package.resolved | 13 ++----------- Utils/Package.swift | 2 -- Utils/Sources/Utils/EventBus/EventBus.swift | 1 - Utils/Sources/Utils/Lazy.swift | 8 ++++---- Utils/Sources/Utils/Ref.swift | 4 +--- Utils/Sources/Utils/UniqueId.swift | 6 +++--- Utils/Tests/UtilsTests/LazyTests.swift | 18 +++++++----------- 11 files changed, 22 insertions(+), 68 deletions(-) diff --git a/Blockchain/Package.resolved b/Blockchain/Package.resolved index 7bbc69b7..dd4094c8 100644 --- a/Blockchain/Package.resolved +++ b/Blockchain/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "64fa76cb48bfb721e9426cf0d246c06245d80b6c98b16ca1a7c9ba00acfabb1b", + "originHash" : "72d6aa1fce2803c836be5d4b1c239fb67e531e7ed4619f96c34745064c756ba8", "pins" : [ { "identity" : "blake2.swift", @@ -10,15 +10,6 @@ "version" : "0.2.0" } }, - { - "identity" : "swift-atomics", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-atomics.git", - "state" : { - "revision" : "cd142fd2f64be2100422d658e7411e39489da985", - "version" : "1.2.0" - } - }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", diff --git a/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift b/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift index 1f8322c6..2fb0cdb9 100644 --- a/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift +++ b/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift @@ -1,4 +1,3 @@ -import Atomics import Foundation import TracingUtils import Utils diff --git a/JAMTests/Package.resolved b/JAMTests/Package.resolved index 784ad7cf..9520d3bc 100644 --- a/JAMTests/Package.resolved +++ b/JAMTests/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "cd9cc1e5d46c44090826877a95ecb2fbfa27868633e11d6438dda8ee66ea8452", + "originHash" : "b09552c8dbc0d6746d5c30f4bd3bce579055c1ec4333006ae939b3251732c315", "pins" : [ { "identity" : "blake2.swift", @@ -10,15 +10,6 @@ "version" : "0.2.0" } }, - { - "identity" : "swift-atomics", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-atomics.git", - "state" : { - "revision" : "cd142fd2f64be2100422d658e7411e39489da985", - "version" : "1.2.0" - } - }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -58,7 +49,7 @@ { "identity" : "swift-numerics", "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-numerics", + "location" : "https://github.com/apple/swift-numerics.git", "state" : { "branch" : "main", "revision" : "e30276bff2ff5ed80566fbdca49f50aa160b0e83" diff --git a/PolkaVM/Package.resolved b/PolkaVM/Package.resolved index 12ae8fdb..abeed02f 100644 --- a/PolkaVM/Package.resolved +++ b/PolkaVM/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "9e424d7bcd3f17dac238113a9cadb3e3629fd0d79c9ae55474a71e3340fff9e8", + "originHash" : "a91ad177156caefa2e488081e3e97d998b7265492cacff52c8348bf4ef81e168", "pins" : [ { "identity" : "blake2.swift", @@ -10,15 +10,6 @@ "version" : "0.2.0" } }, - { - "identity" : "swift-atomics", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-atomics.git", - "state" : { - "revision" : "cd142fd2f64be2100422d658e7411e39489da985", - "version" : "1.2.0" - } - }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -58,7 +49,7 @@ { "identity" : "swift-numerics", "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-numerics", + "location" : "https://github.com/apple/swift-numerics.git", "state" : { "branch" : "main", "revision" : "e30276bff2ff5ed80566fbdca49f50aa160b0e83" diff --git a/Utils/Package.resolved b/Utils/Package.resolved index 75638cbe..8c7162ca 100644 --- a/Utils/Package.resolved +++ b/Utils/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "d6c46e06c62a99990ec7081e211b89a840b494c327f7339c2580791428261b14", + "originHash" : "3af4fa15534a1d00e66459fc585003c2f7835023bebcf2a960f70e34d14438b2", "pins" : [ { "identity" : "blake2.swift", @@ -10,15 +10,6 @@ "version" : "0.2.0" } }, - { - "identity" : "swift-atomics", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-atomics.git", - "state" : { - "revision" : "cd142fd2f64be2100422d658e7411e39489da985", - "version" : "1.2.0" - } - }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -58,7 +49,7 @@ { "identity" : "swift-numerics", "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-numerics", + "location" : "https://github.com/apple/swift-numerics.git", "state" : { "branch" : "main", "revision" : "e30276bff2ff5ed80566fbdca49f50aa160b0e83" diff --git a/Utils/Package.swift b/Utils/Package.swift index 1b619277..17386e22 100644 --- a/Utils/Package.swift +++ b/Utils/Package.swift @@ -21,7 +21,6 @@ let package = Package( .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"), .package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "4.0.0"), .package(url: "https://github.com/apple/swift-testing.git", branch: "0.10.0"), - .package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0"), .package(url: "https://github.com/apple/swift-numerics.git", branch: "main"), ], targets: [ @@ -34,7 +33,6 @@ let package = Package( "TracingUtils", .product(name: "Blake2", package: "Blake2.swift"), .product(name: "Crypto", package: "swift-crypto"), - .product(name: "Atomics", package: "swift-atomics"), .product(name: "Numerics", package: "swift-numerics"), "bls", "bandersnatch_vrfs", diff --git a/Utils/Sources/Utils/EventBus/EventBus.swift b/Utils/Sources/Utils/EventBus/EventBus.swift index 614b4aa2..f5840cca 100644 --- a/Utils/Sources/Utils/EventBus/EventBus.swift +++ b/Utils/Sources/Utils/EventBus/EventBus.swift @@ -1,4 +1,3 @@ -import Atomics import Foundation import TracingUtils diff --git a/Utils/Sources/Utils/Lazy.swift b/Utils/Sources/Utils/Lazy.swift index a03d2c73..05435e15 100644 --- a/Utils/Sources/Utils/Lazy.swift +++ b/Utils/Sources/Utils/Lazy.swift @@ -1,9 +1,9 @@ -import Atomics +import Synchronization /// A thread-safe lazy value. /// Note: The initializer could be called multiple times. -public final class Lazy { - private let ref = ManagedAtomicLazyReference() +public final class Lazy { + private let ref = AtomicLazyReference() private let initFn: @Sendable () -> T public init(_ initFn: @Sendable @escaping () -> T) { @@ -12,7 +12,7 @@ public final class Lazy { public var value: T { guard let value = ref.load() else { - return ref.storeIfNilThenLoad(initFn()) + return ref.storeIfNil(initFn()) } return value } diff --git a/Utils/Sources/Utils/Ref.swift b/Utils/Sources/Utils/Ref.swift index 8427cc71..fc76755b 100644 --- a/Utils/Sources/Utils/Ref.swift +++ b/Utils/Sources/Utils/Ref.swift @@ -1,6 +1,4 @@ -import Atomics - -open class Ref: @unchecked Sendable, AtomicReference, CustomStringConvertible { +open class Ref: @unchecked Sendable, CustomStringConvertible { public let value: T public required init(_ value: T) { diff --git a/Utils/Sources/Utils/UniqueId.swift b/Utils/Sources/Utils/UniqueId.swift index 82e46d3e..2b58eb63 100644 --- a/Utils/Sources/Utils/UniqueId.swift +++ b/Utils/Sources/Utils/UniqueId.swift @@ -1,14 +1,14 @@ -import Atomics +import Synchronization import TracingUtils public struct UniqueId: Sendable { - private static let idGenerator: ManagedAtomic = ManagedAtomic(0) + private static let idGenerator: Atomic = .init(0) public let id: Int public let name: String public init(_ name: String = "") { - id = UniqueId.idGenerator.loadThenWrappingIncrement(ordering: .relaxed) + (_, id) = UniqueId.idGenerator.wrappingAdd(1, ordering: .relaxed) self.name = name } diff --git a/Utils/Tests/UtilsTests/LazyTests.swift b/Utils/Tests/UtilsTests/LazyTests.swift index e01ff46f..304b9589 100644 --- a/Utils/Tests/UtilsTests/LazyTests.swift +++ b/Utils/Tests/UtilsTests/LazyTests.swift @@ -1,19 +1,15 @@ -import Atomics import Testing @testable import Utils struct LazyTests { - @Test func lazyRef() throws { - let called = ManagedAtomic(false) - let lazy = Lazy { - let calledValue = called.load(ordering: .relaxed) - #expect(!calledValue) - called.store(true, ordering: .relaxed) - return Ref(42) + @Test func lazyRef() async throws { + await confirmation { confirm in + let lazy = Lazy { + confirm() + return Ref(42) + } + #expect(lazy.value.value == 42) } - #expect(lazy.value.value == 42) - let calledValue = called.load(ordering: .relaxed) - #expect(calledValue) } }