This commit is contained in:
Gwendal Roué committed 2016-07-08 18:02:06 +02:00
1 parent 1cd9c8f817
commit 827dc3da8b
2 files changed
+16 -15

No files matched your search

+12 -5
View File
@@ -7,22 +7,21 @@
/// 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.
///
/// Two entry points:
///
/// - DatabaseScheduler.makeSerializedQueueAllowing(database:) creates a
/// dispatch queue that allows one database.
///
/// It does so by registering one instance of DatabaseScheduler as a specific
/// of the dispatch queue, a DatabaseScheduler that allows that database only.
///
/// Later on, the queue can be shared by several databases by mutating the
/// allowedDatabases property. See SerializedDatabase.sync()
/// Later on, the queue can be shared by several databases with the method
/// allowing(databases:execute:). See SerializedDatabase.sync() for
/// an example.
///
/// - preconditionValidQueue() crashes whenever a database is used in an invalid
/// dispatch queue.
final class DatabaseScheduler {
private static let specificKey = DispatchSpecificKey<DatabaseScheduler>()
var allowedDatabases: [Database]
private(set) var allowedDatabases: [Database]
private init(allowedDatabase database: Database) {
allowedDatabases = [database]
@@ -35,6 +34,14 @@ final class DatabaseScheduler {
return queue
}
// Temporarily allows `databases` while executing `body`
func allowing<T>(databases: [Database], execute body: @noescape () throws -> T) rethrows -> T {
let backup = allowedDatabases
allowedDatabases.append(contentsOf: databases)
defer { allowedDatabases = backup }
return try body()
}
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)
}
+4 -10
View File
@@ -73,7 +73,7 @@ final class SerializedDatabase {
// }
// }
if let sourceScheduler = DatabaseScheduler.current {
if let scheduler = DatabaseScheduler.current {
// Case 2 or 3:
//
// 2. A database is invoked in a reentrant way:
@@ -91,7 +91,7 @@ final class SerializedDatabase {
// }
//
// 2 is forbidden.
GRDBPrecondition(!sourceScheduler.allows(db), "Database methods are not reentrant.")
GRDBPrecondition(!scheduler.allows(db), "Database methods are not reentrant.")
// Case 3:
//
@@ -105,15 +105,9 @@ final class SerializedDatabase {
// 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
try DatabaseScheduler.current!.allowing(databases: scheduler.allowedDatabases) {
try block(db: db)
}
return try block(db: db)
}
} else {
// Case 1: