From de4c06478dae37e052a9cd3a36a5626ba9cc4dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 6 Nov 2021 15:54:52 +0100 Subject: [PATCH] New Concurrency Images --- Documentation/Concurrency.md | 8 +- .../Images/DatabasePoolConcurrentRead.svg | 335 ++++++++++++++++ .../Images/DatabasePoolScheduling.svg | 359 +++++++++++------- .../Images/DatabaseQueueScheduling.svg | 305 +++++++++------ Documentation/Images/Makefile | 3 - 5 files changed, 739 insertions(+), 271 deletions(-) create mode 100644 Documentation/Images/DatabasePoolConcurrentRead.svg delete mode 100644 Documentation/Images/Makefile diff --git a/Documentation/Concurrency.md b/Documentation/Concurrency.md index 58076e289..911c5a6b0 100644 --- a/Documentation/Concurrency.md +++ b/Documentation/Concurrency.md @@ -320,11 +320,11 @@ Despite the common [guarantees](#safe-and-unsafe-database-accesses) and [rules]( [DatabaseQueue] opens a single database connection, and serializes all database accesses, reads, and writes. There is never more than one thread that uses the database. In the image below, we see how three threads can see the database as time passes: -![DatabaseQueueScheduling](https://cdn.rawgit.com/groue/GRDB.swift/master/Documentation/Images/DatabaseQueueScheduling.svg) +![DatabaseQueueScheduling](https://cdn.rawgit.com/groue/GRDB.swift/development/Documentation/Images/DatabaseQueueScheduling.svg) [DatabasePool] manages a pool of several database connections, and allows concurrent reads and writes. It serializes all writes. Reads are isolated so that they don't see changes performed by other threads. This gives a very different picture: -![DatabasePoolScheduling](https://cdn.rawgit.com/groue/GRDB.swift/master/Documentation/Images/DatabasePoolScheduling.svg) +![DatabasePoolScheduling](https://cdn.rawgit.com/groue/GRDB.swift/development/Documentation/Images/DatabasePoolScheduling.svg) See how, with database pools, two reads can see different database states at the same time. This may look scary, but there is a simple way to think about it. After all, most applications are generally interested in the latest state of the database: @@ -402,6 +402,10 @@ try dbPool.writeWithoutTransaction { db in `concurrentRead` and `asyncConcurrentRead` block until they can guarantee their closure argument an isolated access to the database, in the exact state left by the last transaction. It then asynchronously executes this closure. +In the illustration below, the striped band shows the delay needed for the reading thread to acquire isolation. Until then, other threads can not write: + +![DatabasePoolConcurrentRead](https://cdn.rawgit.com/groue/GRDB.swift/development/Documentation/Images/DatabasePoolConcurrentRead.svg) + [Transaction Observers](../README.md#transactionobserver-protocol) can also use those methods in their `databaseDidCommit` method, in order to process database changes without blocking other threads that want to write into the database. diff --git a/Documentation/Images/DatabasePoolConcurrentRead.svg b/Documentation/Images/DatabasePoolConcurrentRead.svg new file mode 100644 index 000000000..6f5408185 --- /dev/null +++ b/Documentation/Images/DatabasePoolConcurrentRead.svg @@ -0,0 +1,335 @@ + + + DatabasePoolConcurrentRead + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Documentation/Images/DatabasePoolScheduling.svg b/Documentation/Images/DatabasePoolScheduling.svg index a39e6ab27..030414466 100644 --- a/Documentation/Images/DatabasePoolScheduling.svg +++ b/Documentation/Images/DatabasePoolScheduling.svg @@ -1,173 +1,248 @@ - - + DatabasePoolScheduling - Created with Sketch. - - + + + + + + + + + + + - - - - + + - - + + + + + - - - - + + + + + + - - + + + + + - - - - + + + + + + - - + + + + + - - - - + + + + + + - - + + + + + - - - - + + - - - - - - - - - - + + + + + - - - - - - - - Thread 1 - - - Thread 2 - - - Thread 3 - - - - - - - - - - - - - Read State A - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Read State A - + + + + + + + + + + + + + + - - - - - - - - - - - - Write State B - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Read State B - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Write State C - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Read State C - + + + + + + + + + + + + + + + + + + - - - Time - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + \ No newline at end of file diff --git a/Documentation/Images/DatabaseQueueScheduling.svg b/Documentation/Images/DatabaseQueueScheduling.svg index 01cdf4cf6..559a2947d 100644 --- a/Documentation/Images/DatabaseQueueScheduling.svg +++ b/Documentation/Images/DatabaseQueueScheduling.svg @@ -1,150 +1,207 @@ - - + DatabaseQueueScheduling - Created with Sketch. - - + + + + + + + + + + + - - - - + + - - + + + + + - - - - + + - - + + + + + - - - - + + - - + + + + + - - - - + + - - - - - - - - - - + + + + + - - - - - - - - Thread 1 - - - Thread 2 - - - Thread 3 - - - - Time - - - - - - - - - - - - - - - Read State A - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Write State B - + + + + + + + + + + + + + + - - - - - - - - - - - - Read State B - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Read State C - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - Write State C - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + \ No newline at end of file diff --git a/Documentation/Images/Makefile b/Documentation/Images/Makefile deleted file mode 100644 index 7c42c98f5..000000000 --- a/Documentation/Images/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -fixfonts: - sed -i '' -- 's/SFUIText-Regular, SF UI Text/-apple-system,sans-serif/g' *.svg - sed -i '' -- 's/SFUIText-Semibold, SF UI Text/-apple-system,sans-serif/g' *.svg