Skip to content

Commit 2d9d218

Browse files
authored
Remove use of NIOAtomics in favor of Atomics (#120)
remove use of NIOAtomic in favor of swift-atomics
1 parent efb1bd5 commit 2d9d218

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let package = Package(
1515
.package(url: "https://github.com/apple/swift-nio.git", from: "2.33.0"),
1616
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.14.0"),
1717
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.11.4"),
18+
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
1819
],
1920
targets: [
2021
.target(name: "WebSocketKit", dependencies: [
@@ -26,6 +27,7 @@ let package = Package(
2627
.product(name: "NIOSSL", package: "swift-nio-ssl"),
2728
.product(name: "NIOWebSocket", package: "swift-nio"),
2829
.product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
30+
.product(name: "Atomics", package: "swift-atomics")
2931
]),
3032
.testTarget(name: "WebSocketKitTests", dependencies: [
3133
.target(name: "WebSocketKit"),

Sources/WebSocketKit/WebSocketClient.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import NIOHTTP1
55
import NIOWebSocket
66
import NIOSSL
77
import NIOTransportServices
8+
import Atomics
89

910
public final class WebSocketClient {
1011
public enum Error: Swift.Error, LocalizedError {
@@ -37,7 +38,7 @@ public final class WebSocketClient {
3738
let eventLoopGroupProvider: EventLoopGroupProvider
3839
let group: EventLoopGroup
3940
let configuration: Configuration
40-
let isShutdown = NIOAtomic.makeAtomic(value: false)
41+
let isShutdown = ManagedAtomic(false)
4142

4243
public init(eventLoopGroupProvider: EventLoopGroupProvider, configuration: Configuration = .init()) {
4344
self.eventLoopGroupProvider = eventLoopGroupProvider
@@ -135,7 +136,11 @@ public final class WebSocketClient {
135136
case .shared:
136137
return
137138
case .createNew:
138-
if self.isShutdown.compareAndExchange(expected: false, desired: true) {
139+
if self.isShutdown.compareExchange(
140+
expected: false,
141+
desired: true,
142+
ordering: .relaxed
143+
).exchanged {
139144
try self.group.syncShutdownGracefully()
140145
} else {
141146
throw WebSocketClient.Error.alreadyShutdown
@@ -162,7 +167,7 @@ public final class WebSocketClient {
162167
case .shared:
163168
return
164169
case .createNew:
165-
assert(self.isShutdown.load(), "WebSocketClient not shutdown before deinit.")
170+
assert(self.isShutdown.load(ordering: .relaxed), "WebSocketClient not shutdown before deinit.")
166171
}
167172
}
168173
}

0 commit comments

Comments
 (0)