Commit 5a0ce3dc authored by valenting's avatar valenting
Browse files

Bug 1665836 - Make CacheIOThread::ThreadFunc hold reference to thread. r=kershaw, a=RyanVM

Differential Revision: https://phabricator.services.mozilla.com/D118157
parent 82c90892
......@@ -248,6 +248,10 @@ nsresult CacheIOThread::Init() {
mBlockingIOWatcher = MakeUnique<detail::BlockingIOWatcher>();
}
// Increase the reference count while spawning a new thread.
// If PR_CreateThread succeeds, we will forget this reference and the thread
// will be responsible to release it when it completes.
RefPtr<CacheIOThread> self = this;
mThread =
PR_CreateThread(PR_USER_THREAD, ThreadFunc, this, PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 128 * 1024);
......@@ -255,6 +259,11 @@ nsresult CacheIOThread::Init() {
return NS_ERROR_FAILURE;
}
// IMPORTANT: The thread now owns this reference, so it's important that we
// leak it here, otherwise we'll end up with a bad refcount.
// See the dont_AddRef in ThreadFunc().
Unused << self.forget().take();
return NS_OK;
}
......@@ -412,7 +421,9 @@ void CacheIOThread::ThreadFunc(void* aClosure) {
NS_SetCurrentThreadName("Cache2 I/O");
mozilla::IOInterposer::RegisterCurrentThread();
CacheIOThread* thread = static_cast<CacheIOThread*>(aClosure);
// We hold on to this reference for the duration of the thread.
RefPtr<CacheIOThread> thread =
dont_AddRef(static_cast<CacheIOThread*>(aClosure));
thread->ThreadFunc();
mozilla::IOInterposer::UnregisterCurrentThread();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment