Skip to content

Commit

Permalink
remove swift-atomics (#203)
Browse files Browse the repository at this point in the history
* remove swift-atomics

* fix test
  • Loading branch information
xlc authored Oct 28, 2024
1 parent 80a45d5 commit 61defc9
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 68 deletions.
11 changes: 1 addition & 10 deletions Blockchain/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Atomics
import Foundation
import TracingUtils
import Utils
Expand Down
13 changes: 2 additions & 11 deletions JAMTests/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions PolkaVM/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions Utils/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Utils/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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",
Expand Down
1 change: 0 additions & 1 deletion Utils/Sources/Utils/EventBus/EventBus.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Atomics
import Foundation
import TracingUtils

Expand Down
8 changes: 4 additions & 4 deletions Utils/Sources/Utils/Lazy.swift
Original file line number Diff line number Diff line change
@@ -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<T: AtomicReference> {
private let ref = ManagedAtomicLazyReference<T>()
public final class Lazy<T: AnyObject> {
private let ref = AtomicLazyReference<T>()
private let initFn: @Sendable () -> T

public init(_ initFn: @Sendable @escaping () -> T) {
Expand All @@ -12,7 +12,7 @@ public final class Lazy<T: AtomicReference> {

public var value: T {
guard let value = ref.load() else {
return ref.storeIfNilThenLoad(initFn())
return ref.storeIfNil(initFn())
}
return value
}
Expand Down
4 changes: 1 addition & 3 deletions Utils/Sources/Utils/Ref.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Atomics

open class Ref<T: Sendable>: @unchecked Sendable, AtomicReference, CustomStringConvertible {
open class Ref<T: Sendable>: @unchecked Sendable, CustomStringConvertible {
public let value: T

public required init(_ value: T) {
Expand Down
6 changes: 3 additions & 3 deletions Utils/Sources/Utils/UniqueId.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Atomics
import Synchronization
import TracingUtils

public struct UniqueId: Sendable {
private static let idGenerator: ManagedAtomic<Int> = ManagedAtomic(0)
private static let idGenerator: Atomic<Int> = .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
}

Expand Down
18 changes: 7 additions & 11 deletions Utils/Tests/UtilsTests/LazyTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 61defc9

Please sign in to comment.