From 2e80db8aed964b521dd3571d3625dca59811b40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 14 Jun 2018 13:36:42 +0200 Subject: [PATCH] Database dispatch queues now have labels --- GRDB/Core/DatabasePool.swift | 8 ++++++-- GRDB/Core/DatabaseQueue.swift | 6 ++++-- GRDB/Core/DatabaseSnapshot.swift | 3 ++- GRDB/Core/SchedulingWatchdog.swift | 4 ++-- GRDB/Core/SerializedDatabase.swift | 4 ++-- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 21e908166..11c475be6 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -47,7 +47,8 @@ public final class DatabasePool: DatabaseWriter { writer = try SerializedDatabase( path: path, configuration: configuration, - schemaCache: SimpleDatabaseSchemaCache()) + schemaCache: SimpleDatabaseSchemaCache(), + label: "GRDB.DatabasePool.Writer") // Activate WAL Mode unless readonly if !configuration.readonly { @@ -77,11 +78,14 @@ public final class DatabasePool: DatabaseWriter { readerConfig.readonly = true readerConfig.defaultTransactionKind = .deferred // Make it the default for readers. Other transaction kinds are forbidden by SQLite in read-only connections. readerConfig.allowsUnsafeTransactions = false // Because there's no guarantee that one can get the same reader in order to close its opened transaction. + var readerCount = 0 readerPool = Pool(maximumCount: configuration.maximumReaderCount, makeElement: { [unowned self] in + readerCount += 1 // protected by pool's ReadWriteBox (undocumented behavior and protection) let reader = try SerializedDatabase( path: path, configuration: self.readerConfig, - schemaCache: SimpleDatabaseSchemaCache()) + schemaCache: SimpleDatabaseSchemaCache(), + label: "GRDB.DatabasePool.Reader.\(readerCount)") reader.sync { self.setupDatabase($0) } return reader }) diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 600ed95c5..3d7cc2c54 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -39,7 +39,8 @@ public final class DatabaseQueue: DatabaseWriter { writer = try SerializedDatabase( path: path, configuration: configuration, - schemaCache: SimpleDatabaseSchemaCache()) + schemaCache: SimpleDatabaseSchemaCache(), + label: "GRDB.DatabaseQueue") } /// Opens an in-memory SQLite database. @@ -54,7 +55,8 @@ public final class DatabaseQueue: DatabaseWriter { writer = try! SerializedDatabase( path: ":memory:", configuration: configuration, - schemaCache: SimpleDatabaseSchemaCache()) + schemaCache: SimpleDatabaseSchemaCache(), + label: "GRDB.DatabaseQueue") } #if os(iOS) diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 35bed7e2c..f82b4c069 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -20,7 +20,8 @@ public class DatabaseSnapshot : DatabaseReader { serializedDatabase = try SerializedDatabase( path: path, configuration: configuration, - schemaCache: SimpleDatabaseSchemaCache()) + schemaCache: SimpleDatabaseSchemaCache(), + label: "GRDB.DatabaseSnapshot") try serializedDatabase.sync { db in // Assert WAL mode diff --git a/GRDB/Core/SchedulingWatchdog.swift b/GRDB/Core/SchedulingWatchdog.swift index c954cfbf7..f7e83e13f 100644 --- a/GRDB/Core/SchedulingWatchdog.swift +++ b/GRDB/Core/SchedulingWatchdog.swift @@ -30,8 +30,8 @@ final class SchedulingWatchdog { allowedDatabases = [database] } - static func makeSerializedQueue(allowingDatabase database: Database) -> DispatchQueue { - let queue = DispatchQueue(label: "GRDB.SerializedDatabase") + static func makeSerializedQueue(allowingDatabase database: Database, label: String) -> DispatchQueue { + let queue = DispatchQueue(label: label) let watchdog = SchedulingWatchdog(allowedDatabase: database) queue.setSpecific(key: specificKey, value: watchdog) return queue diff --git a/GRDB/Core/SerializedDatabase.swift b/GRDB/Core/SerializedDatabase.swift index 9d9a44066..9efa3762f 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) throws { + init(path: String, configuration: Configuration = Configuration(), schemaCache: DatabaseSchemaCache, label: String) throws { // According to https://www.sqlite.org/threadsafe.html // // > SQLite support three different threading modes: @@ -38,7 +38,7 @@ final class SerializedDatabase { config.threadingMode = .multiThread db = try Database(path: path, configuration: config, schemaCache: schemaCache) - queue = SchedulingWatchdog.makeSerializedQueue(allowingDatabase: db) + queue = SchedulingWatchdog.makeSerializedQueue(allowingDatabase: db, label: label) self.path = path try queue.sync { try db.setup()