Files
swift-certificates/Sources/X509/LockedValueBox.swift
T
David Nadoba 3c33085404 Support loading trust root CAs on Linux (#136)
* Support loading trust roots on Linux

* Fix linux

* run `swift-format`

* Apply review feedback

* Implement `_TinyArray2`

* fix tests

* Update benchmarks for _TinyArray2

* run swift-format

* use two properties instead of TinyArray2

* remove usage of `_TinyArray2` and rename `insert` to `append`

* remove TinyArray2 and rename TinyArray1 back to just TinyArray

* run swift-format

* fix tests

* Fix review comments
2023-10-18 14:01:10 +01:00

57 lines
1.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCertificates open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
final class LockedValueBox<Value> {
private let _lock: NSLock = .init()
private var _value: Value
var unsafeUnlockedValue: Value {
get { _value }
set { _value = newValue }
}
func lock() {
_lock.lock()
}
func unlock() {
_lock.unlock()
}
init(_ value: Value) {
self._value = value
}
func withLockedValue<Result>(
_ body: (inout Value) throws -> Result
) rethrows -> Result {
try _lock.withLock {
try body(&_value)
}
}
}
extension LockedValueBox: @unchecked Sendable where Value: Sendable {}
extension NSLock {
// this API doesn't exist on Linux and therefore we have a copy of it here
func withLock<Result>(_ body: () throws -> Result) rethrows -> Result {
self.lock()
defer { self.unlock() }
return try body()
}
}