2013-04-16 19:40:08 +04:00
|
|
|
/* -*- 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"
|
2021-04-30 10:20:24 +03:00
|
|
|
#include "nsIThread.h"
|
2013-04-16 19:40:08 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsDestroyThreadEvent : public Runnable {
|
2013-04-16 19:40:08 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
explicit nsDestroyThreadEvent(nsIThread* thread)
|
|
|
|
: mozilla::Runnable("nsDestroyThreadEvent"), mThread(thread) {}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-04-16 19:40:08 +04:00
|
|
|
mThread->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-04-16 19:40:08 +04:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
nsShutdownThread::nsShutdownThread(nsIThread* aThread)
|
|
|
|
: mozilla::Runnable("nsShutdownThread"),
|
|
|
|
mMonitor("nsShutdownThread.mMonitor"),
|
2016-05-13 09:56:22 +03:00
|
|
|
mShuttingDown(false),
|
2013-04-16 19:40:08 +04:00
|
|
|
mThread(aThread) {}
|
|
|
|
|
|
|
|
nsresult nsShutdownThread::Shutdown(nsIThread* aThread) {
|
|
|
|
nsresult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsDestroyThreadEvent> ev = new nsDestroyThreadEvent(aThread);
|
2013-04-16 19:40:08 +04:00
|
|
|
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;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsShutdownThread> st = new nsShutdownThread(aThread);
|
2013-04-16 19:40:08 +04:00
|
|
|
nsCOMPtr<nsIThread> workerThread;
|
|
|
|
|
|
|
|
rv = NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't create nsShutDownThread worker thread!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-13 09:56:22 +03:00
|
|
|
MonitorAutoLock mon(st->mMonitor);
|
2013-09-20 13:11:25 +04:00
|
|
|
rv = workerThread->Dispatch(st, NS_DISPATCH_NORMAL);
|
2013-04-16 19:40:08 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"Dispatching event in nsShutdownThread::BlockingShutdown failed!");
|
|
|
|
} else {
|
2016-05-13 09:56:22 +03:00
|
|
|
st->mShuttingDown = true;
|
|
|
|
while (st->mShuttingDown) {
|
|
|
|
mon.Wait();
|
|
|
|
}
|
2013-04-16 19:40:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Shutdown(workerThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsShutdownThread::Run() {
|
2016-05-13 09:56:22 +03:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2013-04-16 19:40:08 +04:00
|
|
|
mThread->Shutdown();
|
2016-05-13 09:56:22 +03:00
|
|
|
mShuttingDown = false;
|
|
|
|
mon.Notify();
|
2013-04-16 19:40:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|