mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Merge branch 'master' of github.com:groue/GRDB.swift into Swift3
Conflicts: GRDB/Core/DatabaseScheduler.swift GRDB/Core/SerializedDatabase.swift
This commit is contained in:
commit
1cd9c8f817
2 files changed
+90
-74
No files matched your search
@@ -7,9 +7,7 @@
|
||||
/// https://github.com/groue/GRDB.swift/issues/55. To support this use case, a
|
||||
/// single dispatch queue can be temporarily shared by two or more connections.
|
||||
///
|
||||
/// Managing this queue sharing is the job of the DatabaseScheduler class.
|
||||
///
|
||||
/// Three entry points:
|
||||
/// Two entry points:
|
||||
///
|
||||
/// - DatabaseScheduler.makeSerializedQueueAllowing(database:) creates a
|
||||
/// dispatch queue that allows one database.
|
||||
@@ -17,98 +15,39 @@
|
||||
/// It does so by registering one instance of DatabaseScheduler as a specific
|
||||
/// of the dispatch queue, a DatabaseScheduler that allows that database only.
|
||||
///
|
||||
/// - The dispatchSync() function helps using several databases in the same
|
||||
/// dispatch queue. It does so by temporarily extending the allowed databases
|
||||
/// in the dispatch queue when it is called from a dispatch queue that already
|
||||
/// allows some databases.
|
||||
/// Later on, the queue can be shared by several databases by mutating the
|
||||
/// allowedDatabases property. See SerializedDatabase.sync()
|
||||
///
|
||||
/// - preconditionValidQueue() crashes whenever a database is used in an invalid
|
||||
/// dispatch queue.
|
||||
final class DatabaseScheduler {
|
||||
private static let specificKey = DispatchSpecificKey<DatabaseScheduler>()
|
||||
private var allowedSerializedDatabases: [Database]
|
||||
var allowedDatabases: [Database]
|
||||
|
||||
private init(allowedSerializedDatabase database: Database) {
|
||||
allowedSerializedDatabases = [database]
|
||||
private init(allowedDatabase database: Database) {
|
||||
allowedDatabases = [database]
|
||||
}
|
||||
|
||||
static func makeSerializedQueueAllowing(database: Database) -> DispatchQueue {
|
||||
let queue = DispatchQueue(label: "GRDB.SerializedDatabase")
|
||||
let scheduler = DatabaseScheduler(allowedSerializedDatabase: database)
|
||||
let scheduler = DatabaseScheduler(allowedDatabase: database)
|
||||
queue.setSpecific(key: specificKey, value: scheduler)
|
||||
return queue
|
||||
}
|
||||
|
||||
static func sync<T>(queue: DispatchQueue, database: Database, block: @noescape (db: Database) throws -> T) rethrows -> T {
|
||||
if let sourceScheduler = currentScheduler() {
|
||||
// We're in a queue where some databases are allowed.
|
||||
//
|
||||
// First things first: forbid reentrancy.
|
||||
//
|
||||
// Reentrancy looks nice at first sight:
|
||||
//
|
||||
// dbQueue.inDatabase { db in
|
||||
// dbQueue.inDatabase { db in
|
||||
// // Look, ma! I'm reentrant!
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// But it does not survive this code, which deadlocks:
|
||||
//
|
||||
// let queue = dispatch_queue_create("...", nil)
|
||||
// dbQueue.inDatabase { db in
|
||||
// queue.sync {
|
||||
// dbQueue.inDatabase { db in
|
||||
// // Never run
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// I try not to ship half-baked solutions, so until a robust
|
||||
// solution is found to this problem, I prefer discouraging
|
||||
// reentrancy altogether, hoping that users will learn and avoid
|
||||
// the deadlock pattern.
|
||||
GRDBPrecondition(!sourceScheduler.allows(database), "Database methods are not reentrant.")
|
||||
|
||||
// Now let's enter the new queue, and temporarily allow the
|
||||
// currently allowed databases inside.
|
||||
return try queue.sync {
|
||||
let targetScheduler = currentScheduler()!
|
||||
assert(targetScheduler.allowedSerializedDatabases[0] === database) // sanity check
|
||||
|
||||
let backup = targetScheduler.allowedSerializedDatabases
|
||||
targetScheduler.allowedSerializedDatabases.append(contentsOf: sourceScheduler.allowedSerializedDatabases)
|
||||
defer {
|
||||
targetScheduler.allowedSerializedDatabases = backup
|
||||
}
|
||||
return try block(db: database)
|
||||
}
|
||||
|
||||
} else {
|
||||
// We're in a queue where no database is allowed: just dispatch
|
||||
// block to queue.
|
||||
return try queue.sync {
|
||||
try block(db: database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static func preconditionValidQueue(_ db: Database, _ message: @autoclosure() -> String = "Database was not used on the correct thread.", file: StaticString = #file, line: UInt = #line) {
|
||||
GRDBPrecondition(allows(db), message, file: file, line: line)
|
||||
}
|
||||
|
||||
static func allows(_ db: Database) -> Bool {
|
||||
return currentScheduler()?.allows(db) ?? false
|
||||
return current?.allows(db) ?? false
|
||||
}
|
||||
|
||||
private func allows(_ db: Database) -> Bool {
|
||||
return allowedSerializedDatabases.contains { $0 === db }
|
||||
func allows(_ db: Database) -> Bool {
|
||||
return allowedDatabases.contains { $0 === db }
|
||||
}
|
||||
|
||||
private static func currentScheduler() -> DatabaseScheduler? {
|
||||
guard let scheduler = DispatchQueue.getSpecific(key: specificKey) else {
|
||||
return nil
|
||||
}
|
||||
return scheduler
|
||||
static var current: DatabaseScheduler? {
|
||||
return DispatchQueue.getSpecific(key: specificKey)
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,84 @@ final class SerializedDatabase {
|
||||
///
|
||||
/// This method is *not* reentrant.
|
||||
func sync<T>(_ block: @noescape (db: Database) throws -> T) rethrows -> T {
|
||||
return try DatabaseScheduler.sync(queue: queue, database: db, block: block)
|
||||
// Three different cases:
|
||||
//
|
||||
// 1. A database is invoked from some queue like the main queue:
|
||||
//
|
||||
// dbQueue.inDatabase { db in
|
||||
// }
|
||||
//
|
||||
// 2. A database is invoked in a reentrant way:
|
||||
//
|
||||
// dbQueue.inDatabase { db in
|
||||
// dbQueue.inDatabase { db in
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// 3. A database in invoked from another database:
|
||||
//
|
||||
// dbQueue1.inDatabase { db1 in
|
||||
// dbQueue2.inDatabase { db2 in
|
||||
// }
|
||||
// }
|
||||
|
||||
if let sourceScheduler = DatabaseScheduler.current {
|
||||
// Case 2 or 3:
|
||||
//
|
||||
// 2. A database is invoked in a reentrant way:
|
||||
//
|
||||
// dbQueue.inDatabase { db in
|
||||
// dbQueue.inDatabase { db in
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// 3. A database in invoked from another database:
|
||||
//
|
||||
// dbQueue1.inDatabase { db1 in
|
||||
// dbQueue2.inDatabase { db2 in
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// 2 is forbidden.
|
||||
GRDBPrecondition(!sourceScheduler.allows(db), "Database methods are not reentrant.")
|
||||
|
||||
// Case 3:
|
||||
//
|
||||
// 3. A database in invoked from another database:
|
||||
//
|
||||
// dbQueue1.inDatabase { db1 in
|
||||
// dbQueue2.inDatabase { db2 in
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Let's enter the new queue, and temporarily allow the
|
||||
// currently allowed databases inside.
|
||||
return try queue.sync {
|
||||
let targetScheduler = DatabaseScheduler.current!
|
||||
assert(targetScheduler.allowedDatabases[0] === db) // sanity check
|
||||
|
||||
let backup = targetScheduler.allowedDatabases
|
||||
targetScheduler.allowedDatabases.append(contentsOf: sourceScheduler.allowedDatabases)
|
||||
defer {
|
||||
targetScheduler.allowedDatabases = backup
|
||||
}
|
||||
return try block(db: db)
|
||||
}
|
||||
} else {
|
||||
// Case 1:
|
||||
//
|
||||
// 1. A database is invoked from some queue like the main queue:
|
||||
//
|
||||
// dbQueue.inDatabase { db in
|
||||
// }
|
||||
//
|
||||
// Just dispatch block to queue:
|
||||
//
|
||||
// The impl function helps us turn dispatch_sync into a rethrowing function
|
||||
return try queue.sync {
|
||||
try block(db: db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Asynchronously executes a block in the serialized dispatch queue.
|
||||
|
||||
Reference in new issue
Block a user