Backed out changeset 59a6c227df86
authorRobert O'Callahan <robert@ocallahan.org>
Fri, 02 Oct 2009 15:23:36 +1000
changeset 33374 739b0588b4df2dd7671fa6ac55373eababc48a12
parent 33348 59a6c227df8638124dbf0050790984b3dc0b5857
child 33375 9ab0563fea4623dc2fef5c6fbbaee02e2e97c68b
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
milestone1.9.3a1pre
backs out59a6c227df8638124dbf0050790984b3dc0b5857
Backed out changeset 59a6c227df86
db/sqlite3/src/sqlite.def
storage/src/mozStorageConnection.cpp
storage/src/mozStorageConnection.h
--- a/db/sqlite3/src/sqlite.def
+++ b/db/sqlite3/src/sqlite.def
@@ -78,17 +78,16 @@ EXPORTS
         sqlite3_config
         sqlite3_create_collation
         sqlite3_create_collation16
         sqlite3_create_function
         sqlite3_create_function16
         sqlite3_create_module
         sqlite3_data_count
         sqlite3_db_handle
-        sqlite3_db_mutex
         sqlite3_declare_vtab
         sqlite3_enable_load_extension
         sqlite3_enable_shared_cache
         sqlite3_errcode
         sqlite3_errmsg
         sqlite3_errmsg16
         sqlite3_exec
         sqlite3_expired
--- a/storage/src/mozStorageConnection.cpp
+++ b/storage/src/mozStorageConnection.cpp
@@ -244,28 +244,33 @@ aggregateFunctionFinalHelper(sqlite3_con
 ////////////////////////////////////////////////////////////////////////////////
 //// Connection
 
 Connection::Connection(Service *aService)
 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex")
 , mDBConn(nsnull)
 , mAsyncExecutionMutex(nsAutoLock::NewLock("AsyncExecutionMutex"))
 , mAsyncExecutionThreadShuttingDown(PR_FALSE)
-, mDBMutex("Connection::mDBMutex")
+, mTransactionMutex(nsAutoLock::NewLock("TransactionMutex"))
 , mTransactionInProgress(PR_FALSE)
+, mFunctionsMutex(nsAutoLock::NewLock("FunctionsMutex"))
+, mProgressHandlerMutex(nsAutoLock::NewLock("ProgressHandlerMutex"))
 , mProgressHandler(nsnull)
 , mStorageService(aService)
 {
   mFunctions.Init();
 }
 
 Connection::~Connection()
 {
   (void)Close();
   nsAutoLock::DestroyLock(mAsyncExecutionMutex);
+  nsAutoLock::DestroyLock(mTransactionMutex);
+  nsAutoLock::DestroyLock(mFunctionsMutex);
+  nsAutoLock::DestroyLock(mProgressHandlerMutex);
 }
 
 NS_IMPL_THREADSAFE_ISUPPORTS1(
   Connection,
   mozIStorageConnection
 )
 
 already_AddRefed<nsIEventTarget>
@@ -291,16 +296,19 @@ Connection::getAsyncExecutionTarget()
   return eventTarget;
 }
 
 nsresult
 Connection::initialize(nsIFile *aDatabaseFile)
 {
   NS_ASSERTION (!mDBConn, "Initialize called on already opened database!");
   NS_ENSURE_TRUE(mAsyncExecutionMutex, NS_ERROR_OUT_OF_MEMORY);
+  NS_ENSURE_TRUE(mTransactionMutex, NS_ERROR_OUT_OF_MEMORY);
+  NS_ENSURE_TRUE(mFunctionsMutex, NS_ERROR_OUT_OF_MEMORY);
+  NS_ENSURE_TRUE(mProgressHandlerMutex, NS_ERROR_OUT_OF_MEMORY);
 
   int srv;
   nsresult rv;
 
   mDatabaseFile = aDatabaseFile;
 
   if (aDatabaseFile) {
     nsAutoString path;
@@ -313,19 +321,16 @@ Connection::initialize(nsIFile *aDatabas
     // in memory database requested, sqlite uses a magic file name
     srv = ::sqlite3_open(":memory:", &mDBConn);
   }
   if (srv != SQLITE_OK) {
     mDBConn = nsnull;
     return convertResultCode(srv);
   }
 
-  // Properly wrap the database handle's mutex.
-  mDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
-
 #ifdef PR_LOGGING
   if (!gStorageLog)
     gStorageLog = ::PR_NewLogModule("mozStorage");
 
   ::sqlite3_trace(mDBConn, tracefunc, this);
 
   nsCAutoString leafName(":memory");
   if (aDatabaseFile)
@@ -434,33 +439,33 @@ Connection::databaseElementExists(enum D
   }
 
   return convertResultCode(srv);
 }
 
 bool
 Connection::findFunctionByInstance(nsISupports *aInstance)
 {
-  mDBMutex.assertCurrentThreadOwns();
+  PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mFunctionsMutex);
   FFEArguments args = { aInstance, false };
   mFunctions.EnumerateRead(findFunctionEnumerator, &args);
   return args.found;
 }
 
 /* static */ int
 Connection::sProgressHelper(void *aArg)
 {
   Connection *_this = static_cast<Connection *>(aArg);
   return _this->progressHandler();
 }
 
 int
 Connection::progressHandler()
 {
-  mDBMutex.assertCurrentThreadOwns();
+  nsAutoLock mutex(mProgressHandlerMutex);
   if (mProgressHandler) {
     PRBool result;
     nsresult rv = mProgressHandler->OnProgress(this, &result);
     if (NS_FAILED(rv)) return 0; // Don't break request
     return result ? 1 : 0;
   }
   return 0;
 }
