Skip to content

Commit 0500947

Browse files
committed
Use ThreadSafeBox
1 parent 2af9979 commit 0500947

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

Sources/Basics/ConcurrencyHelpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final class ThreadSafeKeyValueStore<Key, Value> where Key: Hashable {
6767
}
6868
}
6969

70-
/// Thread-safe value boxing structure
70+
/// Thread-safe value boxing structure
7171
public final class ThreadSafeBox<Value> {
7272
private var underlying: Value?
7373
private let lock = Lock()

Sources/PackageCollections/Storage/SQLitePackageCollectionsStorage.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
4444
private let ftsLock = Lock()
4545

4646
private let targetTrie = Trie<CollectionPackage>()
47-
private var targetTrieReady = false
48-
private let targetTrieReadyLock = Lock()
47+
private var targetTrieReady = ThreadSafeBox<Bool>(false)
4948

5049
init(location: SQLite.Location? = nil, diagnosticsEngine: DiagnosticsEngine? = nil) {
5150
self.location = location ?? .path(localFileSystem.swiftPMCacheDirectory.appending(components: "package-collection.db"))
@@ -433,7 +432,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
433432
case .success(let collections):
434433
var matches = [(collection: Model.CollectionIdentifier, package: PackageIdentity, targetName: String)]()
435434
// Trie is more performant for target search; use it if available
436-
if self.targetTrieReadyLock.withLock({ self.targetTrieReady }) {
435+
if self.targetTrieReady.get() ?? false {
437436
do {
438437
switch type {
439438
case .exactMatch:
@@ -657,10 +656,10 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
657656
}
658657
}
659658
}
660-
self.targetTrieReadyLock.withLock { self.targetTrieReady = true }
659+
self.targetTrieReady.put(true)
661660
callback(.success(()))
662661
} catch {
663-
self.targetTrieReadyLock.withLock { self.targetTrieReady = false }
662+
self.targetTrieReady.put(false)
664663
callback(.failure(error))
665664
}
666665
}

0 commit comments

Comments
 (0)