Skip to content

Commit 021ff08

Browse files
authored
Use AtomicLazyReference to store the fallback event handler. (#1401)
I forgot we had this type! `AtomicLazyReference` is designed for exactly our use case. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent ef3e4af commit 021ff08

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Sources/_TestingInterop/FallbackEventHandler.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private final class _FallbackEventHandlerStorage: Sendable, RawRepresentable {
3636
}
3737

3838
/// The installed event handler.
39-
private let _fallbackEventHandler = Atomic<Unmanaged<_FallbackEventHandlerStorage>?>(nil)
39+
private let _fallbackEventHandler = AtomicLazyReference<_FallbackEventHandlerStorage>()
4040
#endif
4141

4242
/// A type describing a fallback event handler that testing API can invoke as an
@@ -82,9 +82,7 @@ package func _swift_testing_getFallbackEventHandler() -> FallbackEventHandler? {
8282
// would need a full lock in order to avoid that problem. However, because we
8383
// instead have a one-time installation function, we can be sure that the
8484
// loaded value (if non-nil) will never be replaced with another value.
85-
return _fallbackEventHandler.load(ordering: .sequentiallyConsistent).map { fallbackEventHandler in
86-
fallbackEventHandler.takeUnretainedValue().rawValue
87-
}
85+
return _fallbackEventHandler.load()?.rawValue
8886
#endif
8987
}
9088

@@ -116,14 +114,9 @@ package func _swift_testing_installFallbackEventHandler(_ handler: FallbackEvent
116114
return true
117115
}
118116
#else
119-
let handler = Unmanaged.passRetained(_FallbackEventHandlerStorage(rawValue: handler))
120-
defer {
121-
if !result {
122-
handler.release()
123-
}
124-
}
125-
126-
result = _fallbackEventHandler.compareExchange(expected: nil, desired: handler, ordering: .sequentiallyConsistent).exchanged
117+
let handler = _FallbackEventHandlerStorage(rawValue: handler)
118+
let stored = _fallbackEventHandler.storeIfNil(handler)
119+
result = (handler === stored)
127120
#endif
128121

129122
return result

0 commit comments

Comments
 (0)