From e650ac1279c22d9a99fbe0b7b101ee4e82bb0a73 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 30 Jun 2022 11:11:42 +0200 Subject: [PATCH] Adopt `noasync` for `ThreadSpecificVariable` (#2206) --- Sources/NIOPosix/Thread.swift | 47 +++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/Sources/NIOPosix/Thread.swift b/Sources/NIOPosix/Thread.swift index a8953b396..4e0cf4eea 100644 --- a/Sources/NIOPosix/Thread.swift +++ b/Sources/NIOPosix/Thread.swift @@ -185,26 +185,47 @@ public final class ThreadSpecificVariable { 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 - .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.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 + .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.fromOpaque(raw).release() + } + self.key.set(value: newValue.map { Unmanaged.passRetained(Box((self, $0))).toOpaque() }) + } } extension NIOThread: Equatable {