2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
#include "nsThreadUtils.h"
|
2012-06-13 07:08:53 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2016-08-22 15:52:45 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-10-06 05:00:59 +03:00
|
|
|
#include "LeakRefPtr.h"
|
2017-09-25 13:01:25 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2017-10-10 12:59:39 +03:00
|
|
|
#include "nsExceptionHandler.h"
|
2017-10-20 19:36:29 +03:00
|
|
|
#include "nsITimer.h"
|
2019-02-25 19:20:50 +03:00
|
|
|
#include "prsystem.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
# include "nsThreadManager.h"
|
|
|
|
#else
|
|
|
|
# include "nsXPCOMCIDInternal.h"
|
|
|
|
# include "nsIThreadManager.h"
|
|
|
|
# include "nsServiceManagerUtils.h"
|
|
|
|
#endif
|
|
|
|
|
2009-11-09 22:30:01 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
# include <windows.h>
|
2012-07-25 18:25:08 +04:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
# include <sys/resource.h>
|
2009-11-09 22:30:01 +03:00
|
|
|
#endif
|
|
|
|
|
2019-04-03 03:23:22 +03:00
|
|
|
#if defined(ANDROID)
|
|
|
|
# include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-06 05:00:59 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
2016-08-24 17:18:06 +03:00
|
|
|
NS_IMPL_ISUPPORTS(IdlePeriod, nsIIdlePeriod)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IdlePeriod::GetIdlePeriodHint(TimeStamp* aIdleDeadline) {
|
|
|
|
*aIdleDeadline = TimeStamp();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-04 23:59:02 +03:00
|
|
|
// NS_IMPL_NAMED_* relies on the mName field, which is not present on
|
|
|
|
// release or beta. Instead, fall back to using "Runnable" for all
|
|
|
|
// runnables.
|
2018-03-23 21:53:55 +03:00
|
|
|
# ifndef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
|
|
|
NS_IMPL_ISUPPORTS(Runnable, nsIRunnable)
|
2018-01-04 23:59:02 +03:00
|
|
|
# else
|
2017-10-25 23:51:17 +03:00
|
|
|
NS_IMPL_NAMED_ADDREF(Runnable, mName)
|
|
|
|
NS_IMPL_NAMED_RELEASE(Runnable, mName)
|
|
|
|
NS_IMPL_QUERY_INTERFACE(Runnable, nsIRunnable, nsINamed)
|
2018-01-04 23:59:02 +03:00
|
|
|
# endif
|
2012-12-11 22:13:29 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2016-04-26 03:23:21 +03:00
|
|
|
Runnable::Run() {
|
2006-05-10 21:30:15 +04:00
|
|
|
// Do nothing
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2016-11-23 03:18:52 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Runnable::GetName(nsACString& aName) {
|
2017-01-18 03:50:34 +03:00
|
|
|
if (mName) {
|
|
|
|
aName.AssignASCII(mName);
|
|
|
|
} else {
|
|
|
|
aName.Truncate();
|
|
|
|
}
|
2016-11-23 03:18:52 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-03-23 21:53:55 +03:00
|
|
|
# endif
|
2016-11-23 03:18:52 +03:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(CancelableRunnable, Runnable, nsICancelableRunnable)
|
2012-12-11 22:13:29 +04:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult CancelableRunnable::Cancel() {
|
2012-12-11 22:13:29 +04:00
|
|
|
// Do nothing
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:18:33 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(IdleRunnable, CancelableRunnable, nsIIdleRunnable)
|
2016-08-24 17:18:06 +03:00
|
|
|
|
2017-05-19 10:41:24 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(PrioritizableRunnable, Runnable,
|
|
|
|
nsIRunnablePriority)
|
|
|
|
|
|
|
|
PrioritizableRunnable::PrioritizableRunnable(
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable, uint32_t aPriority)
|
|
|
|
// Real runnable name is managed by overridding the GetName function.
|
|
|
|
: Runnable("PrioritizableRunnable"),
|
2018-05-30 22:15:35 +03:00
|
|
|
mRunnable(std::move(aRunnable)),
|
2017-05-19 10:41:24 +03:00
|
|
|
mPriority(aPriority) {
|
|
|
|
# if DEBUG
|
|
|
|
nsCOMPtr<nsIRunnablePriority> runnablePrio = do_QueryInterface(mRunnable);
|
|
|
|
MOZ_ASSERT(!runnablePrio);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2017-05-19 10:41:24 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PrioritizableRunnable::GetName(nsACString& aName) {
|
|
|
|
// Try to get a name from the underlying runnable.
|
|
|
|
nsCOMPtr<nsINamed> named = do_QueryInterface(mRunnable);
|
|
|
|
if (named) {
|
|
|
|
named->GetName(aName);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-03-23 21:53:55 +03:00
|
|
|
# endif
|
2017-05-19 10:41:24 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PrioritizableRunnable::Run() {
|
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
|
|
|
return mRunnable->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PrioritizableRunnable::GetPriority(uint32_t* aPriority) {
|
|
|
|
*aPriority = mPriority;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:38:53 +03:00
|
|
|
already_AddRefed<nsIRunnable> mozilla::CreateMediumHighRunnable(
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new PrioritizableRunnable(
|
|
|
|
std::move(aRunnable), nsIRunnablePriority::PRIORITY_MEDIUMHIGH);
|
|
|
|
return runnable.forget();
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-12-20 16:18:22 +03:00
|
|
|
nsresult NS_NewNamedThread(const nsACString& aName, nsIThread** aResult,
|
|
|
|
nsIRunnable* aEvent, uint32_t aStackSize) {
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2016-12-20 16:18:22 +03:00
|
|
|
nsresult rv = nsThreadManager::get().nsThreadManager::NewNamedThread(
|
|
|
|
aName, aStackSize, getter_AddRefs(thread));
|
2006-05-10 21:30:15 +04:00
|
|
|
#else
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIThreadManager> mgr =
|
2014-06-27 05:35:39 +04:00
|
|
|
do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2016-12-20 16:18:22 +03:00
|
|
|
rv = mgr->NewNamedThread(aName, aStackSize, getter_AddRefs(thread));
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
2014-06-27 05:35:39 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aEvent) {
|
|
|
|
rv = thread->Dispatch(aEvent, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
*aResult = nullptr;
|
|
|
|
thread.swap(*aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult NS_GetCurrentThread(nsIThread** aResult) {
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2016-06-10 09:04:49 +03:00
|
|
|
return nsThreadManager::get().nsThreadManager::GetCurrentThread(aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
#else
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIThreadManager> mgr =
|
2014-06-27 05:35:39 +04:00
|
|
|
do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
return mgr->GetCurrentThread(aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult NS_GetMainThread(nsIThread** aResult) {
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2016-06-10 09:04:49 +03:00
|
|
|
return nsThreadManager::get().nsThreadManager::GetMainThread(aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
#else
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIThreadManager> mgr =
|
2014-06-27 05:35:39 +04:00
|
|
|
do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
return mgr->GetMainThread(aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:00:59 +03:00
|
|
|
nsresult NS_DispatchToCurrentThread(already_AddRefed<nsIRunnable>&& aEvent) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2017-06-01 23:44:20 +03:00
|
|
|
nsIEventTarget* thread = GetCurrentThreadEventTarget();
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!thread) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
#else
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
2015-10-06 05:00:59 +03:00
|
|
|
rv = NS_GetCurrentThread(getter_AddRefs(thread));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
2015-10-06 05:00:59 +03:00
|
|
|
// To keep us from leaking the runnable if dispatch method fails,
|
|
|
|
// we grab the reference on failures and release it.
|
|
|
|
nsIRunnable* temp = event.get();
|
|
|
|
rv = thread->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// Dispatch() leaked the reference to the event, but due to caller's
|
|
|
|
// assumptions, we shouldn't leak here. And given we are on the same
|
|
|
|
// thread as the dispatch target, it's mostly safe to do it here.
|
|
|
|
NS_RELEASE(temp);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is common to call NS_DispatchToCurrentThread with a newly
|
|
|
|
// allocated runnable with a refcount of zero. To keep us from leaking
|
|
|
|
// the runnable if the dispatch method fails, we take a death grip.
|
|
|
|
nsresult NS_DispatchToCurrentThread(nsIRunnable* aEvent) {
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return NS_DispatchToCurrentThread(event.forget());
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
nsresult NS_DispatchToMainThread(already_AddRefed<nsIRunnable>&& aEvent,
|
|
|
|
uint32_t aDispatchFlags) {
|
2018-05-30 22:15:35 +03:00
|
|
|
LeakRefPtr<nsIRunnable> event(std::move(aEvent));
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_ASSERTION(false,
|
|
|
|
"Failed NS_DispatchToMainThread() in shutdown; leaking");
|
2015-07-10 06:21:46 +03:00
|
|
|
// NOTE: if you stop leaking here, adjust Promise::MaybeReportRejected(),
|
|
|
|
// which assumes a leak here, or split into leaks and no-leaks versions
|
2015-10-06 05:00:59 +03:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2015-10-06 05:00:59 +03:00
|
|
|
return thread->Dispatch(event.take(), aDispatchFlags);
|
2015-07-10 06:21:46 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
// In the case of failure with a newly allocated runnable with a
|
|
|
|
// refcount of zero, we intentionally leak the runnable, because it is
|
|
|
|
// likely that the runnable is being dispatched to the main thread
|
|
|
|
// because it owns main thread only objects, so it is not safe to
|
|
|
|
// release them here.
|
2015-07-10 06:21:46 +03:00
|
|
|
nsresult NS_DispatchToMainThread(nsIRunnable* aEvent, uint32_t aDispatchFlags) {
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return NS_DispatchToMainThread(event.forget(), aDispatchFlags);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2016-08-22 15:52:45 +03:00
|
|
|
nsresult NS_DelayedDispatchToCurrentThread(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent, uint32_t aDelayMs) {
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2017-06-01 23:44:20 +03:00
|
|
|
nsIEventTarget* thread = GetCurrentThreadEventTarget();
|
2016-08-22 15:52:45 +03:00
|
|
|
if (!thread) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
rv = NS_GetCurrentThread(getter_AddRefs(thread));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return thread->DelayedDispatch(event.forget(), aDelayMs);
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:18:05 +03:00
|
|
|
nsresult NS_DispatchToThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
|
|
|
|
nsIThread* aThread,
|
|
|
|
EventQueuePriority aQueue) {
|
2016-08-24 17:18:06 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
2017-05-25 00:34:50 +03:00
|
|
|
NS_ENSURE_TRUE(event, NS_ERROR_INVALID_ARG);
|
2017-06-14 11:05:53 +03:00
|
|
|
if (!aThread) {
|
2016-08-24 17:18:06 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
// To keep us from leaking the runnable if dispatch method fails,
|
|
|
|
// we grab the reference on failures and release it.
|
|
|
|
nsIRunnable* temp = event.get();
|
2019-01-26 20:18:05 +03:00
|
|
|
rv = aThread->DispatchToQueue(event.forget(), aQueue);
|
2016-08-24 17:18:06 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// Dispatch() leaked the reference to the event, but due to caller's
|
|
|
|
// assumptions, we shouldn't leak here. And given we are on the same
|
|
|
|
// thread as the dispatch target, it's mostly safe to do it here.
|
|
|
|
NS_RELEASE(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:18:05 +03:00
|
|
|
nsresult NS_DispatchToCurrentThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
|
|
|
|
EventQueuePriority aQueue) {
|
|
|
|
return NS_DispatchToThreadQueue(std::move(aEvent), NS_GetCurrentThread(),
|
|
|
|
aQueue);
|
2017-06-14 11:05:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToMainThreadQueue(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent, EventQueuePriority aQueue) {
|
2018-10-02 00:38:55 +03:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
nsresult rv = NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2019-01-26 20:18:05 +03:00
|
|
|
return NS_DispatchToThreadQueue(std::move(aEvent), mainThread, aQueue);
|
2018-10-02 00:38:55 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-10-22 16:44:50 +03:00
|
|
|
class IdleRunnableWrapper final : public IdleRunnable {
|
2017-05-25 00:34:50 +03:00
|
|
|
public:
|
|
|
|
explicit IdleRunnableWrapper(already_AddRefed<nsIRunnable>&& aEvent)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mRunnable(std::move(aEvent)) {}
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
if (!mRunnable) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
CancelTimer();
|
2020-02-13 17:38:48 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = std::move(mRunnable);
|
2017-05-25 00:34:50 +03:00
|
|
|
return runnable->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TimedOut(nsITimer* aTimer, void* aClosure) {
|
|
|
|
RefPtr<IdleRunnableWrapper> runnable =
|
|
|
|
static_cast<IdleRunnableWrapper*>(aClosure);
|
|
|
|
runnable->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetTimer(uint32_t aDelay, nsIEventTarget* aTarget) override {
|
|
|
|
MOZ_ASSERT(aTarget);
|
|
|
|
MOZ_ASSERT(!mTimer);
|
2017-10-16 09:11:22 +03:00
|
|
|
NS_NewTimerWithFuncCallback(getter_AddRefs(mTimer), TimedOut, this, aDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"IdleRunnableWrapper::SetTimer", aTarget);
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
2017-06-30 00:42:14 +03:00
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
#ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2017-06-30 00:42:14 +03:00
|
|
|
NS_IMETHOD GetName(nsACString& aName) override {
|
|
|
|
aName.AssignLiteral("IdleRunnableWrapper");
|
|
|
|
if (nsCOMPtr<nsINamed> named = do_QueryInterface(mRunnable)) {
|
|
|
|
nsAutoCString name;
|
|
|
|
named->GetName(name);
|
|
|
|
if (!name.IsEmpty()) {
|
|
|
|
aName.AppendLiteral(" for ");
|
|
|
|
aName.Append(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-03-23 21:53:55 +03:00
|
|
|
#endif
|
2017-06-30 00:42:14 +03:00
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
private:
|
|
|
|
~IdleRunnableWrapper() { CancelTimer(); }
|
|
|
|
|
|
|
|
void CancelTimer() {
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
};
|
|
|
|
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
|
|
|
|
uint32_t aTimeout, nsIThread* aThread,
|
|
|
|
EventQueuePriority aQueue) {
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event(std::move(aEvent));
|
2017-05-25 00:34:50 +03:00
|
|
|
NS_ENSURE_TRUE(event, NS_ERROR_INVALID_ARG);
|
2019-01-26 20:18:05 +03:00
|
|
|
MOZ_ASSERT(aQueue == EventQueuePriority::Idle ||
|
|
|
|
aQueue == EventQueuePriority::DeferredTimers);
|
2017-06-16 20:00:04 +03:00
|
|
|
|
|
|
|
// XXX Using current thread for now as the nsIEventTarget.
|
|
|
|
nsIEventTarget* target = mozilla::GetCurrentThreadEventTarget();
|
2017-06-20 20:31:33 +03:00
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2017-06-16 20:00:04 +03:00
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
nsCOMPtr<nsIIdleRunnable> idleEvent = do_QueryInterface(event);
|
|
|
|
|
|
|
|
if (!idleEvent) {
|
|
|
|
idleEvent = new IdleRunnableWrapper(event.forget());
|
|
|
|
event = do_QueryInterface(idleEvent);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(event);
|
|
|
|
}
|
2017-06-16 20:00:04 +03:00
|
|
|
idleEvent->SetTimer(aTimeout, target);
|
2017-05-25 00:34:50 +03:00
|
|
|
|
2019-01-26 20:18:05 +03:00
|
|
|
return NS_DispatchToThreadQueue(event.forget(), aThread, aQueue);
|
2017-06-14 11:05:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToCurrentThreadQueue(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent, uint32_t aTimeout,
|
|
|
|
EventQueuePriority aQueue) {
|
|
|
|
return NS_DispatchToThreadQueue(std::move(aEvent), aTimeout,
|
|
|
|
NS_GetCurrentThread(), aQueue);
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
2007-04-02 19:29:41 +04:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult NS_ProcessPendingEvents(nsIThread* aThread, PRIntervalTime aTimeout) {
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
# ifdef MOZILLA_INTERNAL_API
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aThread) {
|
|
|
|
aThread = NS_GetCurrentThread();
|
|
|
|
if (NS_WARN_IF(!aThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
# else
|
|
|
|
nsCOMPtr<nsIThread> current;
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aThread) {
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = NS_GetCurrentThread(getter_AddRefs(current));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
aThread = current.get();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
PRIntervalTime start = PR_IntervalNow();
|
|
|
|
for (;;) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool processedEvent;
|
2014-06-27 05:35:39 +04:00
|
|
|
rv = aThread->ProcessNextEvent(false, &processedEvent);
|
|
|
|
if (NS_FAILED(rv) || !processedEvent) {
|
2006-05-10 21:30:15 +04:00
|
|
|
break;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
if (PR_IntervalNow() - start > aTimeout) {
|
2006-05-10 21:30:15 +04:00
|
|
|
break;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2007-04-02 19:29:41 +04:00
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
inline bool hasPendingEvents(nsIThread* aThread) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool val;
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_SUCCEEDED(aThread->HasPendingEvents(&val)) && val;
|
2009-01-04 21:59:17 +03:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
bool NS_HasPendingEvents(nsIThread* aThread) {
|
|
|
|
if (!aThread) {
|
2009-01-04 21:59:17 +03:00
|
|
|
#ifndef MOZILLA_INTERNAL_API
|
|
|
|
nsCOMPtr<nsIThread> current;
|
|
|
|
NS_GetCurrentThread(getter_AddRefs(current));
|
|
|
|
return hasPendingEvents(current);
|
|
|
|
#else
|
2014-06-27 05:35:39 +04:00
|
|
|
aThread = NS_GetCurrentThread();
|
|
|
|
if (NS_WARN_IF(!aThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
2009-01-04 21:59:17 +03:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
return hasPendingEvents(aThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
bool NS_ProcessNextEvent(nsIThread* aThread, bool aMayWait) {
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aThread) {
|
|
|
|
aThread = NS_GetCurrentThread();
|
|
|
|
if (NS_WARN_IF(!aThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
nsCOMPtr<nsIThread> current;
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aThread) {
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_GetCurrentThread(getter_AddRefs(current));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (NS_WARN_IF(!current)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
aThread = current.get();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
#endif
|
2011-09-29 10:19:26 +04:00
|
|
|
bool val;
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_SUCCEEDED(aThread->ProcessNextEvent(aMayWait, &val)) && val;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2017-02-07 13:57:23 +03:00
|
|
|
void NS_SetCurrentThreadName(const char* aName) {
|
2019-04-03 03:23:22 +03:00
|
|
|
#if defined(ANDROID)
|
|
|
|
// Workaround for Bug 1541216 - PR_SetCurrentThreadName() Fails to set the
|
|
|
|
// thread name on Android.
|
|
|
|
prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(aName));
|
|
|
|
#else
|
2017-02-07 13:57:23 +03:00
|
|
|
PR_SetCurrentThreadName(aName);
|
2019-04-03 03:23:22 +03:00
|
|
|
#endif
|
2017-02-07 13:57:23 +03:00
|
|
|
CrashReporter::SetCurrentThreadName(aName);
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2014-06-27 05:35:39 +04:00
|
|
|
nsIThread* NS_GetCurrentThread() {
|
2016-06-10 09:04:49 +03:00
|
|
|
return nsThreadManager::get().GetCurrentThread();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2018-01-19 18:41:22 +03:00
|
|
|
|
|
|
|
nsIThread* NS_GetCurrentThreadNoCreate() {
|
|
|
|
if (nsThreadManager::get().IsNSThread()) {
|
|
|
|
return NS_GetCurrentThread();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
2012-06-12 21:06:20 +04:00
|
|
|
|
|
|
|
// nsThreadPoolNaming
|
2016-12-22 02:38:41 +03:00
|
|
|
nsCString nsThreadPoolNaming::GetNextThreadName(const nsACString& aPoolName) {
|
|
|
|
nsCString name(aPoolName);
|
|
|
|
name.AppendLiteral(" #");
|
|
|
|
name.AppendInt(++mCounter, 10); // The counter is declared as atomic
|
|
|
|
return name;
|
|
|
|
}
|
2012-07-25 18:25:08 +04:00
|
|
|
|
2019-11-06 00:19:18 +03:00
|
|
|
nsresult NS_DispatchBackgroundTask(already_AddRefed<nsIRunnable> aEvent,
|
2019-11-26 17:35:02 +03:00
|
|
|
uint32_t aDispatchFlags) {
|
2019-09-25 07:03:28 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
2019-10-06 21:29:55 +03:00
|
|
|
return nsThreadManager::get().DispatchToBackgroundThread(event,
|
|
|
|
aDispatchFlags);
|
2019-09-25 07:03:28 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:19:18 +03:00
|
|
|
nsresult NS_DispatchBackgroundTask(nsIRunnable* aEvent,
|
2019-11-26 17:35:02 +03:00
|
|
|
uint32_t aDispatchFlags) {
|
2019-10-06 21:29:55 +03:00
|
|
|
return nsThreadManager::get().DispatchToBackgroundThread(aEvent,
|
|
|
|
aDispatchFlags);
|
2019-09-25 07:03:28 +03:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:09:19 +03:00
|
|
|
nsresult NS_CreateBackgroundTaskQueue(const char* aName,
|
|
|
|
nsISerialEventTarget** aTarget) {
|
|
|
|
nsCOMPtr<nsISerialEventTarget> target =
|
2020-01-10 00:50:11 +03:00
|
|
|
nsThreadManager::get().CreateBackgroundTaskQueue(aName);
|
2019-12-12 18:09:19 +03:00
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
target.forget(aTarget);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-25 18:25:08 +04:00
|
|
|
// nsAutoLowPriorityIO
|
|
|
|
nsAutoLowPriorityIO::nsAutoLowPriorityIO() {
|
|
|
|
#if defined(XP_WIN)
|
2017-01-12 12:13:55 +03:00
|
|
|
lowIOPrioritySet =
|
|
|
|
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
|
2012-07-25 18:25:08 +04:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
oldPriority = getiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD);
|
|
|
|
lowIOPrioritySet =
|
|
|
|
oldPriority != -1 &&
|
|
|
|
setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD, IOPOL_THROTTLE) != -1;
|
|
|
|
#else
|
|
|
|
lowIOPrioritySet = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoLowPriorityIO::~nsAutoLowPriorityIO() {
|
|
|
|
#if defined(XP_WIN)
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_LIKELY(lowIOPrioritySet)) {
|
2012-07-25 18:25:08 +04:00
|
|
|
// On Windows the old thread priority is automatically restored
|
|
|
|
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
|
|
|
|
}
|
|
|
|
#elif defined(XP_MACOSX)
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_LIKELY(lowIOPrioritySet)) {
|
2012-07-25 18:25:08 +04:00
|
|
|
setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD, oldPriority);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2017-05-03 01:42:02 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-05-23 01:12:43 +03:00
|
|
|
nsIEventTarget* GetCurrentThreadEventTarget() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread->EventTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIEventTarget* GetMainThreadEventTarget() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread->EventTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISerialEventTarget* GetCurrentThreadSerialEventTarget() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread->SerialEventTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISerialEventTarget* GetMainThreadSerialEventTarget() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread->SerialEventTarget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 19:20:50 +03:00
|
|
|
size_t GetNumberOfProcessors() {
|
|
|
|
#if defined(XP_LINUX) && defined(MOZ_SANDBOX)
|
|
|
|
static const PRInt32 procs = PR_GetNumberOfProcessors();
|
|
|
|
#else
|
|
|
|
PRInt32 procs = PR_GetNumberOfProcessors();
|
|
|
|
#endif
|
|
|
|
MOZ_ASSERT(procs > 0);
|
|
|
|
return static_cast<size_t>(procs);
|
|
|
|
}
|
|
|
|
|
2017-05-03 01:42:02 +03:00
|
|
|
} // namespace mozilla
|
2017-05-22 21:26:39 +03:00
|
|
|
|
|
|
|
bool nsIEventTarget::IsOnCurrentThread() {
|
2019-12-12 03:56:53 +03:00
|
|
|
if (mThread) {
|
|
|
|
return mThread == PR_GetCurrentThread();
|
2017-05-22 21:26:39 +03:00
|
|
|
}
|
|
|
|
return IsOnCurrentThreadInfallible();
|
|
|
|
}
|
2019-02-07 19:14:04 +03:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
// These functions use the C language linkage because they're exposed to Rust
|
|
|
|
// via the xpcom/rust/moz_task crate, which wraps them in safe Rust functions
|
|
|
|
// that enable Rust code to get/create threads and dispatch runnables on them.
|
|
|
|
|
|
|
|
nsresult NS_GetCurrentThreadEventTarget(nsIEventTarget** aResult) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target = mozilla::GetCurrentThreadEventTarget();
|
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
target.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_GetMainThreadEventTarget(nsIEventTarget** aResult) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target = mozilla::GetMainThreadEventTarget();
|
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
target.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NS_NewNamedThread's aStackSize parameter has the default argument
|
|
|
|
// nsIThreadManager::DEFAULT_STACK_SIZE, but we can't omit default arguments
|
|
|
|
// when calling a C++ function from Rust, and we can't access
|
|
|
|
// nsIThreadManager::DEFAULT_STACK_SIZE in Rust to pass it explicitly,
|
|
|
|
// since it is defined in a %{C++ ... %} block within nsIThreadManager.idl.
|
|
|
|
// So we indirect through this function.
|
|
|
|
nsresult NS_NewNamedThreadWithDefaultStackSize(const nsACString& aName,
|
|
|
|
nsIThread** aResult,
|
|
|
|
nsIRunnable* aEvent) {
|
|
|
|
return NS_NewNamedThread(aName, aResult, aEvent);
|
|
|
|
}
|
Bug 1482608 - Add owning thread pointer holders for Rust code. r=nika,myk
This commit adds `ThreadPtr{Handle, Holder}` to wrap an `XpCom` object
with thread-safe refcounting. These are analagous to
`nsMainThreadPtr{Handle, Holder}`, but can hold references to
objects from any thread, not just the main thread.
`ThreadPtrHolder` is similar to `ThreadBoundRefPtr`. However, it's
not possible to clone a `ThreadBoundRefPtr`, so it can't be shared
among tasks. This is fine for objects that are only used once, like
callbacks. However, `ThreadBoundRefPtr` doesn't work well for loggers
or event emitters, which might need to be called multiple times on
the owning thread.
Unlike a `ThreadBoundRefPtr`, it's allowed and expected to
clone and drop a `ThreadPtrHolder` on other threads. Internally,
the holder keeps an atomic refcount, and releases the wrapped object
on the owning thread once the count reaches zero.
This commit also changes `TaskRunnable` to support dispatching from
threads other than the main thread.
Differential Revision: https://phabricator.services.mozilla.com/D20074
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:24 +03:00
|
|
|
|
|
|
|
bool NS_IsCurrentThread(nsIEventTarget* aThread) {
|
|
|
|
return aThread->IsOnCurrentThread();
|
2019-02-07 19:14:04 +03:00
|
|
|
}
|
Bug 1482608 - Add owning thread pointer holders for Rust code. r=nika,myk
This commit adds `ThreadPtr{Handle, Holder}` to wrap an `XpCom` object
with thread-safe refcounting. These are analagous to
`nsMainThreadPtr{Handle, Holder}`, but can hold references to
objects from any thread, not just the main thread.
`ThreadPtrHolder` is similar to `ThreadBoundRefPtr`. However, it's
not possible to clone a `ThreadBoundRefPtr`, so it can't be shared
among tasks. This is fine for objects that are only used once, like
callbacks. However, `ThreadBoundRefPtr` doesn't work well for loggers
or event emitters, which might need to be called multiple times on
the owning thread.
Unlike a `ThreadBoundRefPtr`, it's allowed and expected to
clone and drop a `ThreadPtrHolder` on other threads. Internally,
the holder keeps an atomic refcount, and releases the wrapped object
on the owning thread once the count reaches zero.
This commit also changes `TaskRunnable` to support dispatching from
threads other than the main thread.
Differential Revision: https://phabricator.services.mozilla.com/D20074
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:24 +03:00
|
|
|
|
|
|
|
} // extern "C"
|