Skip to content

Commit

Permalink
Use more narrow locking
Browse files Browse the repository at this point in the history
  • Loading branch information
jtbergman committed Sep 11, 2024
1 parent aa1c32f commit bf5d6d3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Sources/Dependencies/DependencyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit bf5d6d3

Please sign in to comment.