From f74aaa09a887f98fead442d059e49bf62b872f43 Mon Sep 17 00:00:00 2001 From: JT Bergman <61445278+jtbergman@users.noreply.github.com> Date: Wed, 11 Sep 2024 10:59:11 -0400 Subject: [PATCH] Use more narrow locking --- Sources/Dependencies/DependencyValues.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sources/Dependencies/DependencyValues.swift b/Sources/Dependencies/DependencyValues.swift index 5f46beec..bc80eb6e 100644 --- a/Sources/Dependencies/DependencyValues.swift +++ b/Sources/Dependencies/DependencyValues.swift @@ -385,12 +385,14 @@ public final class CachedValues: @unchecked Sendable { line: UInt = #line, column: UInt = #line ) -> Key.Value { - lock.lock() - defer { lock.unlock() } - return withIssueContext(fileID: fileID, filePath: filePath, line: line, column: column) { let cacheKey = CacheKey(id: TypeIdentifier(key), context: context) - guard let base = cached[cacheKey], let value = base as? Key.Value + + lock.lock() + let base = cached[cacheKey] + lock.unlock() + + guard let base, let value = base as? Key.Value else { let value: Key.Value? switch context { @@ -464,12 +466,16 @@ public final class CachedValues: @unchecked Sendable { #endif let value = Key.testValue if !DependencyValues.isSetting { + lock.lock() cached[cacheKey] = value + lock.unlock() } return value } + lock.lock() cached[cacheKey] = value + lock.unlock() return value }