Make open transactions keep their DB thread alive

This commit is contained in:
Ben Turner 2010-05-20 15:47:34 -07:00
parent 91db4b768c
commit 5cb526715d
5 changed files with 75 additions and 2 deletions

View File

@ -168,6 +168,14 @@ public:
void OnObjectStoreCreated(const ObjectStoreInfo& aInfo);
void OnObjectStoreRemoved(const ObjectStoreInfo& aInfo);
void DisableConnectionThreadTimeout() {
mConnectionThread->DisableIdleTimeout();
}
void EnableConnectionThreadTimeout() {
mConnectionThread->EnableIdleTimeout();
}
protected:
IDBDatabaseRequest();
~IDBDatabaseRequest();

View File

@ -84,6 +84,9 @@ void
IDBTransactionRequest::OnNewRequest()
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
if (!mPendingRequests) {
mDatabase->DisableConnectionThreadTimeout();
}
++mPendingRequests;
}
@ -94,6 +97,7 @@ IDBTransactionRequest::OnRequestFinished()
NS_ASSERTION(mPendingRequests, "Mismatched calls!");
--mPendingRequests;
if (!mPendingRequests) {
mDatabase->EnableConnectionThreadTimeout();
// Commit!
NS_NOTYETIMPLEMENTED("Implement me!");
}

View File

@ -63,7 +63,7 @@ USING_INDEXEDDB_NAMESPACE
namespace {
const PRUint32 kDefaultThreadTimeoutMS = 30000;
const PRUint32 kDefaultThreadTimeoutMS = 5000;
class OpenDatabaseHelper : public AsyncConnectionHelper
{

View File

@ -72,7 +72,8 @@ LazyIdleThread::LazyIdleThread(PRUint32 aIdleTimeoutMS,
mPendingEventCount(0),
mIdleNotificationCount(0),
mShutdown(PR_FALSE),
mThreadIsShuttingDown(PR_FALSE)
mThreadIsShuttingDown(PR_FALSE),
mIdleTimeoutEnabled(PR_TRUE)
{
NS_ASSERTION(mOwningThread, "This should never fail!");
}
@ -98,6 +99,50 @@ LazyIdleThread::SetWeakIdleObserver(nsIObserver* aObserver)
mIdleObserver = aObserver;
}
void
LazyIdleThread::DisableIdleTimeout()
{
ASSERT_OWNING_THREAD();
if (!mIdleTimeoutEnabled) {
return;
}
mIdleTimeoutEnabled = PR_FALSE;
if (mIdleTimer && NS_FAILED(mIdleTimer->Cancel())) {
NS_WARNING("Failed to cancel timer!");
}
MutexAutoLock lock(mMutex);
// Pretend we have a pending event to keep the idle timer from firing.
NS_ASSERTION(mPendingEventCount < PR_UINT32_MAX, "Way too many!");
mPendingEventCount++;
}
void
LazyIdleThread::EnableIdleTimeout()
{
ASSERT_OWNING_THREAD();
if (mIdleTimeoutEnabled) {
return;
}
mIdleTimeoutEnabled = PR_TRUE;
{
MutexAutoLock lock(mMutex);
NS_ASSERTION(mPendingEventCount, "Mismatched calls to observer methods!");
--mPendingEventCount;
}
if (mThread) {
nsCOMPtr<nsIRunnable> runnable(new nsRunnable());
if (NS_FAILED(Dispatch(runnable, NS_DISPATCH_NORMAL))) {
NS_WARNING("Failed to dispatch!");
}
}
}
void
LazyIdleThread::PreDispatch()
{

View File

@ -90,6 +90,17 @@ public:
*/
void SetWeakIdleObserver(nsIObserver* aObserver);
/**
* Disable the idle timeout for this thread. No effect if the timeout is
* already disabled.
*/
void DisableIdleTimeout();
/**
* Enable the idle timeout. No effect if the timeout is already enabled.
*/
void EnableIdleTimeout();
private:
/**
* Calls Shutdown().
@ -191,6 +202,11 @@ private:
* further idle notifications during the shutdown process.
*/
PRPackedBool mThreadIsShuttingDown;
/**
* Whether or not the idle timeout is enabled.
*/
PRPackedBool mIdleTimeoutEnabled;
};
END_INDEXEDDB_NAMESPACE