2010-06-23 19:46:08 +00: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 11:12:37 +00: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 19:46:08 +00:00
|
|
|
|
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#include "IDBTransaction.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/Move.h"
|
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
2014-09-17 23:36:01 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2014-09-26 23:21:57 +00:00
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsISupportsPriority.h"
|
|
|
|
#include "nsIThreadPool.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
2013-03-16 06:58:50 +00:00
|
|
|
#include "ProfilerHelpers.h"
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
using mozilla::ipc::AssertIsOnBackgroundThread;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t kThreadLimit = 20;
|
|
|
|
const uint32_t kIdleThreadLimit = 5;
|
|
|
|
const uint32_t kIdleThreadTimeoutMs = 30000;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#if defined(DEBUG) || defined(MOZ_ENABLE_PROFILER_SPS)
|
|
|
|
#define BUILD_THREADPOOL_LISTENER
|
|
|
|
#endif
|
2014-09-13 16:12:19 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#ifdef DEBUG
|
2014-09-13 16:12:19 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
const int32_t kDEBUGThreadPriority = nsISupportsPriority::PRIORITY_NORMAL;
|
|
|
|
const uint32_t kDEBUGThreadSleepMS = 0;
|
|
|
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
#ifdef BUILD_THREADPOOL_LISTENER
|
|
|
|
|
|
|
|
class TransactionThreadPoolListener MOZ_FINAL
|
|
|
|
: public nsIThreadPoolListener
|
2013-03-16 06:58:50 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPoolListener()
|
|
|
|
{ }
|
|
|
|
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-03-16 06:58:50 +00:00
|
|
|
|
|
|
|
private:
|
2013-04-25 15:26:58 +00:00
|
|
|
virtual ~TransactionThreadPoolListener()
|
2013-03-16 06:58:50 +00:00
|
|
|
{ }
|
2014-09-26 23:21:57 +00:00
|
|
|
|
|
|
|
NS_DECL_NSITHREADPOOLLISTENER
|
2013-03-16 06:58:50 +00:00
|
|
|
};
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#endif // BUILD_THREADPOOL_LISTENER
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
class TransactionThreadPool::FinishTransactionRunnable MOZ_FINAL
|
|
|
|
: public nsRunnable
|
2014-09-13 16:12:19 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
typedef TransactionThreadPool::FinishCallback FinishCallback;
|
|
|
|
|
|
|
|
nsRefPtr<TransactionThreadPool> mThreadPool;
|
|
|
|
nsRefPtr<FinishCallback> mFinishCallback;
|
|
|
|
uint64_t mTransactionId;
|
|
|
|
const nsCString mDatabaseId;
|
|
|
|
const nsTArray<nsString> mObjectStoreNames;
|
|
|
|
uint16_t mMode;
|
|
|
|
|
2014-09-13 16:12:19 +00:00
|
|
|
public:
|
2014-09-26 23:21:57 +00:00
|
|
|
FinishTransactionRunnable(already_AddRefed<TransactionThreadPool> aThreadPool,
|
|
|
|
uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode,
|
|
|
|
already_AddRefed<FinishCallback> aFinishCallback);
|
|
|
|
|
|
|
|
void
|
|
|
|
Dispatch()
|
|
|
|
{
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
mThreadPool->mOwningThread->Dispatch(this, NS_DISPATCH_NORMAL)));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
|
|
|
~FinishTransactionRunnable()
|
|
|
|
{ }
|
|
|
|
|
2014-09-17 23:36:01 +00:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2014-09-26 23:21:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TransactionThreadPool::DatabaseTransactionInfo MOZ_FINAL
|
|
|
|
{
|
|
|
|
typedef nsClassHashtable<nsUint64HashKey, TransactionInfo>
|
|
|
|
TransactionHashtable;
|
|
|
|
TransactionHashtable transactions;
|
|
|
|
nsClassHashtable<nsStringHashKey, TransactionInfoPair> blockingTransactions;
|
|
|
|
|
|
|
|
DatabaseTransactionInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DatabaseTransactionInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DatabaseTransactionInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(DatabaseTransactionInfo);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TransactionThreadPool::DatabasesCompleteCallback MOZ_FINAL
|
|
|
|
{
|
|
|
|
friend class nsAutoPtr<DatabasesCompleteCallback>;
|
|
|
|
|
|
|
|
nsTArray<nsCString> mDatabaseIds;
|
|
|
|
nsCOMPtr<nsIRunnable> mCallback;
|
|
|
|
|
|
|
|
DatabasesCompleteCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DatabasesCompleteCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~DatabasesCompleteCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(DatabasesCompleteCallback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TransactionThreadPool::TransactionQueue MOZ_FINAL
|
|
|
|
: public nsRunnable
|
|
|
|
{
|
|
|
|
Monitor mMonitor;
|
|
|
|
|
|
|
|
nsRefPtr<TransactionThreadPool> mOwningThreadPool;
|
|
|
|
uint64_t mTransactionId;
|
|
|
|
const nsCString mDatabaseId;
|
|
|
|
const nsTArray<nsString> mObjectStoreNames;
|
|
|
|
uint16_t mMode;
|
|
|
|
|
|
|
|
nsAutoTArray<nsCOMPtr<nsIRunnable>, 10> mQueue;
|
|
|
|
nsRefPtr<FinishCallback> mFinishCallback;
|
|
|
|
bool mShouldFinish;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TransactionQueue(TransactionThreadPool* aThreadPool,
|
|
|
|
uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
void Unblock();
|
|
|
|
|
|
|
|
void Dispatch(nsIRunnable* aRunnable);
|
2014-09-13 16:12:19 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
void Finish(FinishCallback* aFinishCallback);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-26 23:21:57 +00:00
|
|
|
~TransactionQueue()
|
|
|
|
{ }
|
2014-06-23 19:56:07 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2010-06-23 19:46:08 +00:00
|
|
|
};
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
struct TransactionThreadPool::TransactionInfo MOZ_FINAL
|
|
|
|
{
|
|
|
|
uint64_t transactionId;
|
|
|
|
nsCString databaseId;
|
|
|
|
nsRefPtr<TransactionQueue> queue;
|
|
|
|
nsTHashtable<nsPtrHashKey<TransactionInfo>> blockedOn;
|
|
|
|
nsTHashtable<nsPtrHashKey<TransactionInfo>> blocking;
|
|
|
|
|
|
|
|
TransactionInfo(TransactionThreadPool* aThreadPool,
|
|
|
|
uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode)
|
|
|
|
: transactionId(aTransactionId), databaseId(aDatabaseId)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(TransactionInfo);
|
|
|
|
|
|
|
|
queue = new TransactionQueue(aThreadPool, aTransactionId, aDatabaseId,
|
|
|
|
aObjectStoreNames, aMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
~TransactionInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(TransactionInfo);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TransactionThreadPool::TransactionInfoPair MOZ_FINAL
|
|
|
|
{
|
|
|
|
// Multiple reading transactions can block future writes.
|
|
|
|
nsTArray<TransactionInfo*> lastBlockingWrites;
|
|
|
|
// But only a single writing transaction can block future reads.
|
|
|
|
TransactionInfo* lastBlockingReads;
|
|
|
|
|
|
|
|
TransactionInfoPair()
|
|
|
|
: lastBlockingReads(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(TransactionInfoPair);
|
|
|
|
}
|
|
|
|
|
|
|
|
~TransactionInfoPair()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(TransactionInfoPair);
|
|
|
|
}
|
|
|
|
};
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
TransactionThreadPool::TransactionThreadPool()
|
2014-09-26 23:21:57 +00:00
|
|
|
: mOwningThread(NS_GetCurrentThread())
|
|
|
|
, mNextTransactionId(0)
|
|
|
|
, mShutdownRequested(false)
|
|
|
|
, mShutdownComplete(false)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(mOwningThread);
|
|
|
|
AssertIsOnOwningThread();
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TransactionThreadPool::~TransactionThreadPool()
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mShutdownRequested);
|
|
|
|
MOZ_ASSERT(mShutdownComplete);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#ifdef DEBUG
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
void
|
|
|
|
TransactionThreadPool::AssertIsOnOwningThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningThread);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
bool current;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mOwningThread->IsOnCurrentThread(¤t)));
|
|
|
|
MOZ_ASSERT(current);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#endif // DEBUG
|
|
|
|
|
2014-09-17 23:36:01 +00:00
|
|
|
// static
|
2014-09-26 23:21:57 +00:00
|
|
|
already_AddRefed<TransactionThreadPool>
|
|
|
|
TransactionThreadPool::Create()
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
nsRefPtr<TransactionThreadPool> threadPool = new TransactionThreadPool();
|
|
|
|
threadPool->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(threadPool->Init()))) {
|
|
|
|
threadPool->CleanupAsync();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return threadPool.forget();
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
void
|
2014-09-17 23:36:01 +00:00
|
|
|
TransactionThreadPool::Shutdown()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mShutdownRequested);
|
|
|
|
MOZ_ASSERT(!mShutdownComplete);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
mShutdownRequested = true;
|
2014-09-13 16:12:19 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (!mThreadPool) {
|
|
|
|
MOZ_ASSERT(!mTransactionsInProgress.Count());
|
|
|
|
MOZ_ASSERT(mCompleteCallbacks.IsEmpty());
|
|
|
|
|
|
|
|
mShutdownComplete = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTransactionsInProgress.Count()) {
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mShutdownComplete);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
|
|
|
|
while (!mShutdownComplete) {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_ProcessNextEvent(currentThread));
|
2014-09-13 16:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
// static
|
|
|
|
uint64_t
|
|
|
|
TransactionThreadPool::NextTransactionId()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mNextTransactionId < UINT64_MAX);
|
|
|
|
|
|
|
|
return ++mNextTransactionId;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
nsresult
|
|
|
|
TransactionThreadPool::Init()
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
mThreadPool = do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2012-06-12 17:06:20 +00:00
|
|
|
rv = mThreadPool->SetName(NS_LITERAL_CSTRING("IndexedDB Trans"));
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-06-12 17:06:20 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
rv = mThreadPool->SetThreadLimit(kThreadLimit);
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
rv = mThreadPool->SetIdleThreadLimit(kIdleThreadLimit);
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
rv = mThreadPool->SetIdleThreadTimeout(kIdleThreadTimeoutMs);
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#ifdef BUILD_THREADPOOL_LISTENER
|
2013-03-16 06:58:50 +00:00
|
|
|
nsCOMPtr<nsIThreadPoolListener> listener =
|
|
|
|
new TransactionThreadPoolListener();
|
|
|
|
|
|
|
|
rv = mThreadPool->SetListener(listener);
|
2014-09-26 23:21:57 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif // BUILD_THREADPOOL_LISTENER
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(!kDEBUGThreadSleepMS,
|
|
|
|
"TransactionThreadPool thread debugging enabled, sleeping "
|
|
|
|
"after every event!");
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
void
|
2010-06-23 19:46:08 +00:00
|
|
|
TransactionThreadPool::Cleanup()
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mThreadPool);
|
|
|
|
MOZ_ASSERT(mShutdownRequested);
|
|
|
|
MOZ_ASSERT(!mShutdownComplete);
|
|
|
|
MOZ_ASSERT(!mTransactionsInProgress.Count());
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mThreadPool->Shutdown()));
|
2014-09-13 16:12:19 +00:00
|
|
|
|
2014-09-17 23:36:01 +00:00
|
|
|
if (!mCompleteCallbacks.IsEmpty()) {
|
|
|
|
// Run all callbacks manually now.
|
2014-09-26 23:21:57 +00:00
|
|
|
for (uint32_t count = mCompleteCallbacks.Length(), index = 0;
|
|
|
|
index < count;
|
|
|
|
index++) {
|
|
|
|
nsAutoPtr<DatabasesCompleteCallback>& completeCallback =
|
|
|
|
mCompleteCallbacks[index];
|
|
|
|
MOZ_ASSERT(completeCallback);
|
|
|
|
MOZ_ASSERT(completeCallback->mCallback);
|
|
|
|
|
|
|
|
completeCallback->mCallback->Run();
|
|
|
|
|
|
|
|
completeCallback = nullptr;
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
2014-09-26 23:21:57 +00:00
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
mCompleteCallbacks.Clear();
|
|
|
|
|
|
|
|
// And make sure they get processed.
|
2014-09-26 23:21:57 +00:00
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_ProcessPendingEvents(currentThread)));
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
mShutdownComplete = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TransactionThreadPool::CleanupAsync()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!mShutdownComplete);
|
|
|
|
MOZ_ASSERT(!mTransactionsInProgress.Count());
|
|
|
|
|
|
|
|
mShutdownRequested = true;
|
|
|
|
|
|
|
|
if (!mThreadPool) {
|
|
|
|
MOZ_ASSERT(mCompleteCallbacks.IsEmpty());
|
|
|
|
|
|
|
|
mShutdownComplete = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethod(this, &TransactionThreadPool::Cleanup);
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(runnable)));
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
// static
|
|
|
|
PLDHashOperator
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::MaybeUnblockTransaction(
|
|
|
|
nsPtrHashKey<TransactionInfo>* aKey,
|
|
|
|
void* aUserArg)
|
2013-02-05 17:01:07 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnBackgroundThread();
|
2013-02-05 17:01:07 +00:00
|
|
|
|
|
|
|
TransactionInfo* maybeUnblockedInfo = aKey->GetKey();
|
|
|
|
TransactionInfo* finishedInfo = static_cast<TransactionInfo*>(aUserArg);
|
|
|
|
|
|
|
|
NS_ASSERTION(maybeUnblockedInfo->blockedOn.Contains(finishedInfo),
|
|
|
|
"Huh?");
|
|
|
|
maybeUnblockedInfo->blockedOn.RemoveEntry(finishedInfo);
|
2013-04-10 16:27:00 +00:00
|
|
|
if (!maybeUnblockedInfo->blockedOn.Count()) {
|
2013-02-05 17:01:07 +00:00
|
|
|
// Let this transaction run.
|
|
|
|
maybeUnblockedInfo->queue->Unblock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
void
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::FinishTransaction(
|
|
|
|
uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
2014-09-17 23:36:01 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
PROFILER_LABEL("IndexedDB",
|
|
|
|
"TransactionThreadPool::FinishTransaction",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
2014-09-26 23:21:57 +00:00
|
|
|
if (!mTransactionsInProgress.Get(aDatabaseId, &dbTransactionInfo)) {
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_ERROR("We don't know anyting about this database?!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
DatabaseTransactionInfo::TransactionHashtable& transactionsInProgress =
|
2010-06-23 19:46:08 +00:00
|
|
|
dbTransactionInfo->transactions;
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
uint32_t transactionCount = transactionsInProgress.Count();
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-09-26 23:21:57 +00:00
|
|
|
if (aMode == IDBTransaction::VERSION_CHANGE) {
|
2010-07-12 14:05:01 +00:00
|
|
|
NS_ASSERTION(transactionCount == 1,
|
|
|
|
"More transactions running than should be!");
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-12 14:05:01 +00:00
|
|
|
if (transactionCount == 1) {
|
2010-06-23 19:46:08 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
const TransactionInfo* info = transactionsInProgress.Get(aTransactionId);
|
|
|
|
NS_ASSERTION(info->transactionId == aTransactionId, "Transaction mismatch!");
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
2014-09-26 23:21:57 +00:00
|
|
|
mTransactionsInProgress.Remove(aDatabaseId);
|
2010-10-19 17:58:33 +00:00
|
|
|
|
|
|
|
// See if we need to fire any complete callbacks.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t index = 0;
|
2012-06-03 16:33:52 +00:00
|
|
|
while (index < mCompleteCallbacks.Length()) {
|
|
|
|
if (MaybeFireCallback(mCompleteCallbacks[index])) {
|
|
|
|
mCompleteCallbacks.RemoveElementAt(index);
|
2014-09-26 23:21:57 +00:00
|
|
|
} else {
|
2012-06-03 16:33:52 +00:00
|
|
|
index++;
|
|
|
|
}
|
2010-10-19 17:58:33 +00:00
|
|
|
}
|
2010-07-12 14:05:01 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (mShutdownRequested) {
|
|
|
|
CleanupAsync();
|
|
|
|
}
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-26 23:21:57 +00:00
|
|
|
|
|
|
|
TransactionInfo* info = transactionsInProgress.Get(aTransactionId);
|
2013-02-05 17:01:07 +00:00
|
|
|
NS_ASSERTION(info, "We've never heard of this transaction?!?");
|
2010-07-12 14:05:01 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
const nsTArray<nsString>& objectStoreNames = aObjectStoreNames;
|
2014-05-09 01:03:35 +00:00
|
|
|
for (size_t index = 0, count = objectStoreNames.Length(); index < count;
|
2013-02-05 17:01:07 +00:00
|
|
|
index++) {
|
|
|
|
TransactionInfoPair* blockInfo =
|
|
|
|
dbTransactionInfo->blockingTransactions.Get(objectStoreNames[index]);
|
|
|
|
NS_ASSERTION(blockInfo, "Huh?");
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (aMode == IDBTransaction::READ_WRITE &&
|
2013-02-05 17:01:07 +00:00
|
|
|
blockInfo->lastBlockingReads == info) {
|
|
|
|
blockInfo->lastBlockingReads = nullptr;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 01:03:35 +00:00
|
|
|
size_t i = blockInfo->lastBlockingWrites.IndexOf(info);
|
2013-02-05 17:01:07 +00:00
|
|
|
if (i != blockInfo->lastBlockingWrites.NoIndex) {
|
|
|
|
blockInfo->lastBlockingWrites.RemoveElementAt(i);
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
info->blocking.EnumerateEntries(MaybeUnblockTransaction, info);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
transactionsInProgress.Remove(aTransactionId);
|
2014-09-13 16:12:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::TransactionQueue*
|
|
|
|
TransactionThreadPool::GetQueueForTransaction(uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId)
|
2014-09-13 16:12:19 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aTransactionId <= mNextTransactionId);
|
2014-09-13 16:12:19 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
|
|
|
if (mTransactionsInProgress.Get(aDatabaseId, &dbTransactionInfo)) {
|
|
|
|
DatabaseTransactionInfo::TransactionHashtable& transactionsInProgress =
|
|
|
|
dbTransactionInfo->transactions;
|
|
|
|
TransactionInfo* info = transactionsInProgress.Get(aTransactionId);
|
|
|
|
if (info) {
|
|
|
|
// We recognize this one.
|
|
|
|
return info->queue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionThreadPool::TransactionQueue&
|
|
|
|
TransactionThreadPool::GetQueueForTransaction(
|
|
|
|
uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aTransactionId <= mNextTransactionId);
|
2014-09-17 23:36:01 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionQueue* existingQueue =
|
|
|
|
GetQueueForTransaction(aTransactionId, aDatabaseId);
|
|
|
|
if (existingQueue) {
|
|
|
|
return *existingQueue;
|
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
// See if we can run this transaction now.
|
|
|
|
DatabaseTransactionInfo* dbTransactionInfo;
|
2014-09-26 23:21:57 +00:00
|
|
|
if (!mTransactionsInProgress.Get(aDatabaseId, &dbTransactionInfo)) {
|
2013-02-05 17:01:07 +00:00
|
|
|
// First transaction for this database.
|
|
|
|
dbTransactionInfo = new DatabaseTransactionInfo();
|
2014-09-26 23:21:57 +00:00
|
|
|
mTransactionsInProgress.Put(aDatabaseId, dbTransactionInfo);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
DatabaseTransactionInfo::TransactionHashtable& transactionsInProgress =
|
2010-06-23 19:46:08 +00:00
|
|
|
dbTransactionInfo->transactions;
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionInfo* info = transactionsInProgress.Get(aTransactionId);
|
2013-02-05 17:01:07 +00:00
|
|
|
if (info) {
|
|
|
|
// We recognize this one.
|
|
|
|
return *info->queue;
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionInfo* transactionInfo = new TransactionInfo(this,
|
|
|
|
aTransactionId,
|
|
|
|
aDatabaseId,
|
|
|
|
aObjectStoreNames,
|
|
|
|
aMode);
|
2013-02-05 17:01:07 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
dbTransactionInfo->transactions.Put(aTransactionId, transactionInfo);;
|
2013-02-05 17:01:07 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
for (uint32_t index = 0, count = aObjectStoreNames.Length(); index < count;
|
2013-02-05 17:01:07 +00:00
|
|
|
index++) {
|
|
|
|
TransactionInfoPair* blockInfo =
|
2014-09-26 23:21:57 +00:00
|
|
|
dbTransactionInfo->blockingTransactions.Get(aObjectStoreNames[index]);
|
2013-02-05 17:01:07 +00:00
|
|
|
if (!blockInfo) {
|
|
|
|
blockInfo = new TransactionInfoPair();
|
|
|
|
blockInfo->lastBlockingReads = nullptr;
|
2014-09-26 23:21:57 +00:00
|
|
|
dbTransactionInfo->blockingTransactions.Put(aObjectStoreNames[index],
|
2013-02-05 17:01:07 +00:00
|
|
|
blockInfo);
|
2010-07-12 15:25:49 +00:00
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
// Mark what we are blocking on.
|
|
|
|
if (blockInfo->lastBlockingReads) {
|
|
|
|
TransactionInfo* blockingInfo = blockInfo->lastBlockingReads;
|
|
|
|
transactionInfo->blockedOn.PutEntry(blockingInfo);
|
|
|
|
blockingInfo->blocking.PutEntry(transactionInfo);
|
|
|
|
}
|
2010-07-12 15:25:49 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (aMode == IDBTransaction::READ_WRITE &&
|
2013-02-05 17:01:07 +00:00
|
|
|
blockInfo->lastBlockingWrites.Length()) {
|
|
|
|
for (uint32_t index = 0,
|
|
|
|
count = blockInfo->lastBlockingWrites.Length(); index < count;
|
|
|
|
index++) {
|
|
|
|
TransactionInfo* blockingInfo = blockInfo->lastBlockingWrites[index];
|
|
|
|
transactionInfo->blockedOn.PutEntry(blockingInfo);
|
|
|
|
blockingInfo->blocking.PutEntry(transactionInfo);
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 14:05:01 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (aMode == IDBTransaction::READ_WRITE) {
|
2013-02-05 17:01:07 +00:00
|
|
|
blockInfo->lastBlockingReads = transactionInfo;
|
|
|
|
blockInfo->lastBlockingWrites.Clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
blockInfo->lastBlockingWrites.AppendElement(transactionInfo);
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 14:05:01 +00:00
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
if (!transactionInfo->blockedOn.Count()) {
|
|
|
|
transactionInfo->queue->Unblock();
|
2010-07-12 15:25:49 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
return *transactionInfo->queue;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
void
|
|
|
|
TransactionThreadPool::Dispatch(uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode,
|
2010-06-23 19:46:08 +00:00
|
|
|
nsIRunnable* aRunnable,
|
|
|
|
bool aFinish,
|
2014-09-26 23:21:57 +00:00
|
|
|
FinishCallback* aFinishCallback)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
MOZ_ASSERT(aTransactionId <= mNextTransactionId);
|
|
|
|
MOZ_ASSERT(!mShutdownRequested);
|
|
|
|
|
|
|
|
TransactionQueue& queue = GetQueueForTransaction(aTransactionId,
|
|
|
|
aDatabaseId,
|
|
|
|
aObjectStoreNames,
|
|
|
|
aMode);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
queue.Dispatch(aRunnable);
|
|
|
|
if (aFinish) {
|
|
|
|
queue.Finish(aFinishCallback);
|
2010-09-10 19:12:11 +00:00
|
|
|
}
|
2014-09-26 23:21:57 +00:00
|
|
|
}
|
2010-09-10 19:12:11 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
void
|
|
|
|
TransactionThreadPool::Dispatch(uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
nsIRunnable* aRunnable,
|
|
|
|
bool aFinish,
|
|
|
|
FinishCallback* aFinishCallback)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTransactionId <= mNextTransactionId);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionQueue* queue = GetQueueForTransaction(aTransactionId, aDatabaseId);
|
|
|
|
MOZ_ASSERT(queue, "Passed an invalid transaction id!");
|
|
|
|
|
|
|
|
queue->Dispatch(aRunnable);
|
2010-06-23 19:46:08 +00:00
|
|
|
if (aFinish) {
|
2014-09-26 23:21:57 +00:00
|
|
|
queue->Finish(aFinishCallback);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:13:17 +00:00
|
|
|
void
|
|
|
|
TransactionThreadPool::WaitForDatabasesToComplete(
|
2014-09-26 23:21:57 +00:00
|
|
|
nsTArray<nsCString>& aDatabaseIds,
|
|
|
|
nsIRunnable* aCallback)
|
2010-09-10 19:12:11 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
NS_ASSERTION(!aDatabaseIds.IsEmpty(), "No databases to wait on!");
|
2010-10-19 17:58:33 +00:00
|
|
|
NS_ASSERTION(aCallback, "Null pointer!");
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
nsAutoPtr<DatabasesCompleteCallback> callback(
|
|
|
|
new DatabasesCompleteCallback());
|
2010-10-19 17:58:52 +00:00
|
|
|
callback->mCallback = aCallback;
|
2014-09-26 23:21:57 +00:00
|
|
|
callback->mDatabaseIds.SwapElements(aDatabaseIds);
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (!MaybeFireCallback(callback)) {
|
|
|
|
mCompleteCallbacks.AppendElement(callback.forget());
|
2012-06-03 16:33:52 +00:00
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
// static
|
|
|
|
PLDHashOperator
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::CollectTransactions(const uint64_t& aTransactionId,
|
2013-02-05 17:01:07 +00:00
|
|
|
TransactionInfo* aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
nsAutoTArray<TransactionInfo*, 50>* transactionArray =
|
|
|
|
static_cast<nsAutoTArray<TransactionInfo*, 50>*>(aUserArg);
|
|
|
|
transactionArray->AppendElement(aValue);
|
2013-02-05 17:01:07 +00:00
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-04-12 03:20:18 +00:00
|
|
|
struct MOZ_STACK_CLASS TransactionSearchInfo
|
2013-02-05 17:01:07 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
explicit TransactionSearchInfo(const nsACString& aDatabaseId)
|
|
|
|
: databaseId(aDatabaseId)
|
|
|
|
, found(false)
|
2013-02-05 17:01:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
nsCString databaseId;
|
2013-02-05 17:01:07 +00:00
|
|
|
bool found;
|
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
|
|
|
PLDHashOperator
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::FindTransaction(const uint64_t& aTransactionId,
|
2013-02-05 17:01:07 +00:00
|
|
|
TransactionInfo* aValue,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
TransactionSearchInfo* info = static_cast<TransactionSearchInfo*>(aUserArg);
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (aValue->databaseId == info->databaseId) {
|
2013-02-05 17:01:07 +00:00
|
|
|
info->found = true;
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
2014-09-26 23:21:57 +00:00
|
|
|
|
2011-01-27 21:47:36 +00:00
|
|
|
bool
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::HasTransactionsForDatabase(const nsACString& aDatabaseId)
|
2011-01-27 21:47:36 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!aDatabaseId.IsEmpty(), "An empty DatabaseId!");
|
2011-01-27 21:47:36 +00:00
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
DatabaseTransactionInfo* dbTransactionInfo = nullptr;
|
2014-09-26 23:21:57 +00:00
|
|
|
dbTransactionInfo = mTransactionsInProgress.Get(aDatabaseId);
|
2013-02-05 17:01:07 +00:00
|
|
|
if (!dbTransactionInfo) {
|
2011-01-27 21:47:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionSearchInfo info(aDatabaseId);
|
2013-02-05 17:01:07 +00:00
|
|
|
dbTransactionInfo->transactions.EnumerateRead(FindTransaction, &info);
|
2011-01-27 21:47:36 +00:00
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
return info.found;
|
2011-01-27 21:47:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-03 16:33:52 +00:00
|
|
|
bool
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::MaybeFireCallback(DatabasesCompleteCallback* aCallback)
|
2010-10-19 17:58:52 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
MOZ_ASSERT(!aCallback->mDatabaseIds.IsEmpty());
|
|
|
|
MOZ_ASSERT(aCallback->mCallback);
|
2010-10-19 17:58:52 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
PROFILER_LABEL("IndexedDB",
|
|
|
|
"TransactionThreadPool::MaybeFireCallback",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
for (uint32_t count = aCallback->mDatabaseIds.Length(), index = 0;
|
|
|
|
index < count;
|
|
|
|
index++) {
|
|
|
|
const nsCString& databaseId = aCallback->mDatabaseIds[index];
|
|
|
|
MOZ_ASSERT(!databaseId.IsEmpty());
|
2013-04-09 22:08:28 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (mTransactionsInProgress.Get(databaseId, nullptr)) {
|
2012-06-03 16:33:52 +00:00
|
|
|
return false;
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
aCallback->mCallback->Run();
|
2012-06-03 16:33:52 +00:00
|
|
|
return true;
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
TransactionThreadPool::
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionQueue::TransactionQueue(TransactionThreadPool* aThreadPool,
|
|
|
|
uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode)
|
2011-11-18 15:21:04 +00:00
|
|
|
: mMonitor("TransactionQueue::mMonitor"),
|
2014-09-26 23:21:57 +00:00
|
|
|
mOwningThreadPool(aThreadPool),
|
|
|
|
mTransactionId(aTransactionId),
|
|
|
|
mDatabaseId(aDatabaseId),
|
|
|
|
mObjectStoreNames(aObjectStoreNames),
|
|
|
|
mMode(aMode),
|
2010-06-23 19:46:08 +00:00
|
|
|
mShouldFinish(false)
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
MOZ_ASSERT(aThreadPool);
|
|
|
|
aThreadPool->AssertIsOnOwningThread();
|
2013-02-05 17:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TransactionThreadPool::TransactionQueue::Unblock()
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
// NB: Finish may be called before Unblock.
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
mOwningThreadPool->mThreadPool->Dispatch(this, NS_DISPATCH_NORMAL)));
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TransactionThreadPool::TransactionQueue::Dispatch(nsIRunnable* aRunnable)
|
|
|
|
{
|
2011-11-18 15:21:04 +00:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(!mShouldFinish, "Dispatch called after Finish!");
|
|
|
|
|
2011-11-18 15:21:04 +00:00
|
|
|
mQueue.AppendElement(aRunnable);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2011-11-18 15:21:04 +00:00
|
|
|
mMonitor.Notify();
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::TransactionQueue::Finish(FinishCallback* aFinishCallback)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
2011-11-18 15:21:04 +00:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(!mShouldFinish, "Finish called more than once!");
|
|
|
|
|
|
|
|
mShouldFinish = true;
|
2014-09-26 23:21:57 +00:00
|
|
|
mFinishCallback = aFinishCallback;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2011-11-18 15:21:04 +00:00
|
|
|
mMonitor.Notify();
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(TransactionThreadPool::TransactionQueue,
|
|
|
|
nsRunnable)
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TransactionThreadPool::TransactionQueue::Run()
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
PROFILER_LABEL("IndexedDB",
|
|
|
|
"TransactionThreadPool::TransactionQueue""Run",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 06:58:50 +00:00
|
|
|
|
|
|
|
IDB_PROFILER_MARK("IndexedDB Transaction %llu: Beginning database work",
|
|
|
|
"IDBTransaction[%llu] DT Start",
|
|
|
|
mTransaction->GetSerialNumber());
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
nsAutoTArray<nsCOMPtr<nsIRunnable>, 10> queue;
|
2014-09-26 23:21:57 +00:00
|
|
|
nsRefPtr<FinishCallback> finishCallback;
|
2010-06-23 19:46:08 +00:00
|
|
|
bool shouldFinish = false;
|
|
|
|
|
2013-02-05 17:01:07 +00:00
|
|
|
do {
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_ASSERTION(queue.IsEmpty(), "Should have cleared this!");
|
|
|
|
|
|
|
|
{
|
2011-11-18 15:21:04 +00:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-06-23 19:46:08 +00:00
|
|
|
while (!mShouldFinish && mQueue.IsEmpty()) {
|
2011-11-18 15:21:04 +00:00
|
|
|
if (NS_FAILED(mMonitor.Wait())) {
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_ERROR("Failed to wait!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mQueue.SwapElements(queue);
|
|
|
|
if (mShouldFinish) {
|
2014-09-26 23:21:57 +00:00
|
|
|
mFinishCallback.swap(finishCallback);
|
2010-06-23 19:46:08 +00:00
|
|
|
shouldFinish = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = queue.Length();
|
|
|
|
for (uint32_t index = 0; index < count; index++) {
|
2014-09-26 23:21:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (kDEBUGThreadSleepMS) {
|
|
|
|
MOZ_ALWAYS_TRUE(
|
|
|
|
PR_Sleep(PR_MillisecondsToInterval(kDEBUGThreadSleepMS)) ==
|
|
|
|
PR_SUCCESS);
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
nsCOMPtr<nsIRunnable>& runnable = queue[index];
|
|
|
|
runnable->Run();
|
2012-07-30 14:20:58 +00:00
|
|
|
runnable = nullptr;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
queue.Clear();
|
|
|
|
}
|
2013-02-05 17:01:07 +00:00
|
|
|
} while (!shouldFinish);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (kDEBUGThreadSleepMS) {
|
|
|
|
MOZ_ALWAYS_TRUE(
|
|
|
|
PR_Sleep(PR_MillisecondsToInterval(kDEBUGThreadSleepMS)) == PR_SUCCESS);
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2013-03-16 06:58:50 +00:00
|
|
|
IDB_PROFILER_MARK("IndexedDB Transaction %llu: Finished database work",
|
|
|
|
"IDBTransaction[%llu] DT Done",
|
|
|
|
mTransaction->GetSerialNumber());
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
nsRefPtr<FinishTransactionRunnable> finishTransactionRunnable =
|
|
|
|
new FinishTransactionRunnable(mOwningThreadPool.forget(),
|
|
|
|
mTransactionId,
|
|
|
|
mDatabaseId,
|
|
|
|
mObjectStoreNames,
|
|
|
|
mMode,
|
|
|
|
finishCallback.forget());
|
|
|
|
finishTransactionRunnable->Dispatch();
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::
|
2010-06-23 19:46:08 +00:00
|
|
|
FinishTransactionRunnable::FinishTransactionRunnable(
|
2014-09-26 23:21:57 +00:00
|
|
|
already_AddRefed<TransactionThreadPool> aThreadPool,
|
|
|
|
uint64_t aTransactionId,
|
|
|
|
const nsACString& aDatabaseId,
|
|
|
|
const nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
uint16_t aMode,
|
|
|
|
already_AddRefed<FinishCallback> aFinishCallback)
|
|
|
|
: mThreadPool(Move(aThreadPool)),
|
|
|
|
mFinishCallback(aFinishCallback),
|
|
|
|
mTransactionId(aTransactionId),
|
|
|
|
mDatabaseId(aDatabaseId),
|
|
|
|
mObjectStoreNames(aObjectStoreNames),
|
|
|
|
mMode(aMode)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(TransactionThreadPool::FinishTransactionRunnable,
|
|
|
|
nsRunnable)
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-09-26 23:21:57 +00:00
|
|
|
TransactionThreadPool::
|
2010-06-23 19:46:08 +00:00
|
|
|
FinishTransactionRunnable::Run()
|
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
MOZ_ASSERT(mThreadPool);
|
|
|
|
mThreadPool->AssertIsOnOwningThread();
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
PROFILER_LABEL("IndexedDB",
|
|
|
|
"TransactionThreadPool::FinishTransactionRunnable::Run",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
nsRefPtr<TransactionThreadPool> threadPool;
|
|
|
|
mThreadPool.swap(threadPool);
|
|
|
|
|
|
|
|
nsRefPtr<FinishCallback> callback;
|
|
|
|
mFinishCallback.swap(callback);
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback->TransactionFinishedBeforeUnblock();
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
threadPool->FinishTransaction(mTransactionId,
|
|
|
|
mDatabaseId,
|
|
|
|
mObjectStoreNames,
|
|
|
|
mMode);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
if (callback) {
|
|
|
|
callback->TransactionFinishedAfterUnblock();
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#ifdef BUILD_THREADPOOL_LISTENER
|
2013-03-16 06:58:50 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(TransactionThreadPoolListener, nsIThreadPoolListener)
|
2013-03-16 06:58:50 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TransactionThreadPoolListener::OnThreadCreated()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-09-26 23:21:57 +00:00
|
|
|
|
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
2013-05-28 12:03:38 +00:00
|
|
|
char aLocal;
|
|
|
|
profiler_register_thread("IndexedDB Transaction", &aLocal);
|
2014-09-26 23:21:57 +00:00
|
|
|
#endif // MOZ_ENABLE_PROFILER_SPS
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (kDEBUGThreadPriority != nsISupportsPriority::PRIORITY_NORMAL) {
|
|
|
|
NS_WARNING("TransactionThreadPool thread debugging enabled, priority has "
|
|
|
|
"been modified!");
|
|
|
|
nsCOMPtr<nsISupportsPriority> thread =
|
|
|
|
do_QueryInterface(NS_GetCurrentThread());
|
|
|
|
MOZ_ASSERT(thread);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(thread->SetPriority(kDEBUGThreadPriority)));
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2013-03-16 06:58:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TransactionThreadPoolListener::OnThreadShuttingDown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-09-26 23:21:57 +00:00
|
|
|
|
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
2013-03-16 06:58:50 +00:00
|
|
|
profiler_unregister_thread();
|
2014-09-26 23:21:57 +00:00
|
|
|
#endif // MOZ_ENABLE_PROFILER_SPS
|
|
|
|
|
2013-03-16 06:58:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
#endif // BUILD_THREADPOOL_LISTENER
|
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|