Adopt noasync for ThreadSpecificVariable (#2206)

This commit is contained in:
David Nadoba authored and GitHub committed 2022-06-30 11:11:42 +02:00
1 parent e2c7fa4d4b
commit e650ac1279
1 file changed
+34 -13
+34 -13
View File
@@ -185,26 +185,47 @@ public final class ThreadSpecificVariable<Value: AnyObject> {
self.currentValue = value
}
#if swift(>=5.7)
/// The value for the current thread.
@available(*, noasync, message: "threads can change between suspension points and therefore the thread specific value too")
public var currentValue: Value? {
get {
self.get()
}
set {
self.set(newValue)
}
}
#else
/// The value for the current thread.
public var currentValue: Value? {
/// Get the current value for the calling thread.
get {
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)
self.get()
}
/// Set the current value for the calling threads. The `currentValue` for all other threads remains unchanged.
set {
if let raw = self.key.get() {
Unmanaged<BoxedType>.fromOpaque(raw).release()
}
self.key.set(value: newValue.map { Unmanaged.passRetained(Box((self, $0))).toOpaque() })
self.set(newValue)
}
}
#endif
/// Get the current value for the calling thread.
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.
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 NIOThread: Equatable {