mirror of
https://github.com/apple/swift-nio
synced 2026-11-27 05:04:22 +00:00
address additional NIOLockedValueBox review comments (#2270)
This commit is contained in:
1 parent
4ed8e1e228
commit
5ce13ea045
2 files changed
+9
-4
No files matched your search
+7
-2
@@ -36,7 +36,7 @@ public struct NIOLockedValueBox<Value> {
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal var _storage: _Storage
|
||||
internal let _storage: _Storage
|
||||
|
||||
/// Initialize the `Value`.
|
||||
public init(_ value: Value) {
|
||||
@@ -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
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user