mirror of
https://github.com/apple/swift-crypto
synced 2026-11-27 04:54:21 +00:00
Rename _CryptoExtras Take 2 (#407)
Following on from #281, opened as a new PR as the conflicts were too many ### Checklist - [x] I've run tests to see all new and existing tests pass - [x] I've followed the code style of the rest of the project - [x] I've read the [Contribution Guidelines](CONTRIBUTING.md) - [x] I've updated the documentation if necessary #### If you've made changes to `gyb` files - [ ] I've run `.script/generate_boilerplate_files_with_gyb` and included updated generated files in a commit of this pull request ### Motivation: _[Explain here the context, and why you're making that change. What is the problem you're trying to solve.]_ ### Modifications: _[Describe the modifications you've done.]_ ### Result: _[After your change, what will change.]_
This commit is contained in:
1 parent
ea7f8fbce1
commit
4e662b89da
152 files changed
+244
-219
No files matched your search
@@ -0,0 +1,111 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
//
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the SwiftCrypto project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// Derived from swift-nio's ThreadSpecificVariable type.
|
||||
|
||||
/// A ``ThreadSpecificVariable`` is a variable that can be read and set like a normal variable except that it holds
|
||||
/// different variables per thread.
|
||||
///
|
||||
/// ``ThreadSpecificVariable`` is thread-safe so it can be used with multiple threads at the same time but the value
|
||||
/// returned by ``currentValue`` is defined per thread.
|
||||
///
|
||||
/// - Note: Though ``ThreadSpecificVariable`` is thread-safe, it is not `Sendable` unless `Value` is `Sendable`.
|
||||
/// If ``ThreadSpecificVariable`` were unconditionally `Sendable`, it could be used to "smuggle"
|
||||
/// non-`Sendable` state out of an actor or other isolation domain without triggering warnings. If you
|
||||
/// are attempting to use ``ThreadSpecificVariable`` with non-`Sendable` data, consider using a dynamic
|
||||
/// enforcement tool like `NIOLoopBoundBox` to police the access.
|
||||
final class ThreadSpecificVariable<Value: AnyObject> {
|
||||
// the actual type in there is `Box<(ThreadSpecificVariable<T>, T)>` but we can't use that as C functions can't capture (even types)
|
||||
private typealias BoxedType = Box<(AnyObject, AnyObject)>
|
||||
|
||||
private class Key {
|
||||
private var underlyingKey: ThreadOpsSystem.ThreadSpecificKey
|
||||
|
||||
internal init(destructor: @escaping ThreadOpsSystem.ThreadSpecificKeyDestructor) {
|
||||
self.underlyingKey = ThreadOpsSystem.allocateThreadSpecificValue(destructor: destructor)
|
||||
}
|
||||
|
||||
deinit {
|
||||
ThreadOpsSystem.deallocateThreadSpecificValue(self.underlyingKey)
|
||||
}
|
||||
|
||||
func get() -> UnsafeMutableRawPointer? {
|
||||
ThreadOpsSystem.getThreadSpecificValue(self.underlyingKey)
|
||||
}
|
||||
|
||||
func set(value: UnsafeMutableRawPointer?) {
|
||||
ThreadOpsSystem.setThreadSpecificValue(key: self.underlyingKey, value: value)
|
||||
}
|
||||
}
|
||||
|
||||
private let key: Key
|
||||
|
||||
/// Initialize a new `ThreadSpecificVariable` without a current value (`currentValue == nil`).
|
||||
init() {
|
||||
self.key = Key(destructor: {
|
||||
Unmanaged<BoxedType>.fromOpaque(($0 as UnsafeMutableRawPointer?)!).release()
|
||||
})
|
||||
}
|
||||
|
||||
/// Initialize a new `ThreadSpecificVariable` with `value` for the calling thread. After calling this, the calling
|
||||
/// thread will see `currentValue == value` but on all other threads `currentValue` will be `nil` until changed.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: The value to set for the calling thread.
|
||||
convenience init(value: Value) {
|
||||
self.init()
|
||||
self.currentValue = value
|
||||
}
|
||||
|
||||
/// The value for the current thread.
|
||||
@available(
|
||||
*,
|
||||
noasync,
|
||||
message: "threads can change between suspension points and therefore the thread specific value too"
|
||||
)
|
||||
var currentValue: Value? {
|
||||
get {
|
||||
self.get()
|
||||
}
|
||||
set {
|
||||
self.set(newValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current value for the calling thread.
|
||||
private func get() -> Value? {
|
||||
guard let raw = self.key.get() else { return nil }
|
||||
// parenthesize the return value to silence the cast warning
|
||||
return
|
||||
(Unmanaged<BoxedType>
|
||||
.fromOpaque(raw)
|
||||
.takeUnretainedValue()
|
||||
.value.1 as! Value)
|
||||
}
|
||||
|
||||
/// Set the current value for the calling threads. The `currentValue` for all other threads remains unchanged.
|
||||
private func set(_ newValue: Value?) {
|
||||
if let raw = self.key.get() {
|
||||
Unmanaged<BoxedType>.fromOpaque(raw).release()
|
||||
}
|
||||
self.key.set(value: newValue.map { Unmanaged.passRetained(Box((self, $0))).toOpaque() })
|
||||
}
|
||||
}
|
||||
|
||||
extension ThreadSpecificVariable: @unchecked Sendable where Value: Sendable {}
|
||||
|
||||
final class Box<T> {
|
||||
let value: T
|
||||
init(_ value: T) { self.value = value }
|
||||
}
|
||||
Reference in new issue
Block a user