[geary/wip/serial-db-724475] Add mutex around upgrades



commit 145e310f2fdfabefc379c8228ac187220c963806
Author: Charles Lindsay <chaz yorba org>
Date:   Mon Mar 3 17:28:02 2014 -0800

    Add mutex around upgrades

 src/engine/db/db-versioned-database.vala |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
---
diff --git a/src/engine/db/db-versioned-database.vala b/src/engine/db/db-versioned-database.vala
index 311438d..850738d 100644
--- a/src/engine/db/db-versioned-database.vala
+++ b/src/engine/db/db-versioned-database.vala
@@ -7,6 +7,8 @@
 public class Geary.Db.VersionedDatabase : Geary.Db.Database {
     public delegate void WorkCallback();
     
+    private static Mutex upgrade_mutex = new Mutex();
+    
     public File schema_dir { get; private set; }
     
     public VersionedDatabase(File db_file, File schema_dir) {
@@ -107,6 +109,14 @@ public class Geary.Db.VersionedDatabase : Geary.Db.Database {
                 started = true;
             }
             
+            // Since these upgrades run in a background thread, there's a possibility they
+            // can run in parallel.  That leads to Geary absolutely taking over the machine,
+            // with potentially several threads all doing heavy database manipulation at
+            // once.  So, we wrap this bit in a mutex lock so that only one database is
+            // updating at once.  It means overall it might take a bit longer, but it keeps
+            // things usable in the meantime.  See <https://bugzilla.gnome.org/show_bug.cgi?id=724475>.
+            upgrade_mutex  lock();
+            
             pre_upgrade(db_version);
             
             check_cancelled("VersionedDatabase.open", cancellable);
@@ -121,11 +131,14 @@ public class Geary.Db.VersionedDatabase : Geary.Db.Database {
                 }, cancellable);
             } catch (Error err) {
                 warning("Error upgrading database to version %d: %s", db_version, err.message);
+                upgrade_mutex.unlock();
                 
                 throw err;
             }
             
             post_upgrade(db_version);
+            
+            upgrade_mutex.unlock();
         }
         
         if (started)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]