@@ -502,16 +507,22 @@ Connection::Close()
   while ((stmt = ::sqlite3_next_stmt(mDBConn, stmt))) {
     char *msg = ::PR_smprintf("SQL statement '%s' was not finalized",
                               ::sqlite3_sql(stmt));
     NS_WARNING(msg);
     ::PR_smprintf_free(msg);
   }
 #endif
 
+  {
+    nsAutoLock mutex(mProgressHandlerMutex);
+    if (mProgressHandler)
+      ::sqlite3_progress_handler(mDBConn, 0, NULL, NULL);
+  }
+
   int srv = ::sqlite3_close(mDBConn);
   NS_ASSERTION(srv == SQLITE_OK,
                "sqlite3_close failed. There are probably outstanding statements that are listed above!");
 
   mDBConn = NULL;
   return convertResultCode(srv);
 }
 
@@ -660,31 +671,31 @@ Connection::IndexExists(const nsACString
                         PRBool* _exists)
 {
     return databaseElementExists(INDEX, aIndexName, _exists);
 }
 
 NS_IMETHODIMP
 Connection::GetTransactionInProgress(PRBool *_inProgress)
 {
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mTransactionMutex);
   *_inProgress = mTransactionInProgress;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 Connection::BeginTransaction()
 {
   return BeginTransactionAs(mozIStorageConnection::TRANSACTION_DEFERRED);
 }
 
 NS_IMETHODIMP
 Connection::BeginTransactionAs(PRInt32 aTransactionType)
 {
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mTransactionMutex);
   if (mTransactionInProgress)
     return NS_ERROR_FAILURE;
   nsresult rv;
   switch(aTransactionType) {
     case TRANSACTION_DEFERRED:
       rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("BEGIN DEFERRED"));
       break;
     case TRANSACTION_IMMEDIATE:
@@ -699,29 +710,29 @@ Connection::BeginTransactionAs(PRInt32 a
   if (NS_SUCCEEDED(rv))
     mTransactionInProgress = PR_TRUE;
   return rv;
 }
 
 NS_IMETHODIMP
 Connection::CommitTransaction()
 {
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mTransactionMutex);
   if (!mTransactionInProgress)
     return NS_ERROR_FAILURE;
   nsresult rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("COMMIT TRANSACTION"));
   if (NS_SUCCEEDED(rv))
     mTransactionInProgress = PR_FALSE;
   return rv;
 }
 
 NS_IMETHODIMP
 Connection::RollbackTransaction()
 {
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mTransactionMutex);
   if (!mTransactionInProgress)
     return NS_ERROR_FAILURE;
   nsresult rv = ExecuteSimpleSQL(NS_LITERAL_CSTRING("ROLLBACK TRANSACTION"));
   if (NS_SUCCEEDED(rv))
     mTransactionInProgress = PR_FALSE;
   return rv;
 }
 
@@ -745,17 +756,17 @@ NS_IMETHODIMP
 Connection::CreateFunction(const nsACString &aFunctionName,
                            PRInt32 aNumArguments,
                            mozIStorageFunction *aFunction)
 {
   if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
 
   // Check to see if this function is already defined.  We only check the name
   // because a function can be defined with the same body but different names.
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mFunctionsMutex);
   NS_ENSURE_FALSE(mFunctions.Get(aFunctionName, NULL), NS_ERROR_FAILURE);
 
   int srv = ::sqlite3_create_function(mDBConn,
                                       nsPromiseFlatCString(aFunctionName).get(),
                                       aNumArguments,
                                       SQLITE_ANY,
                                       aFunction,
                                       basicFunctionHelper,
@@ -773,17 +784,17 @@ Connection::CreateFunction(const nsACStr
 NS_IMETHODIMP
 Connection::CreateAggregateFunction(const nsACString &aFunctionName,
                                     PRInt32 aNumArguments,
                                     mozIStorageAggregateFunction *aFunction)
 {
   if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
 
   // Check to see if this function name is already defined.
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mFunctionsMutex);
   NS_ENSURE_FALSE(mFunctions.Get(aFunctionName, NULL), NS_ERROR_FAILURE);
 
   // Because aggregate functions depend on state across calls, you cannot have
   // the same instance use the same name.  We want to enumerate all functions
   // and make sure this instance is not already registered.
   NS_ENSURE_FALSE(findFunctionByInstance(aFunction), NS_ERROR_FAILURE);
 
   int srv = ::sqlite3_create_function(mDBConn,
@@ -803,17 +814,17 @@ Connection::CreateAggregateFunction(cons
   return NS_OK;
 }
 
 NS_IMETHODIMP
 Connection::RemoveFunction(const nsACString &aFunctionName)
 {
   if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
 
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mFunctionsMutex);
   NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, NULL), NS_ERROR_FAILURE);
 
   int srv = ::sqlite3_create_function(mDBConn,
                                       nsPromiseFlatCString(aFunctionName).get(),
                                       0,
                                       SQLITE_ANY,
                                       NULL,
                                       NULL,
@@ -830,17 +841,17 @@ Connection::RemoveFunction(const nsACStr
 NS_IMETHODIMP
 Connection::SetProgressHandler(PRInt32 aGranularity,
                                mozIStorageProgressHandler *aHandler,
                                mozIStorageProgressHandler **_oldHandler)
 {
   if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
 
   // Return previous one
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mProgressHandlerMutex);
   NS_IF_ADDREF(*_oldHandler = mProgressHandler);
 
   if (!aHandler || aGranularity <= 0) {
     aHandler = nsnull;
     aGranularity = 0;
   }
   mProgressHandler = aHandler;
   ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
@@ -849,17 +860,17 @@ Connection::SetProgressHandler(PRInt32 a
 }
 
 NS_IMETHODIMP
 Connection::RemoveProgressHandler(mozIStorageProgressHandler **_oldHandler)
 {
   if (!mDBConn) return NS_ERROR_NOT_INITIALIZED;
 
   // Return previous one
-  SQLiteMutexAutoLock lockedScope(mDBMutex);
+  nsAutoLock mutex(mProgressHandlerMutex);
   NS_IF_ADDREF(*_oldHandler = mProgressHandler);
 
   mProgressHandler = nsnull;
   ::sqlite3_progress_handler(mDBConn, 0, NULL, NULL);
 
   return NS_OK;
 }
 
--- a/storage/src/mozStorageConnection.h
+++ b/storage/src/mozStorageConnection.h
@@ -43,17 +43,16 @@
 
 #include "nsAutoPtr.h"
 #include "nsCOMPtr.h"
 #include "mozilla/Mutex.h"
 
 #include "nsString.h"
 #include "nsInterfaceHashtable.h"
 #include "mozIStorageProgressHandler.h"
-#include "SQLiteMutex.h"
 #include "mozIStorageConnection.h"
 #include "mozStorageService.h"
 
 #include "nsIMutableArray.h"
 
 #include "sqlite3.h"
 
 struct PRLock;
@@ -154,37 +153,23 @@ private:
   /**
    * Set to true by Close() prior to actually shutting down the thread.  This
    * lets getAsyncExecutionTarget() know not to hand out any more thread
    * references (or to create the thread in the first place).  This variable
    * should be accessed while holding the mAsyncExecutionMutex.
    */
   PRBool mAsyncExecutionThreadShuttingDown;
 
-  /**
-   * Wraps the mutex that SQLite gives us from sqlite3_db_mutex.
-   */
-  SQLiteMutex mDBMutex;
-
-  /**
-   * Tracks if we have a transaction in progress or not.  Access protected by
-   * mDBMutex.
-   */
+  PRLock *mTransactionMutex;
   PRBool mTransactionInProgress;
 
-  /**
-   * Stores the mapping of a given function by name to its instance.  Access is
-   * protected by mDBMutex.
-   */
+  PRLock *mFunctionsMutex;
   nsInterfaceHashtable<nsCStringHashKey, nsISupports> mFunctions;
 
-  /**
-   * Stores the registered progress handler for the database connection.  Access
-   * is protected by mDBMutex.
-   */
+  PRLock *mProgressHandlerMutex;
   nsCOMPtr<mozIStorageProgressHandler> mProgressHandler;
 
   // This is here for two reasons: 1) It's used to make sure that the
   // connections do not outlive the service.  2) Our custom collating functions
   // call its localeCompareStrings() method.
   nsRefPtr<Service> mStorageService;
 };