From 54da27d48ff68a127e0a0ee8fb849db48b284657 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 6 Nov 2025 16:38:00 -0500 Subject: [PATCH] Use `AtomicLazyReference` to store the fallback event handler. I forgot we had this type! `AtomicLazyReference` is designed for exactly our use case. --- .../_TestingInterop/FallbackEventHandler.swift | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Sources/_TestingInterop/FallbackEventHandler.swift b/Sources/_TestingInterop/FallbackEventHandler.swift index bd4286315..9408bbdfb 100644 --- a/Sources/_TestingInterop/FallbackEventHandler.swift +++ b/Sources/_TestingInterop/FallbackEventHandler.swift @@ -36,7 +36,7 @@ private final class _FallbackEventHandlerStorage: Sendable, RawRepresentable { } /// The installed event handler. -private let _fallbackEventHandler = Atomic?>(nil) +private let _fallbackEventHandler = AtomicLazyReference<_FallbackEventHandlerStorage>() #endif /// A type describing a fallback event handler that testing API can invoke as an @@ -82,9 +82,7 @@ package func _swift_testing_getFallbackEventHandler() -> FallbackEventHandler? { // would need a full lock in order to avoid that problem. However, because we // instead have a one-time installation function, we can be sure that the // loaded value (if non-nil) will never be replaced with another value. - return _fallbackEventHandler.load(ordering: .sequentiallyConsistent).map { fallbackEventHandler in - fallbackEventHandler.takeUnretainedValue().rawValue - } + return _fallbackEventHandler.load()?.rawValue #endif } @@ -116,14 +114,9 @@ package func _swift_testing_installFallbackEventHandler(_ handler: FallbackEvent return true } #else - let handler = Unmanaged.passRetained(_FallbackEventHandlerStorage(rawValue: handler)) - defer { - if !result { - handler.release() - } - } - - result = _fallbackEventHandler.compareExchange(expected: nil, desired: handler, ordering: .sequentiallyConsistent).exchanged + let handler = _FallbackEventHandlerStorage(rawValue: handler) + let stored = _fallbackEventHandler.storeIfNil(handler) + result = (handler === stored) #endif return result