From f07eee3ca7fd95dbb8634bfcbaf6cd2afa1f167c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 1 Dec 2018 16:22:05 +0100 Subject: [PATCH] DispatchQueue creation cleanup --- GRDB/Core/Configuration.swift | 9 +++++++++ GRDB/Core/DatabasePool.swift | 9 ++++++--- GRDB/Core/DatabaseQueue.swift | 4 ++-- GRDB/Core/DatabaseSnapshot.swift | 5 +++-- GRDB/Core/SchedulingWatchdog.swift | 22 +++++----------------- GRDB/Core/SerializedDatabase.swift | 11 ++++------- GRDB/ValueObservation/ValueObserver.swift | 7 +------ 7 files changed, 30 insertions(+), 37 deletions(-) diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index cbd979143..fac576893 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -171,6 +171,15 @@ public struct Configuration { let readWriteFlags = readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE) return threadingMode.SQLiteOpenFlags | readWriteFlags } + + func makeDispatchQueue(defaultLabel: String, purpose: String? = nil) -> DispatchQueue { + let label = (self.label ?? defaultLabel) + (purpose.map { "." + $0 } ?? "") + if let targetQueue = targetQueue { + return DispatchQueue(label: label, target: targetQueue) + } else { + return DispatchQueue(label: label, qos: qos) + } + } } /// A tracing function that takes an SQL string. diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index d691cc5e0..621915f87 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -52,7 +52,8 @@ public final class DatabasePool: DatabaseWriter { path: path, configuration: configuration, schemaCache: SimpleDatabaseSchemaCache(), - label: (configuration.label ?? "GRDB.DatabasePool") + ".writer") + defaultLabel: "GRDB.DatabasePool", + purpose: "writer") // Activate WAL Mode unless readonly if !configuration.readonly { @@ -89,7 +90,8 @@ public final class DatabasePool: DatabaseWriter { path: path, configuration: self.readerConfig, schemaCache: SimpleDatabaseSchemaCache(), - label: (self.readerConfig.label ?? "GRDB.DatabasePool") + ".reader.\(readerCount)") + defaultLabel: "GRDB.DatabasePool", + purpose: "reader.\(readerCount)") reader.sync { self.setupDatabase($0) } return reader }) @@ -757,7 +759,8 @@ extension DatabasePool { let snapshot = try DatabaseSnapshot( path: path, configuration: writer.configuration, - labelSuffix: ".snapshot.\(snapshotCount.increment())") + defaultLabel: "GRDB.DatabasePool", + purpose: "snapshot.\(snapshotCount.increment())") snapshot.read { setupDatabase($0) } return snapshot } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 0814bb11c..60df0bfe8 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -40,7 +40,7 @@ public final class DatabaseQueue: DatabaseWriter { path: path, configuration: configuration, schemaCache: SimpleDatabaseSchemaCache(), - label: configuration.label ?? "GRDB.DatabaseQueue") + defaultLabel: "GRDB.DatabaseQueue") } /// Opens an in-memory SQLite database. @@ -56,7 +56,7 @@ public final class DatabaseQueue: DatabaseWriter { path: ":memory:", configuration: configuration, schemaCache: SimpleDatabaseSchemaCache(), - label: configuration.label ?? "GRDB.DatabaseQueue") + defaultLabel: "GRDB.DatabaseQueue") } #if os(iOS) diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 305738bb8..5ea57064c 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -14,7 +14,7 @@ public class DatabaseSnapshot : DatabaseReader { return serializedDatabase.configuration } - init(path: String, configuration: Configuration = Configuration(), labelSuffix: String) throws { + init(path: String, configuration: Configuration = Configuration(), defaultLabel: String, purpose: String) throws { var configuration = configuration configuration.readonly = true configuration.allowsUnsafeTransactions = true // Snaphost keeps a long-lived transaction @@ -23,7 +23,8 @@ public class DatabaseSnapshot : DatabaseReader { path: path, configuration: configuration, schemaCache: SimpleDatabaseSchemaCache(), - label: (configuration.label ?? "GRDB.DatabasePool") + labelSuffix) + defaultLabel: defaultLabel, + purpose: purpose) try serializedDatabase.sync { db in // Assert WAL mode diff --git a/GRDB/Core/SchedulingWatchdog.swift b/GRDB/Core/SchedulingWatchdog.swift index ca06d027b..165abfb05 100644 --- a/GRDB/Core/SchedulingWatchdog.swift +++ b/GRDB/Core/SchedulingWatchdog.swift @@ -22,7 +22,7 @@ import Dispatch /// - preconditionValidQueue() crashes whenever a database is used in an invalid /// dispatch queue. final class SchedulingWatchdog { - private static let specificKey = DispatchSpecificKey() + private static let watchDogKey = DispatchSpecificKey() private(set) var allowedDatabases: [Database] var databaseObservationBroker: DatabaseObservationBroker? @@ -30,22 +30,10 @@ final class SchedulingWatchdog { allowedDatabases = [database] } - static func makeSerializedQueue( - allowingDatabase database: Database, - label: String, - qos: DispatchQoS, - targetQueue: DispatchQueue?) - -> DispatchQueue - { - let queue: DispatchQueue - if let targetQueue = targetQueue { - queue = DispatchQueue(label: label, target: targetQueue) - } else { - queue = DispatchQueue(label: label, qos: qos) - } + static func allowDatabase(_ database: Database, onQueue queue: DispatchQueue) { + precondition(queue.getSpecific(key: watchDogKey) == nil) let watchdog = SchedulingWatchdog(allowedDatabase: database) - queue.setSpecific(key: specificKey, value: watchdog) - return queue + queue.setSpecific(key: watchDogKey, value: watchdog) } func inheritingAllowedDatabases(from other: SchedulingWatchdog, execute body: () throws -> T) rethrows -> T { @@ -60,7 +48,7 @@ final class SchedulingWatchdog { } static var current: SchedulingWatchdog? { - return DispatchQueue.getSpecific(key: specificKey) + return DispatchQueue.getSpecific(key: watchDogKey) } func allows(_ db: Database) -> Bool { diff --git a/GRDB/Core/SerializedDatabase.swift b/GRDB/Core/SerializedDatabase.swift index 730bb7d56..2a804e965 100644 --- a/GRDB/Core/SerializedDatabase.swift +++ b/GRDB/Core/SerializedDatabase.swift @@ -16,7 +16,7 @@ final class SerializedDatabase { /// The dispatch queue private let queue: DispatchQueue - init(path: String, configuration: Configuration = Configuration(), schemaCache: DatabaseSchemaCache, label: String) throws { + init(path: String, configuration: Configuration = Configuration(), schemaCache: DatabaseSchemaCache, defaultLabel: String, purpose: String? = nil) throws { // According to https://www.sqlite.org/threadsafe.html // // > SQLite support three different threading modes: @@ -37,13 +37,10 @@ final class SerializedDatabase { var config = configuration config.threadingMode = .multiThread - db = try Database(path: path, configuration: config, schemaCache: schemaCache) - queue = SchedulingWatchdog.makeSerializedQueue( - allowingDatabase: db, - label: label, - qos: configuration.qos, - targetQueue: configuration.targetQueue) self.path = path + self.db = try Database(path: path, configuration: config, schemaCache: schemaCache) + self.queue = configuration.makeDispatchQueue(defaultLabel: defaultLabel, purpose: purpose) + SchedulingWatchdog.allowDatabase(db, onQueue: queue) try queue.sync { try db.setup() } diff --git a/GRDB/ValueObservation/ValueObserver.swift b/GRDB/ValueObservation/ValueObserver.swift index e0392bd78..b261383a8 100644 --- a/GRDB/ValueObservation/ValueObserver.swift +++ b/GRDB/ValueObservation/ValueObserver.swift @@ -27,12 +27,7 @@ class ValueObserver: TransactionObserver { self.notificationQueue = notificationQueue self.onChange = onChange self.onError = onError - let reduceQueueLabel = (configuration.label ?? "GRDB") + ".ValueObservation.reducer" - if let targetQueue = configuration.targetQueue { - self.reduceQueue = DispatchQueue(label: reduceQueueLabel, target: targetQueue) - } else { - self.reduceQueue = DispatchQueue(label: reduceQueueLabel, qos: configuration.qos) - } + self.reduceQueue = configuration.makeDispatchQueue(defaultLabel: "GRDB", purpose: "ValueObservation.reducer") } func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool {