Pool has a QoS

This commit is contained in:
Gwendal Roué committed 2022-05-25 17:21:50 +02:00
1 parent de4266499c
commit ee92db9d6f
2 files changed
+25 -11

No files matched your search

+11 -8
View File
@@ -57,14 +57,17 @@ public final class DatabasePool: DatabaseWriter {
readerConfiguration.allowsUnsafeTransactions = false
var readerCount = 0
readerPool = Pool(maximumCount: configuration.maximumReaderCount, makeElement: {
readerCount += 1 // protected by Pool (TODO: document this protection behavior)
return try SerializedDatabase(
path: path,
configuration: readerConfiguration,
defaultLabel: "GRDB.DatabasePool",
purpose: "reader.\(readerCount)")
})
readerPool = Pool(
maximumCount: configuration.maximumReaderCount,
qos: configuration.readQoS,
makeElement: {
readerCount += 1 // protected by Pool (TODO: document this protection behavior)
return try SerializedDatabase(
path: path,
configuration: readerConfiguration,
defaultLabel: "GRDB.DatabasePool",
purpose: "reader.\(readerCount)")
})
// Activate WAL Mode unless readonly
if !configuration.readonly {
+14 -3
View File
@@ -53,13 +53,24 @@ final class Pool<T> {
private let barrierQueue: DispatchQueue
private let semaphoreWaitingQueue: DispatchQueue // Inspired by https://khanlou.com/2016/04/the-GCD-handbook/
init(maximumCount: Int, makeElement: @escaping () throws -> T) {
/// Creates a Pool.
///
/// - parameters:
/// - maximumCount: The maximum number of elements.
/// - qos: The quality of service of asynchronous accesses.
/// - makeElement: A function that creates an element. It is called
/// on demand.
init(
maximumCount: Int,
qos: DispatchQoS = .unspecified,
makeElement: @escaping () throws -> T)
{
GRDBPrecondition(maximumCount > 0, "Pool size must be at least 1")
self.makeElement = makeElement
self.itemsSemaphore = DispatchSemaphore(value: maximumCount)
self.itemsGroup = DispatchGroup()
self.barrierQueue = DispatchQueue(label: "GRDB.Pool.barrier", attributes: [.concurrent])
self.semaphoreWaitingQueue = DispatchQueue(label: "GRDB.Pool.wait")
self.barrierQueue = DispatchQueue(label: "GRDB.Pool.barrier", qos: qos, attributes: [.concurrent])
self.semaphoreWaitingQueue = DispatchQueue(label: "GRDB.Pool.wait", qos: qos)
}
/// Returns a tuple (element, release)