Skip to content

Commit

Permalink
Remove use of NIOAtomics in favor of Atomics (#120)
Browse files Browse the repository at this point in the history
remove use of NIOAtomic in favor of swift-atomics
  • Loading branch information
MahdiBM committed Aug 8, 2022
1 parent efb1bd5 commit 2d9d218
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", from: "2.33.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.14.0"),
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.11.4"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
],
targets: [
.target(name: "WebSocketKit", dependencies: [
Expand All @@ -26,6 +27,7 @@ let package = Package(
.product(name: "NIOSSL", package: "swift-nio-ssl"),
.product(name: "NIOWebSocket", package: "swift-nio"),
.product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
.product(name: "Atomics", package: "swift-atomics")
]),
.testTarget(name: "WebSocketKitTests", dependencies: [
.target(name: "WebSocketKit"),
Expand Down
11 changes: 8 additions & 3 deletions Sources/WebSocketKit/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NIOHTTP1
import NIOWebSocket
import NIOSSL
import NIOTransportServices
import Atomics

public final class WebSocketClient {
public enum Error: Swift.Error, LocalizedError {
Expand Down Expand Up @@ -37,7 +38,7 @@ public final class WebSocketClient {
let eventLoopGroupProvider: EventLoopGroupProvider
let group: EventLoopGroup
let configuration: Configuration
let isShutdown = NIOAtomic.makeAtomic(value: false)
let isShutdown = ManagedAtomic(false)

public init(eventLoopGroupProvider: EventLoopGroupProvider, configuration: Configuration = .init()) {
self.eventLoopGroupProvider = eventLoopGroupProvider
Expand Down Expand Up @@ -135,7 +136,11 @@ public final class WebSocketClient {
case .shared:
return
case .createNew:
if self.isShutdown.compareAndExchange(expected: false, desired: true) {
if self.isShutdown.compareExchange(
expected: false,
desired: true,
ordering: .relaxed
).exchanged {
try self.group.syncShutdownGracefully()
} else {
throw WebSocketClient.Error.alreadyShutdown
Expand All @@ -162,7 +167,7 @@ public final class WebSocketClient {
case .shared:
return
case .createNew:
assert(self.isShutdown.load(), "WebSocketClient not shutdown before deinit.")
assert(self.isShutdown.load(ordering: .relaxed), "WebSocketClient not shutdown before deinit.")
}
}
}

0 comments on commit 2d9d218

Please sign in to comment.