netwerk/cache/nsCacheUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cache/nsCacheUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsCache.h"
    1.11 +#include "nsCacheUtils.h"
    1.12 +#include "nsThreadUtils.h"
    1.13 +
    1.14 +using namespace mozilla;
    1.15 +
    1.16 +class nsDestroyThreadEvent : public nsRunnable {
    1.17 +public:
    1.18 +  nsDestroyThreadEvent(nsIThread *thread)
    1.19 +    : mThread(thread)
    1.20 +  {}
    1.21 +  NS_IMETHOD Run()
    1.22 +  {
    1.23 +    mThread->Shutdown();
    1.24 +    return NS_OK;
    1.25 +  }
    1.26 +private:
    1.27 +  nsCOMPtr<nsIThread> mThread;
    1.28 +};
    1.29 +
    1.30 +nsShutdownThread::nsShutdownThread(nsIThread *aThread)
    1.31 +  : mLock("nsShutdownThread.mLock")
    1.32 +  , mCondVar(mLock, "nsShutdownThread.mCondVar")
    1.33 +  , mThread(aThread)
    1.34 +{
    1.35 +}
    1.36 +
    1.37 +nsShutdownThread::~nsShutdownThread()
    1.38 +{
    1.39 +}
    1.40 +
    1.41 +nsresult
    1.42 +nsShutdownThread::Shutdown(nsIThread *aThread)
    1.43 +{
    1.44 +  nsresult rv;
    1.45 +  nsRefPtr<nsDestroyThreadEvent> ev = new nsDestroyThreadEvent(aThread);
    1.46 +  rv = NS_DispatchToMainThread(ev);
    1.47 +  if (NS_FAILED(rv)) {
    1.48 +    NS_WARNING("Dispatching event in nsShutdownThread::Shutdown failed!");
    1.49 +  }
    1.50 +  return rv;
    1.51 +}
    1.52 +
    1.53 +nsresult
    1.54 +nsShutdownThread::BlockingShutdown(nsIThread *aThread)
    1.55 +{
    1.56 +  nsresult rv;
    1.57 +
    1.58 +  nsRefPtr<nsShutdownThread> st = new nsShutdownThread(aThread);
    1.59 +  nsCOMPtr<nsIThread> workerThread;
    1.60 +
    1.61 +  rv = NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread));
    1.62 +  if (NS_FAILED(rv)) {
    1.63 +    NS_WARNING("Can't create nsShutDownThread worker thread!");
    1.64 +    return rv;
    1.65 +  }
    1.66 +
    1.67 +  {
    1.68 +    MutexAutoLock lock(st->mLock);
    1.69 +    rv = workerThread->Dispatch(st, NS_DISPATCH_NORMAL);
    1.70 +    if (NS_FAILED(rv)) {
    1.71 +      NS_WARNING(
    1.72 +        "Dispatching event in nsShutdownThread::BlockingShutdown failed!");
    1.73 +    } else {
    1.74 +      st->mCondVar.Wait();
    1.75 +    }
    1.76 +  }
    1.77 +
    1.78 +  return Shutdown(workerThread);
    1.79 +}
    1.80 +
    1.81 +NS_IMETHODIMP
    1.82 +nsShutdownThread::Run()
    1.83 +{
    1.84 +  MutexAutoLock lock(mLock);
    1.85 +  mThread->Shutdown();
    1.86 +  mCondVar.Notify();
    1.87 +  return NS_OK;
    1.88 +}

mercurial