From ee92db9d6fb934bf4f967eaadcae09ee21b6313f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 25 May 2022 17:21:50 +0200 Subject: [PATCH] Pool has a QoS --- GRDB/Core/DatabasePool.swift | 19 +++++++++++-------- GRDB/Utils/Pool.swift | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 9af640d11..5e7a82332 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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 { diff --git a/GRDB/Utils/Pool.swift b/GRDB/Utils/Pool.swift index 7f63a388d..72c49df31 100644 --- a/GRDB/Utils/Pool.swift +++ b/GRDB/Utils/Pool.swift @@ -53,13 +53,24 @@ final class Pool { 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)