Bug 700544: Transactions should be ordered based on creation, not on the first request. r=bent

This commit is contained in:
Kyle Huey 2011-11-18 10:21:04 -05:00
parent 12171860ba
commit 897fb61998
5 changed files with 107 additions and 18 deletions

View File

@ -73,6 +73,35 @@ DoomCachedStatements(const nsACString& aQuery,
return PL_DHASH_REMOVE;
}
// This runnable doesn't actually do anything beyond "prime the pump" and get
// transactions in the right order on the transaction thread pool.
class StartTransactionRunnable : public nsIRunnable
{
public:
NS_DECL_ISUPPORTS
NS_IMETHOD Run()
{
// NOP
return NS_OK;
}
};
// Could really use those NS_REFCOUNTING_HAHA_YEAH_RIGHT macros here.
NS_IMETHODIMP_(nsrefcnt) StartTransactionRunnable::AddRef()
{
return 2;
}
NS_IMETHODIMP_(nsrefcnt) StartTransactionRunnable::Release()
{
return 1;
}
NS_IMPL_QUERY_INTERFACE1(StartTransactionRunnable, nsIRunnable)
StartTransactionRunnable gStartTransactionRunnable;
} // anonymous namespace
// static
@ -121,6 +150,11 @@ IDBTransaction::Create(IDBDatabase* aDatabase,
transaction->mCreating = true;
}
if (aMode != nsIIDBTransaction::VERSION_CHANGE) {
TransactionThreadPool* pool = TransactionThreadPool::GetOrCreate();
pool->Dispatch(transaction, &gStartTransactionRunnable, false, nsnull);
}
return transaction.forget();
}

View File

@ -47,8 +47,7 @@
#include "nsServiceManagerUtils.h"
#include "nsXPCOMCIDInternal.h"
using mozilla::MutexAutoLock;
using mozilla::MutexAutoUnlock;
using mozilla::MonitorAutoLock;
USING_INDEXEDDB_NAMESPACE
@ -604,8 +603,7 @@ TransactionThreadPool::MaybeFireCallback(PRUint32 aCallbackIndex)
TransactionThreadPool::
TransactionQueue::TransactionQueue(IDBTransaction* aTransaction,
nsIRunnable* aRunnable)
: mMutex("TransactionQueue::mMutex"),
mCondVar(mMutex, "TransactionQueue::mCondVar"),
: mMonitor("TransactionQueue::mMonitor"),
mTransaction(aTransaction),
mShouldFinish(false)
{
@ -617,29 +615,26 @@ TransactionQueue::TransactionQueue(IDBTransaction* aTransaction,
void
TransactionThreadPool::TransactionQueue::Dispatch(nsIRunnable* aRunnable)
{
MutexAutoLock lock(mMutex);
MonitorAutoLock lock(mMonitor);
NS_ASSERTION(!mShouldFinish, "Dispatch called after Finish!");
if (!mQueue.AppendElement(aRunnable)) {
MutexAutoUnlock unlock(mMutex);
NS_RUNTIMEABORT("Out of memory!");
}
mQueue.AppendElement(aRunnable);
mCondVar.Notify();
mMonitor.Notify();
}
void
TransactionThreadPool::TransactionQueue::Finish(nsIRunnable* aFinishRunnable)
{
MutexAutoLock lock(mMutex);
MonitorAutoLock lock(mMonitor);
NS_ASSERTION(!mShouldFinish, "Finish called more than once!");
mShouldFinish = true;
mFinishRunnable = aFinishRunnable;
mCondVar.Notify();
mMonitor.Notify();
}
NS_IMPL_THREADSAFE_ISUPPORTS1(TransactionThreadPool::TransactionQueue,
@ -656,9 +651,9 @@ TransactionThreadPool::TransactionQueue::Run()
NS_ASSERTION(queue.IsEmpty(), "Should have cleared this!");
{
MutexAutoLock lock(mMutex);
MonitorAutoLock lock(mMonitor);
while (!mShouldFinish && mQueue.IsEmpty()) {
if (NS_FAILED(mCondVar.Wait())) {
if (NS_FAILED(mMonitor.Wait())) {
NS_ERROR("Failed to wait!");
}
}

View File

@ -46,8 +46,7 @@
#include "nsIObserver.h"
#include "nsIRunnable.h"
#include "mozilla/Mutex.h"
#include "mozilla/CondVar.h"
#include "mozilla/Monitor.h"
#include "nsClassHashtable.h"
#include "nsHashKeys.h"
#include "nsRefPtrHashtable.h"
@ -106,8 +105,7 @@ protected:
inline void Finish(nsIRunnable* aFinishRunnable);
private:
mozilla::Mutex mMutex;
mozilla::CondVar mCondVar;
mozilla::Monitor mMonitor;
IDBTransaction* mTransaction;
nsAutoTArray<nsCOMPtr<nsIRunnable>, 10> mQueue;
nsCOMPtr<nsIRunnable> mFinishRunnable;

View File

@ -102,6 +102,7 @@ TEST_FILES = \
test_transaction_abort.html \
test_transaction_lifetimes.html \
test_transaction_lifetimes_nested.html \
test_transaction_ordering.html \
test_setVersion.html \
test_setVersion_abort.html \
test_setVersion_events.html \

View File

@ -0,0 +1,61 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>Indexed Database Property Test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="text/javascript;version=1.7">
function testSteps()
{
let request = mozIndexedDB.open(window.location.pathname, 1);
request.onerror = errorHandler;
request.onupgradeneeded = grabEventAndContinueHandler;
let event = yield;
let db = event.target.result;
db.onerror = errorHandler;
request.onsuccess = continueToNextStep;
db.createObjectStore("foo");
yield;
let trans1 = db.transaction("foo", IDBTransaction.READ_WRITE);
let trans2 = db.transaction("foo", IDBTransaction.READ_WRITE);
let request1 = trans2.objectStore("foo").put("2", 42);
let request2 = trans1.objectStore("foo").put("1", 42);
request1.onerror = errorHandler;
request2.onerror = errorHandler;
trans1.oncomplete = grabEventAndContinueHandler;
trans2.oncomplete = grabEventAndContinueHandler;
yield;
yield;
let trans3 = db.transaction("foo", IDBTransaction.READ);
let request = trans3.objectStore("foo").get(42);
request.onsuccess = grabEventAndContinueHandler;
request.onerror = errorHandler;
let event = yield;
is(event.target.result, "2", "Transactions were ordered properly.");
finishTest();
yield;
}
</script>
<script type="text/javascript;version=1.7" src="helpers.js"></script>
</head>
<body onload="runTest();"></body>
</html>