2010-06-23 23:46:08 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIThreadPool.h"
|
|
|
|
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
|
2011-11-18 19:21:04 +04:00
|
|
|
using mozilla::MonitorAutoLock;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const PRUint32 kThreadLimit = 20;
|
|
|
|
const PRUint32 kIdleThreadLimit = 5;
|
|
|
|
const PRUint32 kIdleThreadTimeoutMs = 30000;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
TransactionThreadPool* gInstance = nullptr;
|
2010-06-23 23:46:08 +04:00
|
|
|
bool gShutdown = false;
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
inline
|
|
|
|
nsresult
|
|
|
|
CheckOverlapAndMergeObjectStores(nsTArray<nsString>& aLockedStores,
|
|
|
|
const nsTArray<nsString>& aObjectStores,
|
|
|
|
bool aShouldMerge,
|
|
|
|
bool* aStoresOverlap)
|
|
|
|
{
|
|
|
|
PRUint32 length = aObjectStores.Length();
|
|
|
|
|
|
|
|
bool overlap = false;
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < length; index++) {
|
|
|
|
const nsString& storeName = aObjectStores[index];
|
|
|
|
if (aLockedStores.Contains(storeName)) {
|
|
|
|
overlap = true;
|
|
|
|
}
|
|
|
|
else if (aShouldMerge && !aLockedStores.AppendElement(storeName)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aStoresOverlap = overlap;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
BEGIN_INDEXEDDB_NAMESPACE
|
|
|
|
|
2012-05-15 08:50:29 +04:00
|
|
|
class FinishTransactionRunnable MOZ_FINAL : public nsIRunnable
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2010-06-28 20:46:21 +04:00
|
|
|
inline FinishTransactionRunnable(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
nsCOMPtr<nsIRunnable>& aFinishRunnable);
|
|
|
|
|
|
|
|
private:
|
2010-06-28 20:46:21 +04:00
|
|
|
IDBTransaction* mTransaction;
|
2010-06-23 23:46:08 +04:00
|
|
|
nsCOMPtr<nsIRunnable> mFinishRunnable;
|
|
|
|
};
|
|
|
|
|
|
|
|
END_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
TransactionThreadPool::TransactionThreadPool()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!gInstance, "More than one instance!");
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionThreadPool::~TransactionThreadPool()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 21:58:33 +04:00
|
|
|
NS_ASSERTION(gInstance == this, "Different instances!");
|
2012-07-30 18:20:58 +04:00
|
|
|
gInstance = nullptr;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
TransactionThreadPool*
|
|
|
|
TransactionThreadPool::GetOrCreate()
|
|
|
|
{
|
|
|
|
if (!gInstance && !gShutdown) {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 21:58:33 +04:00
|
|
|
nsAutoPtr<TransactionThreadPool> pool(new TransactionThreadPool());
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsresult rv = pool->Init();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
gInstance = pool.forget();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
2010-09-10 23:12:11 +04:00
|
|
|
// static
|
|
|
|
TransactionThreadPool*
|
|
|
|
TransactionThreadPool::Get()
|
|
|
|
{
|
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
TransactionThreadPool::Shutdown()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
gShutdown = true;
|
|
|
|
|
|
|
|
if (gInstance) {
|
|
|
|
if (NS_FAILED(gInstance->Cleanup())) {
|
|
|
|
NS_WARNING("Failed to shutdown thread pool!");
|
|
|
|
}
|
2010-10-19 21:58:33 +04:00
|
|
|
delete gInstance;
|
2012-07-30 18:20:58 +04:00
|
|
|
gInstance = nullptr;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TransactionThreadPool::Init()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-05-18 21:30:49 +04:00
|
|
|
mTransactionsInProgress.Init();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
mThreadPool = do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-12 21:06:20 +04:00
|
|
|
rv = mThreadPool->SetName(NS_LITERAL_CSTRING("IndexedDB Trans"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
rv = mThreadPool->SetThreadLimit(kThreadLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mThreadPool->SetIdleThreadLimit(kIdleThreadLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mThreadPool->SetIdleThreadTimeout(kIdleThreadTimeoutMs);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TransactionThreadPool::Cleanup()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsresult rv = mThreadPool->Shutdown();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
// Make sure the pool is still accessible while any callbacks generated from
|
|
|
|
// the other threads are processed.
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_ProcessPendingEvents(nullptr);
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mCompleteCallbacks.IsEmpty()) {
|
|
|
|
// Run all callbacks manually now.
|
|
|
|
for (PRUint32 index = 0; index < mCompleteCallbacks.Length(); index++) {
|
|
|
|
mCompleteCallbacks[index].mCallback->Run();
|
|
|
|
}
|
|
|
|
mCompleteCallbacks.Clear();
|
|
|
|
|
|
|
|
// And make sure they get processed.
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_ProcessPendingEvents(nullptr);
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-28 20:46:21 +04:00
|
|
|
TransactionThreadPool::FinishTransaction(IDBTransaction* aTransaction)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aTransaction, "Null pointer!");
|
|
|
|
|
|
|
|
// AddRef here because removing from the hash will call Release.
|
2010-06-28 20:46:21 +04:00
|
|
|
nsRefPtr<IDBTransaction> transaction(aTransaction);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-02 16:53:12 +04:00
|
|
|
nsIAtom* databaseId = aTransaction->mDatabase->Id();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
|
|
|
if (!mTransactionsInProgress.Get(databaseId, &dbTransactionInfo)) {
|
|
|
|
NS_ERROR("We don't know anyting about this database?!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<TransactionInfo>& transactionsInProgress =
|
|
|
|
dbTransactionInfo->transactions;
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
PRUint32 transactionCount = transactionsInProgress.Length();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-10-19 21:58:52 +04:00
|
|
|
if (aTransaction->mMode == IDBTransaction::VERSION_CHANGE) {
|
2010-07-12 18:05:01 +04:00
|
|
|
NS_ASSERTION(transactionCount == 1,
|
|
|
|
"More transactions running than should be!");
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
if (transactionCount == 1) {
|
2010-06-23 23:46:08 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
TransactionInfo& info = transactionsInProgress[0];
|
|
|
|
NS_ASSERTION(info.transaction == aTransaction, "Transaction mismatch!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mTransactionsInProgress.Remove(databaseId);
|
2010-10-19 21:58:33 +04:00
|
|
|
|
|
|
|
// See if we need to fire any complete callbacks.
|
2012-06-03 20:33:52 +04:00
|
|
|
PRUint32 index = 0;
|
|
|
|
while (index < mCompleteCallbacks.Length()) {
|
|
|
|
if (MaybeFireCallback(mCompleteCallbacks[index])) {
|
|
|
|
mCompleteCallbacks.RemoveElementAt(index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
index++;
|
|
|
|
}
|
2010-10-19 21:58:33 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-07-12 18:05:01 +04:00
|
|
|
// We need to rebuild the locked object store list.
|
|
|
|
nsTArray<nsString> storesWriting, storesReading;
|
|
|
|
|
|
|
|
for (PRUint32 index = 0, count = transactionCount; index < count; index++) {
|
|
|
|
IDBTransaction* transaction = transactionsInProgress[index].transaction;
|
|
|
|
if (transaction == aTransaction) {
|
|
|
|
NS_ASSERTION(count == transactionCount, "More than one match?!");
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
transactionsInProgress.RemoveElementAt(index);
|
2010-07-12 18:05:01 +04:00
|
|
|
index--;
|
|
|
|
count--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray<nsString>& objectStores = transaction->mObjectStoreNames;
|
|
|
|
|
|
|
|
bool dummy;
|
2012-03-13 08:44:45 +04:00
|
|
|
if (transaction->mMode == IDBTransaction::READ_WRITE) {
|
2010-07-12 18:05:01 +04:00
|
|
|
if (NS_FAILED(CheckOverlapAndMergeObjectStores(storesWriting,
|
|
|
|
objectStores,
|
|
|
|
true, &dummy))) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
}
|
|
|
|
}
|
2012-03-13 08:44:45 +04:00
|
|
|
else if (transaction->mMode == IDBTransaction::READ_ONLY) {
|
2010-07-12 18:05:01 +04:00
|
|
|
if (NS_FAILED(CheckOverlapAndMergeObjectStores(storesReading,
|
|
|
|
objectStores,
|
|
|
|
true, &dummy))) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown mode!");
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
NS_ASSERTION(transactionsInProgress.Length() == transactionCount - 1,
|
2010-06-23 23:46:08 +04:00
|
|
|
"Didn't find the transaction we were looking for!");
|
2010-07-12 18:05:01 +04:00
|
|
|
|
|
|
|
dbTransactionInfo->storesWriting.SwapElements(storesWriting);
|
|
|
|
dbTransactionInfo->storesReading.SwapElements(storesReading);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to dispatch all the queued transactions again.
|
|
|
|
nsTArray<QueuedDispatchInfo> queuedDispatch;
|
|
|
|
queuedDispatch.SwapElements(mDelayedDispatchQueue);
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
transactionCount = queuedDispatch.Length();
|
|
|
|
for (PRUint32 index = 0; index < transactionCount; index++) {
|
|
|
|
if (NS_FAILED(Dispatch(queuedDispatch[index]))) {
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_WARNING("Dispatch failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
nsresult
|
2010-06-28 20:46:21 +04:00
|
|
|
TransactionThreadPool::TransactionCanRun(IDBTransaction* aTransaction,
|
2010-07-12 18:05:01 +04:00
|
|
|
bool* aCanRun,
|
|
|
|
TransactionQueue** aExistingQueue)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aTransaction, "Null pointer!");
|
2010-07-12 18:05:01 +04:00
|
|
|
NS_ASSERTION(aCanRun, "Null pointer!");
|
|
|
|
NS_ASSERTION(aExistingQueue, "Null pointer!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-02 16:53:12 +04:00
|
|
|
nsIAtom* databaseId = aTransaction->mDatabase->Id();
|
2010-06-23 23:46:08 +04:00
|
|
|
const nsTArray<nsString>& objectStoreNames = aTransaction->mObjectStoreNames;
|
|
|
|
const PRUint16 mode = aTransaction->mMode;
|
|
|
|
|
|
|
|
// See if we can run this transaction now.
|
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
|
|
|
if (!mTransactionsInProgress.Get(databaseId, &dbTransactionInfo)) {
|
|
|
|
// First transaction for this database, fine to run.
|
2010-07-12 18:05:01 +04:00
|
|
|
*aCanRun = true;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aExistingQueue = nullptr;
|
2010-07-12 18:05:01 +04:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<TransactionInfo>& transactionsInProgress =
|
|
|
|
dbTransactionInfo->transactions;
|
|
|
|
|
|
|
|
PRUint32 transactionCount = transactionsInProgress.Length();
|
2010-07-12 18:05:01 +04:00
|
|
|
NS_ASSERTION(transactionCount, "Should never be 0!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
for (PRUint32 index = 0; index < transactionCount; index++) {
|
|
|
|
// See if this transaction is in out list of current transactions.
|
|
|
|
const TransactionInfo& info = transactionsInProgress[index];
|
|
|
|
if (info.transaction == aTransaction) {
|
|
|
|
*aCanRun = true;
|
|
|
|
*aExistingQueue = info.queue;
|
|
|
|
return NS_OK;
|
2010-07-12 19:25:49 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-10-25 16:49:31 +04:00
|
|
|
NS_ASSERTION(mode != IDBTransaction::VERSION_CHANGE, "How did we get here?");
|
2010-07-12 19:25:49 +04:00
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
bool writeOverlap;
|
|
|
|
nsresult rv =
|
|
|
|
CheckOverlapAndMergeObjectStores(dbTransactionInfo->storesWriting,
|
|
|
|
objectStoreNames,
|
2012-03-13 08:44:45 +04:00
|
|
|
mode == IDBTransaction::READ_WRITE,
|
2010-07-12 18:05:01 +04:00
|
|
|
&writeOverlap);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool readOverlap;
|
|
|
|
rv = CheckOverlapAndMergeObjectStores(dbTransactionInfo->storesReading,
|
|
|
|
objectStoreNames,
|
2012-03-13 08:44:45 +04:00
|
|
|
mode == IDBTransaction::READ_ONLY,
|
2010-07-12 18:05:01 +04:00
|
|
|
&readOverlap);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (writeOverlap ||
|
2012-03-13 08:44:45 +04:00
|
|
|
(readOverlap && mode == IDBTransaction::READ_WRITE)) {
|
2010-07-12 18:05:01 +04:00
|
|
|
*aCanRun = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aExistingQueue = nullptr;
|
2010-07-12 18:05:01 +04:00
|
|
|
return NS_OK;
|
2010-07-12 19:25:49 +04:00
|
|
|
}
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
*aCanRun = true;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aExistingQueue = nullptr;
|
2010-07-12 18:05:01 +04:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-28 20:46:21 +04:00
|
|
|
TransactionThreadPool::Dispatch(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
nsIRunnable* aRunnable,
|
|
|
|
bool aFinish,
|
|
|
|
nsIRunnable* aFinishRunnable)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aTransaction, "Null pointer!");
|
|
|
|
NS_ASSERTION(aRunnable, "Null pointer!");
|
|
|
|
|
2010-09-10 23:12:11 +04:00
|
|
|
if (aTransaction->mDatabase->IsInvalidated() && !aFinish) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
bool canRun;
|
2010-06-23 23:46:08 +04:00
|
|
|
TransactionQueue* existingQueue;
|
2010-07-12 18:05:01 +04:00
|
|
|
nsresult rv = TransactionCanRun(aTransaction, &canRun, &existingQueue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!canRun) {
|
2010-06-23 23:46:08 +04:00
|
|
|
QueuedDispatchInfo* info = mDelayedDispatchQueue.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(info, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
info->transaction = aTransaction;
|
|
|
|
info->runnable = aRunnable;
|
|
|
|
info->finish = aFinish;
|
|
|
|
info->finishRunnable = aFinishRunnable;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingQueue) {
|
|
|
|
existingQueue->Dispatch(aRunnable);
|
|
|
|
if (aFinish) {
|
|
|
|
existingQueue->Finish(aFinishRunnable);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-02 16:53:12 +04:00
|
|
|
nsIAtom* databaseId = aTransaction->mDatabase->Id();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-10-19 21:58:52 +04:00
|
|
|
if (aTransaction->mMode == IDBTransaction::VERSION_CHANGE) {
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION(!mTransactionsInProgress.Get(databaseId, nullptr),
|
2010-06-23 23:46:08 +04:00
|
|
|
"Shouldn't have anything in progress!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
|
|
|
nsAutoPtr<DatabaseTransactionInfo> autoDBTransactionInfo;
|
|
|
|
|
|
|
|
if (!mTransactionsInProgress.Get(databaseId, &dbTransactionInfo)) {
|
|
|
|
// Make a new struct for this transaction.
|
|
|
|
autoDBTransactionInfo = new DatabaseTransactionInfo();
|
|
|
|
dbTransactionInfo = autoDBTransactionInfo;
|
|
|
|
}
|
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
const nsTArray<nsString>& objectStoreNames = aTransaction->mObjectStoreNames;
|
|
|
|
|
|
|
|
nsTArray<nsString>& storesInUse =
|
2012-03-13 08:44:45 +04:00
|
|
|
aTransaction->mMode == IDBTransaction::READ_WRITE ?
|
2010-07-12 18:05:01 +04:00
|
|
|
dbTransactionInfo->storesWriting :
|
|
|
|
dbTransactionInfo->storesReading;
|
|
|
|
|
|
|
|
if (!storesInUse.AppendElements(objectStoreNames)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsTArray<TransactionInfo>& transactionInfoArray =
|
|
|
|
dbTransactionInfo->transactions;
|
|
|
|
|
|
|
|
TransactionInfo* transactionInfo = transactionInfoArray.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(transactionInfo, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
transactionInfo->transaction = aTransaction;
|
|
|
|
transactionInfo->queue = new TransactionQueue(aTransaction, aRunnable);
|
|
|
|
if (aFinish) {
|
|
|
|
transactionInfo->queue->Finish(aFinishRunnable);
|
|
|
|
}
|
2010-07-12 19:25:49 +04:00
|
|
|
|
2010-07-12 18:05:01 +04:00
|
|
|
if (!transactionInfo->objectStoreNames.AppendElements(objectStoreNames)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (autoDBTransactionInfo) {
|
2012-05-18 21:30:49 +04:00
|
|
|
mTransactionsInProgress.Put(databaseId, autoDBTransactionInfo);
|
2010-06-23 23:46:08 +04:00
|
|
|
autoDBTransactionInfo.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mThreadPool->Dispatch(transactionInfo->queue, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
bool
|
2010-10-19 21:58:52 +04:00
|
|
|
TransactionThreadPool::WaitForAllDatabasesToComplete(
|
2012-08-10 20:15:02 +04:00
|
|
|
nsTArray<IDBDatabase*>& aDatabases,
|
|
|
|
nsIRunnable* aCallback)
|
2010-09-10 23:12:11 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ASSERTION(!aDatabases.IsEmpty(), "No databases to wait on!");
|
2010-10-19 21:58:33 +04:00
|
|
|
NS_ASSERTION(aCallback, "Null pointer!");
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
DatabasesCompleteCallback* callback = mCompleteCallbacks.AppendElement();
|
|
|
|
if (!callback) {
|
2010-10-19 21:58:33 +04:00
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
callback->mCallback = aCallback;
|
|
|
|
if (!callback->mDatabases.SwapElements(aDatabases)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
2012-06-03 20:33:52 +04:00
|
|
|
if (MaybeFireCallback(*callback)) {
|
|
|
|
mCompleteCallbacks.RemoveElementAt(mCompleteCallbacks.Length() - 1);
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:33 +04:00
|
|
|
return true;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-01-28 00:47:36 +03:00
|
|
|
void
|
|
|
|
TransactionThreadPool::AbortTransactionsForDatabase(IDBDatabase* aDatabase)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabase, "Null pointer!");
|
|
|
|
|
|
|
|
// Get list of transactions for this database id
|
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
|
|
|
if (!mTransactionsInProgress.Get(aDatabase->Id(), &dbTransactionInfo)) {
|
|
|
|
// If there are no running transactions, there can't be any pending ones
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<nsRefPtr<IDBTransaction>, 50> transactions;
|
|
|
|
|
|
|
|
// Collect any running transactions
|
|
|
|
nsTArray<TransactionInfo>& transactionsInProgress =
|
|
|
|
dbTransactionInfo->transactions;
|
|
|
|
|
|
|
|
PRUint32 transactionCount = transactionsInProgress.Length();
|
|
|
|
NS_ASSERTION(transactionCount, "Should never be 0!");
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < transactionCount; index++) {
|
|
|
|
// See if any transaction belongs to this IDBDatabase instance
|
|
|
|
IDBTransaction* transaction = transactionsInProgress[index].transaction;
|
|
|
|
if (transaction->Database() == aDatabase) {
|
|
|
|
transactions.AppendElement(transaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect any pending transactions.
|
|
|
|
for (PRUint32 index = 0; index < mDelayedDispatchQueue.Length(); index++) {
|
|
|
|
// See if any transaction belongs to this IDBDatabase instance
|
|
|
|
IDBTransaction* transaction = mDelayedDispatchQueue[index].transaction;
|
|
|
|
if (transaction->Database() == aDatabase) {
|
|
|
|
transactions.AppendElement(transaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort transactions. Do this after collecting the transactions in case
|
|
|
|
// calling Abort() modifies the data structures we're iterating above.
|
|
|
|
for (PRUint32 index = 0; index < transactions.Length(); index++) {
|
|
|
|
// This can fail, for example if the transaction is in the process of
|
|
|
|
// being comitted. That is expected and fine, so we ignore any returned
|
|
|
|
// errors.
|
|
|
|
transactions[index]->Abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 00:47:36 +03:00
|
|
|
bool
|
|
|
|
TransactionThreadPool::HasTransactionsForDatabase(IDBDatabase* aDatabase)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aDatabase, "Null pointer!");
|
|
|
|
|
|
|
|
// Get list of transactions for this database id
|
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
|
|
|
if (!mTransactionsInProgress.Get(aDatabase->Id(), &dbTransactionInfo)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<TransactionInfo>& transactionsInProgress =
|
|
|
|
dbTransactionInfo->transactions;
|
|
|
|
|
|
|
|
PRUint32 transactionCount = transactionsInProgress.Length();
|
|
|
|
NS_ASSERTION(transactionCount, "Should never be 0!");
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < transactionCount; index++) {
|
|
|
|
// See if any transaction belongs to this IDBDatabase instance
|
|
|
|
if (transactionsInProgress[index].transaction->Database() == aDatabase) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-03 20:33:52 +04:00
|
|
|
bool
|
|
|
|
TransactionThreadPool::MaybeFireCallback(DatabasesCompleteCallback& aCallback)
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-06-03 20:33:52 +04:00
|
|
|
for (PRUint32 index = 0; index < aCallback.mDatabases.Length(); index++) {
|
|
|
|
if (mTransactionsInProgress.Get(aCallback.mDatabases[index]->Id(),
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr)) {
|
2012-06-03 20:33:52 +04:00
|
|
|
return false;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-03 20:33:52 +04:00
|
|
|
aCallback.mCallback->Run();
|
|
|
|
return true;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
TransactionThreadPool::
|
2010-06-28 20:46:21 +04:00
|
|
|
TransactionQueue::TransactionQueue(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
nsIRunnable* aRunnable)
|
2011-11-18 19:21:04 +04:00
|
|
|
: mMonitor("TransactionQueue::mMonitor"),
|
2010-06-23 23:46:08 +04:00
|
|
|
mTransaction(aTransaction),
|
|
|
|
mShouldFinish(false)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aTransaction, "Null pointer!");
|
|
|
|
NS_ASSERTION(aRunnable, "Null pointer!");
|
|
|
|
mQueue.AppendElement(aRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TransactionThreadPool::TransactionQueue::Dispatch(nsIRunnable* aRunnable)
|
|
|
|
{
|
2011-11-18 19:21:04 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!mShouldFinish, "Dispatch called after Finish!");
|
|
|
|
|
2011-11-18 19:21:04 +04:00
|
|
|
mQueue.AppendElement(aRunnable);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-18 19:21:04 +04:00
|
|
|
mMonitor.Notify();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TransactionThreadPool::TransactionQueue::Finish(nsIRunnable* aFinishRunnable)
|
|
|
|
{
|
2011-11-18 19:21:04 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!mShouldFinish, "Finish called more than once!");
|
|
|
|
|
|
|
|
mShouldFinish = true;
|
|
|
|
mFinishRunnable = aFinishRunnable;
|
|
|
|
|
2011-11-18 19:21:04 +04:00
|
|
|
mMonitor.Notify();
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(TransactionThreadPool::TransactionQueue,
|
|
|
|
nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TransactionThreadPool::TransactionQueue::Run()
|
|
|
|
{
|
|
|
|
nsAutoTArray<nsCOMPtr<nsIRunnable>, 10> queue;
|
|
|
|
nsCOMPtr<nsIRunnable> finishRunnable;
|
|
|
|
bool shouldFinish = false;
|
|
|
|
|
|
|
|
while(!shouldFinish) {
|
|
|
|
NS_ASSERTION(queue.IsEmpty(), "Should have cleared this!");
|
|
|
|
|
|
|
|
{
|
2011-11-18 19:21:04 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-06-23 23:46:08 +04:00
|
|
|
while (!mShouldFinish && mQueue.IsEmpty()) {
|
2011-11-18 19:21:04 +04:00
|
|
|
if (NS_FAILED(mMonitor.Wait())) {
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ERROR("Failed to wait!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mQueue.SwapElements(queue);
|
|
|
|
if (mShouldFinish) {
|
|
|
|
mFinishRunnable.swap(finishRunnable);
|
|
|
|
shouldFinish = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 count = queue.Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
|
|
|
nsCOMPtr<nsIRunnable>& runnable = queue[index];
|
|
|
|
runnable->Run();
|
2012-07-30 18:20:58 +04:00
|
|
|
runnable = nullptr;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
queue.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> finishTransactionRunnable =
|
|
|
|
new FinishTransactionRunnable(mTransaction, finishRunnable);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(finishTransactionRunnable,
|
|
|
|
NS_DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to dispatch finishTransactionRunnable!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FinishTransactionRunnable::FinishTransactionRunnable(
|
2010-06-28 20:46:21 +04:00
|
|
|
IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
nsCOMPtr<nsIRunnable>& aFinishRunnable)
|
|
|
|
: mTransaction(aTransaction)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aTransaction, "Null pointer!");
|
|
|
|
mFinishRunnable.swap(aFinishRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(FinishTransactionRunnable, nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FinishTransactionRunnable::Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
if (!gInstance) {
|
|
|
|
NS_ERROR("Running after shutdown!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gInstance->FinishTransaction(mTransaction);
|
|
|
|
|
|
|
|
if (mFinishRunnable) {
|
|
|
|
mFinishRunnable->Run();
|
2012-07-30 18:20:58 +04:00
|
|
|
mFinishRunnable = nullptr;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|