Skip to content

Commit

Permalink
address additional NIOLockedValueBox review comments (#2270)
Browse files Browse the repository at this point in the history
  • Loading branch information
weissi authored Sep 23, 2022
1 parent 4ed8e1e commit 5ce13ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct NIOLockedValueBox<Value> {
}

@usableFromInline
internal var _storage: _Storage
internal let _storage: _Storage

/// Initialize the `Value`.
public init(_ value: Value) {
Expand All @@ -45,9 +45,14 @@ public struct NIOLockedValueBox<Value> {

/// Access the `Value`, allowing mutation of it.
@inlinable
public func withNIOLockedValueBox<T>(_ mutate: (inout Value) throws -> T) rethrows -> T {
public func withLockedValue<T>(_ mutate: (inout Value) throws -> T) rethrows -> T {
return try self._storage._lock.withLock {
return try mutate(&self._storage._value)
}
}
}

#if compiler(>=5.5) && canImport(_Concurrency)
extension NIOLockedValueBox: Sendable where Value: Sendable {}
extension NIOLockedValueBox._Storage: @unchecked Sendable {}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -1016,13 +1016,13 @@ class NIOConcurrencyHelpersTests: XCTestCase {
let lv = NIOLockedValueBox<State>(State())
spawnAndJoinRacingThreads(count: 50) { _ in
for _ in 0..<1000 {
lv.withNIOLockedValueBox { state in
lv.withLockedValue { state in
state.count += 1
}
}
}

XCTAssertEqual(50_000, lv.withNIOLockedValueBox { $0.count })
XCTAssertEqual(50_000, lv.withLockedValue { $0.count })
}
}

Expand Down

0 comments on commit 5ce13ea

Please sign in to comment.