Skip to content

Commit

Permalink
Bug 802239: Actually shut down storage threads. r=asuth
Browse files Browse the repository at this point in the history
  • Loading branch information
khuey committed Oct 17, 2012
1 parent 3437dd8 commit 45a2bd1
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions storage/src/mozStorageConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,12 @@ class AsyncCloseConnection : public nsRunnable
public:
AsyncCloseConnection(Connection *aConnection,
nsIEventTarget *aCallingThread,
nsIRunnable *aCallbackEvent)
nsIRunnable *aCallbackEvent,
already_AddRefed<nsIThread> aAsyncExecutionThread)
: mConnection(aConnection)
, mCallingThread(aCallingThread)
, mCallbackEvent(aCallbackEvent)
, mAsyncExecutionThread(aAsyncExecutionThread)
{
}

Expand All @@ -350,13 +352,21 @@ class AsyncCloseConnection : public nsRunnable
bool onCallingThread = false;
(void)mCallingThread->IsOnCurrentThread(&onCallingThread);
if (!onCallingThread) {
#ifdef DEBUG
{
bool onAsyncThread = false;
(void)mAsyncExecutionThread->IsOnCurrentThread(&onAsyncThread);
MOZ_ASSERT(onAsyncThread);
}
#endif
(void)mCallingThread->Dispatch(this, NS_DISPATCH_NORMAL);
return NS_OK;
}

(void)mConnection->internalClose();
if (mCallbackEvent)
(void)mCallingThread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
(void)mAsyncExecutionThread->Shutdown();

// Because we have no guarantee that the invocation of this method on the
// asynchronous thread has fully completed (including the Release of the
Expand All @@ -376,6 +386,7 @@ class AsyncCloseConnection : public nsRunnable
nsRefPtr<Connection> mConnection;
nsCOMPtr<nsIEventTarget> mCallingThread;
nsCOMPtr<nsIRunnable> mCallbackEvent;
nsCOMPtr<nsIThread> mAsyncExecutionThread;
};

} // anonymous namespace
Expand All @@ -402,6 +413,8 @@ Connection::Connection(Service *aService,
Connection::~Connection()
{
(void)Close();

MOZ_ASSERT(!mAsyncExecutionThread);
}

NS_IMPL_THREADSAFE_ADDREF(Connection)
Expand Down Expand Up @@ -912,13 +925,17 @@ Connection::AsyncClose(mozIStorageCompletionCallback *aCallback)
nsCOMPtr<nsIRunnable> completeEvent;
if (aCallback) {
completeEvent = newCompletionEvent(aCallback);
NS_ENSURE_TRUE(completeEvent, NS_ERROR_OUT_OF_MEMORY);
}

// Create and dispatch our close event to the background thread.
nsCOMPtr<nsIRunnable> closeEvent =
new AsyncCloseConnection(this, NS_GetCurrentThread(), completeEvent);
NS_ENSURE_TRUE(closeEvent, NS_ERROR_OUT_OF_MEMORY);
nsCOMPtr<nsIRunnable> closeEvent;
{
// We need to lock because we're modifying mAsyncExecutionThread
MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
closeEvent = new AsyncCloseConnection(this, NS_GetCurrentThread(),
completeEvent,
mAsyncExecutionThread.forget());
}

rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
Expand Down

0 comments on commit 45a2bd1

Please sign in to comment.