Skip to content

Commit

Permalink
Use swift-atomics instead of NIOAtomics (#350)
Browse files Browse the repository at this point in the history
`NIOAtomics` was deprecated in apple/swift-nio#2204 in favor of `swift-atomics` https://github.com/apple/swift-atomics
  • Loading branch information
dnadoba authored Jul 11, 2022
1 parent bd93cc0 commit f9ab1c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ let package = Package(
.library(name: "NIOHTTP2", targets: ["NIOHTTP2"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.35.0")
.package(url: "https://github.com/apple/swift-nio.git", from: "2.35.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
],
targets: [
.executableTarget(
Expand Down Expand Up @@ -49,6 +50,7 @@ let package = Package(
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOTLS", package: "swift-nio"),
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
.product(name: "Atomics", package: "swift-atomics"),
]),
.target(
name: "NIOHPACK",
Expand Down
23 changes: 12 additions & 11 deletions Sources/NIOHTTP2/HTTP2StreamChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//

import Atomics
import NIOCore
import NIOConcurrencyHelpers

Expand Down Expand Up @@ -164,8 +165,8 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
self.streamID = streamID
self.multiplexer = multiplexer
self.windowManager = InboundWindowManager(targetSize: Int32(targetWindowSize))
self._isActiveAtomic = .makeAtomic(value: false)
self._isWritable = .makeAtomic(value: true)
self._isActiveAtomic = .init(false)
self._isWritable = .init(true)
self.state = .idle
self.streamDataType = streamDataType
self.writabilityManager = StreamChannelFlowController(highWatermark: outboundBytesHighWatermark,
Expand Down Expand Up @@ -307,10 +308,10 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
}
self.modifyingState { $0.networkActive() }

if self.writabilityManager.isWritable != self._isWritable.load() {
if self.writabilityManager.isWritable != self._isWritable.load(ordering: .relaxed) {
// We have probably delayed telling the user that this channel isn't writable, but we should do
// it now.
self._isWritable.store(self.writabilityManager.isWritable)
self._isWritable.store(self.writabilityManager.isWritable, ordering: .relaxed)
self.pipeline.fireChannelWritabilityChanged()
}

Expand Down Expand Up @@ -404,20 +405,20 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
}

public var isWritable: Bool {
return self._isWritable.load()
return self._isWritable.load(ordering: .relaxed)
}

private let _isWritable: NIOAtomic<Bool>
private let _isWritable: ManagedAtomic<Bool>

private var _isActive: Bool {
return self.state == .active || self.state == .closing || self.state == .localActive
}

public var isActive: Bool {
return self._isActiveAtomic.load()
return self._isActiveAtomic.load(ordering: .relaxed)
}

private let _isActiveAtomic: NIOAtomic<Bool>
private let _isActiveAtomic: ManagedAtomic<Bool>

public var _channelCore: ChannelCore {
return self
Expand Down Expand Up @@ -663,7 +664,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
}

private func changeWritability(to newWritability: Bool) {
self._isWritable.store(newWritability)
self._isWritable.store(newWritability, ordering: .relaxed)
self.pipeline.fireChannelWritabilityChanged()
}

Expand Down Expand Up @@ -869,7 +870,7 @@ internal extension HTTP2StreamChannel {
// Do nothing here.
return
case .remoteActive, .active, .closing, .closingNeverActivated, .closed:
self._isWritable.store(localValue)
self._isWritable.store(localValue, ordering: .relaxed)
self.pipeline.fireChannelWritabilityChanged()
}
}
Expand All @@ -884,7 +885,7 @@ extension HTTP2StreamChannel {
// A helper function used to ensure that state modification leads to changes in the channel active atomic.
private func modifyingState<ReturnType>(_ closure: (inout StreamChannelState) throws -> ReturnType) rethrows -> ReturnType {
defer {
self._isActiveAtomic.store(self._isActive)
self._isActiveAtomic.store(self._isActive, ordering: .relaxed)
}
return try closure(&self.state)
}
Expand Down

0 comments on commit f9ab1c9

Please sign in to comment.