Skip to content

Commit c0ae883

Browse files
committed
Use ThreadSafeBox
1 parent c5752fd commit c0ae883

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
@@ -126,7 +126,7 @@ public final class ThreadSafeArrayStore<Value> {
126126
}
127127
}
128128

129-
/// Thread-safe value boxing structure
129+
/// Thread-safe value boxing structure
130130
public final class ThreadSafeBox<Value> {
131131
private var underlying: Value?
132132
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"))
@@ -421,7 +420,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
421420
case .success(let collections):
422421
var matches = [(collection: Model.CollectionIdentifier, package: PackageIdentity, targetName: String)]()
423422
// Trie is more performant for target search; use it if available
424-
if self.targetTrieReadyLock.withLock({ self.targetTrieReady }) {
423+
if self.targetTrieReady.get() ?? false {
425424
do {
426425
switch type {
427426
case .exactMatch:
@@ -643,10 +642,10 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
643642
}
644643
}
645644
}
646-
self.targetTrieReadyLock.withLock { self.targetTrieReady = true }
645+
self.targetTrieReady.put(true)
647646
callback(.success(()))
648647
} catch {
649-
self.targetTrieReadyLock.withLock { self.targetTrieReady = false }
648+
self.targetTrieReady.put(false)
650649
callback(.failure(error))
651650
}
652651
}

0 commit comments

Comments
 (0)