From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- netwerk/cache/nsCacheUtils.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 netwerk/cache/nsCacheUtils.cpp (limited to 'netwerk/cache/nsCacheUtils.cpp') diff --git a/netwerk/cache/nsCacheUtils.cpp b/netwerk/cache/nsCacheUtils.cpp new file mode 100644 index 0000000000..8bd9ac7bc2 --- /dev/null +++ b/netwerk/cache/nsCacheUtils.cpp @@ -0,0 +1,78 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsCacheUtils.h" +#include "nsThreadUtils.h" +#include "nsIThread.h" + +using namespace mozilla; + +class nsDestroyThreadEvent : public Runnable { + public: + explicit nsDestroyThreadEvent(nsIThread* thread) + : mozilla::Runnable("nsDestroyThreadEvent"), mThread(thread) {} + NS_IMETHOD Run() override { + mThread->Shutdown(); + return NS_OK; + } + + private: + nsCOMPtr mThread; +}; + +nsShutdownThread::nsShutdownThread(nsIThread* aThread) + : mozilla::Runnable("nsShutdownThread"), + mMonitor("nsShutdownThread.mMonitor"), + mShuttingDown(false), + mThread(aThread) {} + +nsresult nsShutdownThread::Shutdown(nsIThread* aThread) { + nsresult rv; + RefPtr ev = new nsDestroyThreadEvent(aThread); + rv = NS_DispatchToMainThread(ev); + if (NS_FAILED(rv)) { + NS_WARNING("Dispatching event in nsShutdownThread::Shutdown failed!"); + } + return rv; +} + +nsresult nsShutdownThread::BlockingShutdown(nsIThread* aThread) { + nsresult rv; + + RefPtr st = new nsShutdownThread(aThread); + nsCOMPtr workerThread; + + rv = NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread)); + if (NS_FAILED(rv)) { + NS_WARNING("Can't create nsShutDownThread worker thread!"); + return rv; + } + + { + MonitorAutoLock mon(st->mMonitor); + rv = workerThread->Dispatch(st, NS_DISPATCH_NORMAL); + if (NS_FAILED(rv)) { + NS_WARNING( + "Dispatching event in nsShutdownThread::BlockingShutdown failed!"); + } else { + st->mShuttingDown = true; + while (st->mShuttingDown) { + mon.Wait(); + } + } + } + + return Shutdown(workerThread); +} + +NS_IMETHODIMP +nsShutdownThread::Run() { + MonitorAutoLock mon(mMonitor); + mThread->Shutdown(); + mShuttingDown = false; + mon.Notify(); + return NS_OK; +} -- cgit v1.2.3