Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsCache.h" |
michael@0 | 8 | #include "nsCacheUtils.h" |
michael@0 | 9 | #include "nsThreadUtils.h" |
michael@0 | 10 | |
michael@0 | 11 | using namespace mozilla; |
michael@0 | 12 | |
michael@0 | 13 | class nsDestroyThreadEvent : public nsRunnable { |
michael@0 | 14 | public: |
michael@0 | 15 | nsDestroyThreadEvent(nsIThread *thread) |
michael@0 | 16 | : mThread(thread) |
michael@0 | 17 | {} |
michael@0 | 18 | NS_IMETHOD Run() |
michael@0 | 19 | { |
michael@0 | 20 | mThread->Shutdown(); |
michael@0 | 21 | return NS_OK; |
michael@0 | 22 | } |
michael@0 | 23 | private: |
michael@0 | 24 | nsCOMPtr<nsIThread> mThread; |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | nsShutdownThread::nsShutdownThread(nsIThread *aThread) |
michael@0 | 28 | : mLock("nsShutdownThread.mLock") |
michael@0 | 29 | , mCondVar(mLock, "nsShutdownThread.mCondVar") |
michael@0 | 30 | , mThread(aThread) |
michael@0 | 31 | { |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | nsShutdownThread::~nsShutdownThread() |
michael@0 | 35 | { |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | nsresult |
michael@0 | 39 | nsShutdownThread::Shutdown(nsIThread *aThread) |
michael@0 | 40 | { |
michael@0 | 41 | nsresult rv; |
michael@0 | 42 | nsRefPtr<nsDestroyThreadEvent> ev = new nsDestroyThreadEvent(aThread); |
michael@0 | 43 | rv = NS_DispatchToMainThread(ev); |
michael@0 | 44 | if (NS_FAILED(rv)) { |
michael@0 | 45 | NS_WARNING("Dispatching event in nsShutdownThread::Shutdown failed!"); |
michael@0 | 46 | } |
michael@0 | 47 | return rv; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | nsresult |
michael@0 | 51 | nsShutdownThread::BlockingShutdown(nsIThread *aThread) |
michael@0 | 52 | { |
michael@0 | 53 | nsresult rv; |
michael@0 | 54 | |
michael@0 | 55 | nsRefPtr<nsShutdownThread> st = new nsShutdownThread(aThread); |
michael@0 | 56 | nsCOMPtr<nsIThread> workerThread; |
michael@0 | 57 | |
michael@0 | 58 | rv = NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread)); |
michael@0 | 59 | if (NS_FAILED(rv)) { |
michael@0 | 60 | NS_WARNING("Can't create nsShutDownThread worker thread!"); |
michael@0 | 61 | return rv; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | { |
michael@0 | 65 | MutexAutoLock lock(st->mLock); |
michael@0 | 66 | rv = workerThread->Dispatch(st, NS_DISPATCH_NORMAL); |
michael@0 | 67 | if (NS_FAILED(rv)) { |
michael@0 | 68 | NS_WARNING( |
michael@0 | 69 | "Dispatching event in nsShutdownThread::BlockingShutdown failed!"); |
michael@0 | 70 | } else { |
michael@0 | 71 | st->mCondVar.Wait(); |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | return Shutdown(workerThread); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | NS_IMETHODIMP |
michael@0 | 79 | nsShutdownThread::Run() |
michael@0 | 80 | { |
michael@0 | 81 | MutexAutoLock lock(mLock); |
michael@0 | 82 | mThread->Shutdown(); |
michael@0 | 83 | mCondVar.Notify(); |
michael@0 | 84 | return NS_OK; |
michael@0 | 85 | } |