no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / xpcom / threads / nsThreadManager.cpp
blob805be6ee640811a2163bc7dc510ffbfe2f6f7f7b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsThreadManager.h"
8 #include "nsThread.h"
9 #include "nsThreadPool.h"
10 #include "nsThreadUtils.h"
11 #include "nsIClassInfoImpl.h"
12 #include "nsExceptionHandler.h"
13 #include "nsTArray.h"
14 #include "nsXULAppAPI.h"
15 #include "nsExceptionHandler.h"
16 #include "mozilla/AbstractThread.h"
17 #include "mozilla/AppShutdown.h"
18 #include "mozilla/ClearOnShutdown.h"
19 #include "mozilla/CycleCollectedJSContext.h" // nsAutoMicroTask
20 #include "mozilla/EventQueue.h"
21 #include "mozilla/InputTaskManager.h"
22 #include "mozilla/Mutex.h"
23 #include "mozilla/NeverDestroyed.h"
24 #include "mozilla/Preferences.h"
25 #include "mozilla/ProfilerMarkers.h"
26 #include "mozilla/SpinEventLoopUntil.h"
27 #include "mozilla/StaticPtr.h"
28 #include "mozilla/TaskQueue.h"
29 #include "mozilla/ThreadEventQueue.h"
30 #include "mozilla/ThreadLocal.h"
31 #include "TaskController.h"
32 #include "ThreadEventTarget.h"
33 #ifdef MOZ_CANARY
34 # include <fcntl.h>
35 # include <unistd.h>
36 #endif
38 #include "MainThreadIdlePeriod.h"
40 using namespace mozilla;
42 static MOZ_THREAD_LOCAL(bool) sTLSIsMainThread;
44 bool NS_IsMainThreadTLSInitialized() { return sTLSIsMainThread.initialized(); }
46 class BackgroundEventTarget final : public nsIEventTarget,
47 public TaskQueueTracker {
48 public:
49 NS_DECL_THREADSAFE_ISUPPORTS
50 NS_DECL_NSIEVENTTARGET_FULL
52 BackgroundEventTarget() = default;
54 nsresult Init();
56 already_AddRefed<TaskQueue> CreateBackgroundTaskQueue(const char* aName);
58 void BeginShutdown(nsTArray<RefPtr<ShutdownPromise>>&);
59 void FinishShutdown();
61 private:
62 ~BackgroundEventTarget() = default;
64 nsCOMPtr<nsIThreadPool> mPool;
65 nsCOMPtr<nsIThreadPool> mIOPool;
68 NS_IMPL_ISUPPORTS(BackgroundEventTarget, nsIEventTarget, TaskQueueTracker)
70 nsresult BackgroundEventTarget::Init() {
71 nsCOMPtr<nsIThreadPool> pool(new nsThreadPool());
72 NS_ENSURE_TRUE(pool, NS_ERROR_FAILURE);
74 nsresult rv = pool->SetName("BackgroundThreadPool"_ns);
75 NS_ENSURE_SUCCESS(rv, rv);
77 // Use potentially more conservative stack size.
78 rv = pool->SetThreadStackSize(nsIThreadManager::kThreadPoolStackSize);
79 NS_ENSURE_SUCCESS(rv, rv);
81 // Thread limit of 2 makes deadlock during synchronous dispatch less likely.
82 rv = pool->SetThreadLimit(2);
83 NS_ENSURE_SUCCESS(rv, rv);
85 rv = pool->SetIdleThreadLimit(1);
86 NS_ENSURE_SUCCESS(rv, rv);
88 // Leave threads alive for up to 5 minutes
89 rv = pool->SetIdleThreadTimeout(300000);
90 NS_ENSURE_SUCCESS(rv, rv);
92 // Initialize the background I/O event target.
93 nsCOMPtr<nsIThreadPool> ioPool(new nsThreadPool());
94 NS_ENSURE_TRUE(pool, NS_ERROR_FAILURE);
96 // The io pool spends a lot of its time blocking on io, so we want to offload
97 // these jobs on a lower priority if available.
98 rv = ioPool->SetQoSForThreads(nsIThread::QOS_PRIORITY_LOW);
99 NS_ENSURE_SUCCESS(
100 rv, rv); // note: currently infallible, keeping this for brevity.
102 rv = ioPool->SetName("BgIOThreadPool"_ns);
103 NS_ENSURE_SUCCESS(rv, rv);
105 // Use potentially more conservative stack size.
106 rv = ioPool->SetThreadStackSize(nsIThreadManager::kThreadPoolStackSize);
107 NS_ENSURE_SUCCESS(rv, rv);
109 // Thread limit of 4 makes deadlock during synchronous dispatch less likely.
110 rv = ioPool->SetThreadLimit(4);
111 NS_ENSURE_SUCCESS(rv, rv);
113 rv = ioPool->SetIdleThreadLimit(1);
114 NS_ENSURE_SUCCESS(rv, rv);
116 // Leave threads alive for up to 5 minutes
117 rv = ioPool->SetIdleThreadTimeout(300000);
118 NS_ENSURE_SUCCESS(rv, rv);
120 pool.swap(mPool);
121 ioPool.swap(mIOPool);
123 return NS_OK;
126 NS_IMETHODIMP_(bool)
127 BackgroundEventTarget::IsOnCurrentThreadInfallible() {
128 return mPool->IsOnCurrentThread() || mIOPool->IsOnCurrentThread();
131 NS_IMETHODIMP
132 BackgroundEventTarget::IsOnCurrentThread(bool* aValue) {
133 bool value = false;
134 if (NS_SUCCEEDED(mPool->IsOnCurrentThread(&value)) && value) {
135 *aValue = value;
136 return NS_OK;
138 return mIOPool->IsOnCurrentThread(aValue);
141 NS_IMETHODIMP
142 BackgroundEventTarget::Dispatch(already_AddRefed<nsIRunnable> aRunnable,
143 uint32_t aFlags) {
144 // We need to be careful here, because if an event is getting dispatched here
145 // from within TaskQueue::Runner::Run, it will be dispatched with
146 // NS_DISPATCH_AT_END, but we might not be running the event on the same
147 // pool, depending on which pool we were on and the dispatch flags. If we
148 // dispatch an event with NS_DISPATCH_AT_END to the wrong pool, the pool
149 // may not process the event in a timely fashion, which can lead to deadlock.
150 uint32_t flags = aFlags & ~NS_DISPATCH_EVENT_MAY_BLOCK;
151 bool mayBlock = bool(aFlags & NS_DISPATCH_EVENT_MAY_BLOCK);
152 nsCOMPtr<nsIThreadPool>& pool = mayBlock ? mIOPool : mPool;
154 // If we're already running on the pool we want to dispatch to, we can
155 // unconditionally add NS_DISPATCH_AT_END to indicate that we shouldn't spin
156 // up a new thread.
158 // Otherwise, we should remove NS_DISPATCH_AT_END so we don't run into issues
159 // like those in the above comment.
160 if (pool->IsOnCurrentThread()) {
161 flags |= NS_DISPATCH_AT_END;
162 } else {
163 flags &= ~NS_DISPATCH_AT_END;
166 return pool->Dispatch(std::move(aRunnable), flags);
169 NS_IMETHODIMP
170 BackgroundEventTarget::DispatchFromScript(nsIRunnable* aRunnable,
171 uint32_t aFlags) {
172 nsCOMPtr<nsIRunnable> runnable(aRunnable);
173 return Dispatch(runnable.forget(), aFlags);
176 NS_IMETHODIMP
177 BackgroundEventTarget::DelayedDispatch(already_AddRefed<nsIRunnable> aRunnable,
178 uint32_t) {
179 nsCOMPtr<nsIRunnable> dropRunnable(aRunnable);
180 return NS_ERROR_NOT_IMPLEMENTED;
183 NS_IMETHODIMP
184 BackgroundEventTarget::RegisterShutdownTask(nsITargetShutdownTask* aTask) {
185 return NS_ERROR_NOT_IMPLEMENTED;
188 NS_IMETHODIMP
189 BackgroundEventTarget::UnregisterShutdownTask(nsITargetShutdownTask* aTask) {
190 return NS_ERROR_NOT_IMPLEMENTED;
193 void BackgroundEventTarget::BeginShutdown(
194 nsTArray<RefPtr<ShutdownPromise>>& promises) {
195 auto queues = GetAllTrackedTaskQueues();
196 for (auto& queue : queues) {
197 promises.AppendElement(queue->BeginShutdown());
201 void BackgroundEventTarget::FinishShutdown() {
202 mPool->Shutdown();
203 mIOPool->Shutdown();
206 already_AddRefed<TaskQueue> BackgroundEventTarget::CreateBackgroundTaskQueue(
207 const char* aName) {
208 return TaskQueue::Create(do_AddRef(this), aName).forget();
211 extern "C" {
212 // This uses the C language linkage because it's exposed to Rust
213 // via the xpcom/rust/moz_task crate.
214 bool NS_IsMainThread() { return sTLSIsMainThread.get(); }
217 void NS_SetMainThread() {
218 if (!sTLSIsMainThread.init()) {
219 MOZ_CRASH();
221 sTLSIsMainThread.set(true);
222 MOZ_ASSERT(NS_IsMainThread());
223 // We initialize the SerialEventTargetGuard's TLS here for simplicity as it
224 // needs to be initialized around the same time you would initialize
225 // sTLSIsMainThread.
226 SerialEventTargetGuard::InitTLS();
227 nsThreadPool::InitTLS();
230 #ifdef DEBUG
232 namespace mozilla {
234 void AssertIsOnMainThread() { MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); }
236 } // namespace mozilla
238 #endif
240 //-----------------------------------------------------------------------------
242 /* static */
243 void nsThreadManager::ReleaseThread(void* aData) {
244 static_cast<nsThread*>(aData)->Release();
247 // statically allocated instance
248 NS_IMETHODIMP_(MozExternalRefCountType)
249 nsThreadManager::AddRef() { return 2; }
250 NS_IMETHODIMP_(MozExternalRefCountType)
251 nsThreadManager::Release() { return 1; }
252 NS_IMPL_CLASSINFO(nsThreadManager, nullptr,
253 nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON,
254 NS_THREADMANAGER_CID)
255 NS_IMPL_QUERY_INTERFACE_CI(nsThreadManager, nsIThreadManager)
256 NS_IMPL_CI_INTERFACE_GETTER(nsThreadManager, nsIThreadManager)
258 //-----------------------------------------------------------------------------
260 /*static*/ nsThreadManager& nsThreadManager::get() {
261 static NeverDestroyed<nsThreadManager> sInstance;
262 return *sInstance;
265 nsThreadManager::nsThreadManager()
266 : mCurThreadIndex(0),
267 mMutex("nsThreadManager::mMutex"),
268 mState(State::eUninit) {}
270 nsThreadManager::~nsThreadManager() = default;
272 nsresult nsThreadManager::Init() {
273 // Child processes need to initialize the thread manager before they
274 // initialize XPCOM in order to set up the crash reporter. This leads to
275 // situations where we get initialized twice.
277 OffTheBooksMutexAutoLock lock(mMutex);
278 if (mState > State::eUninit) {
279 return NS_OK;
283 if (PR_NewThreadPrivateIndex(&mCurThreadIndex, ReleaseThread) == PR_FAILURE) {
284 return NS_ERROR_FAILURE;
287 #ifdef MOZ_CANARY
288 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NONBLOCK;
289 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
290 char* env_var_flag = getenv("MOZ_KILL_CANARIES");
291 sCanaryOutputFD =
292 env_var_flag
293 ? (env_var_flag[0] ? open(env_var_flag, flags, mode) : STDERR_FILENO)
294 : 0;
295 #endif
297 TaskController::Initialize();
299 // Initialize idle handling.
300 nsCOMPtr<nsIIdlePeriod> idlePeriod = new MainThreadIdlePeriod();
301 TaskController::Get()->SetIdleTaskManager(
302 new IdleTaskManager(idlePeriod.forget()));
304 // Create main thread queue that forwards events to TaskController and
305 // construct main thread.
306 UniquePtr<EventQueue> queue = MakeUnique<EventQueue>(true);
308 RefPtr<ThreadEventQueue> synchronizedQueue =
309 new ThreadEventQueue(std::move(queue), true);
311 mMainThread =
312 new nsThread(WrapNotNull(synchronizedQueue), nsThread::MAIN_THREAD,
313 {0, false, false, Some(W3_LONGTASK_BUSY_WINDOW_MS)});
315 nsresult rv = mMainThread->InitCurrentThread();
316 if (NS_FAILED(rv)) {
317 mMainThread = nullptr;
318 return rv;
320 #ifdef MOZ_MEMORY
321 jemalloc_set_main_thread();
322 #endif
324 // Init AbstractThread.
325 AbstractThread::InitTLS();
326 AbstractThread::InitMainThread();
328 // Initialize the background event target.
329 RefPtr<BackgroundEventTarget> target(new BackgroundEventTarget());
331 rv = target->Init();
332 NS_ENSURE_SUCCESS(rv, rv);
335 OffTheBooksMutexAutoLock lock(mMutex);
337 mBackgroundEventTarget = std::move(target);
339 mState = State::eActive;
342 return NS_OK;
345 void nsThreadManager::ShutdownNonMainThreads() {
346 MOZ_ASSERT(NS_IsMainThread(), "shutdown not called from main thread");
348 // Empty the main thread event queue before we begin shutting down threads.
349 NS_ProcessPendingEvents(mMainThread);
351 mMainThread->mEvents->RunShutdownTasks();
353 RefPtr<BackgroundEventTarget> backgroundEventTarget;
355 OffTheBooksMutexAutoLock lock(mMutex);
356 MOZ_ASSERT(mState == State::eActive, "shutdown called multiple times");
357 backgroundEventTarget = mBackgroundEventTarget;
360 nsTArray<RefPtr<ShutdownPromise>> promises;
361 backgroundEventTarget->BeginShutdown(promises);
363 bool taskQueuesShutdown = false;
364 // It's fine to capture everything by reference in the Then handler since it
365 // runs before we exit the nested event loop, thanks to the SpinEventLoopUntil
366 // below.
367 ShutdownPromise::All(mMainThread, promises)->Then(mMainThread, __func__, [&] {
368 backgroundEventTarget->FinishShutdown();
369 taskQueuesShutdown = true;
372 // Wait for task queues to shutdown, so we don't shut down the underlying
373 // threads of the background event target in the block below, thereby
374 // preventing the task queues from emptying, preventing the shutdown promises
375 // from resolving, and prevent anything checking `taskQueuesShutdown` from
376 // working.
377 mozilla::SpinEventLoopUntil(
378 "nsThreadManager::Shutdown"_ns, [&]() { return taskQueuesShutdown; },
379 mMainThread);
382 // Prevent new nsThreads from being created, and collect a list of threads
383 // which need to be shut down.
385 // We don't prevent new thread creation until we've shut down background
386 // task queues, to ensure that they are able to start thread pool threads
387 // for shutdown tasks.
388 nsTArray<RefPtr<nsThread>> threadsToShutdown;
390 OffTheBooksMutexAutoLock lock(mMutex);
391 mState = State::eShutdown;
393 for (auto* thread : mThreadList) {
394 if (thread->ShutdownRequired()) {
395 threadsToShutdown.AppendElement(thread);
400 // It's tempting to walk the list of threads here and tell them each to stop
401 // accepting new events, but that could lead to badness if one of those
402 // threads is stuck waiting for a response from another thread. To do it
403 // right, we'd need some way to interrupt the threads.
405 // Instead, we process events on the current thread while waiting for
406 // threads to shutdown. This means that we have to preserve a mostly
407 // functioning world until such time as the threads exit.
409 // As we're going to be waiting for all asynchronous shutdowns below, we
410 // can begin asynchronously shutting down all XPCOM threads here, rather
411 // than shutting each thread down one-at-a-time.
412 for (const auto& thread : threadsToShutdown) {
413 thread->AsyncShutdown();
417 // NB: It's possible that there are events in the queue that want to *start*
418 // an asynchronous shutdown. But we have already started async shutdown of
419 // the threads above, so there's no need to worry about them. We only have to
420 // wait for all in-flight asynchronous thread shutdowns to complete.
421 mMainThread->WaitForAllAsynchronousShutdowns();
423 // There are no more background threads at this point.
426 void nsThreadManager::ShutdownMainThread() {
427 #ifdef DEBUG
429 OffTheBooksMutexAutoLock lock(mMutex);
430 MOZ_ASSERT(mState == State::eShutdown, "Must have called BeginShutdown");
432 #endif
434 // Do NS_ProcessPendingEvents but with special handling to set
435 // mEventsAreDoomed atomically with the removal of the last event. This means
436 // that PutEvent cannot succeed if the event would be left in the main thread
437 // queue after our final call to NS_ProcessPendingEvents.
438 // See comments in `nsThread::ThreadFunc` for a more detailed explanation.
439 while (true) {
440 if (mMainThread->mEvents->ShutdownIfNoPendingEvents()) {
441 break;
443 NS_ProcessPendingEvents(mMainThread);
446 // Normally thread shutdown clears the observer for the thread, but since the
447 // main thread is special we do it manually here after we're sure all events
448 // have been processed.
449 mMainThread->SetObserver(nullptr);
451 OffTheBooksMutexAutoLock lock(mMutex);
452 mBackgroundEventTarget = nullptr;
455 void nsThreadManager::ReleaseMainThread() {
456 #ifdef DEBUG
458 OffTheBooksMutexAutoLock lock(mMutex);
459 MOZ_ASSERT(mState == State::eShutdown, "Must have called BeginShutdown");
460 MOZ_ASSERT(!mBackgroundEventTarget, "Must have called ShutdownMainThread");
462 #endif
463 MOZ_ASSERT(mMainThread);
465 // Release main thread object.
466 mMainThread = nullptr;
468 // Remove the TLS entry for the main thread.
469 PR_SetThreadPrivate(mCurThreadIndex, nullptr);
472 void nsThreadManager::RegisterCurrentThread(nsThread& aThread) {
473 MOZ_ASSERT(aThread.GetPRThread() == PR_GetCurrentThread(), "bad aThread");
475 aThread.AddRef(); // for TLS entry
476 PR_SetThreadPrivate(mCurThreadIndex, &aThread);
478 #ifdef DEBUG
480 OffTheBooksMutexAutoLock lock(mMutex);
481 MOZ_ASSERT(aThread.isInList(),
482 "Thread was not added to the thread list before registering!");
484 #endif
487 void nsThreadManager::UnregisterCurrentThread(nsThread& aThread) {
488 MOZ_ASSERT(aThread.GetPRThread() == PR_GetCurrentThread(), "bad aThread");
490 PR_SetThreadPrivate(mCurThreadIndex, nullptr);
491 // Ref-count balanced via ReleaseThread
494 // Not to be used for MainThread!
495 nsThread* nsThreadManager::CreateCurrentThread(SynchronizedEventQueue* aQueue) {
496 // Make sure we don't have an nsThread yet.
497 MOZ_ASSERT(!PR_GetThreadPrivate(mCurThreadIndex));
499 if (!AllowNewXPCOMThreads()) {
500 return nullptr;
503 RefPtr<nsThread> thread = new nsThread(
504 WrapNotNull(aQueue), nsThread::NOT_MAIN_THREAD, {.stackSize = 0});
505 if (NS_FAILED(thread->InitCurrentThread())) {
506 return nullptr;
509 return thread.get(); // reference held in TLS
512 nsresult nsThreadManager::DispatchToBackgroundThread(nsIRunnable* aEvent,
513 uint32_t aDispatchFlags) {
514 RefPtr<BackgroundEventTarget> backgroundTarget;
516 OffTheBooksMutexAutoLock lock(mMutex);
517 if (!AllowNewXPCOMThreadsLocked() || !mBackgroundEventTarget) {
518 return NS_ERROR_FAILURE;
520 backgroundTarget = mBackgroundEventTarget;
523 return backgroundTarget->Dispatch(aEvent, aDispatchFlags);
526 already_AddRefed<TaskQueue> nsThreadManager::CreateBackgroundTaskQueue(
527 const char* aName) {
528 RefPtr<BackgroundEventTarget> backgroundTarget;
530 OffTheBooksMutexAutoLock lock(mMutex);
531 if (!AllowNewXPCOMThreadsLocked() || !mBackgroundEventTarget) {
532 return nullptr;
534 backgroundTarget = mBackgroundEventTarget;
537 return backgroundTarget->CreateBackgroundTaskQueue(aName);
540 nsThread* nsThreadManager::GetCurrentThread() {
541 // read thread local storage
542 void* data = PR_GetThreadPrivate(mCurThreadIndex);
543 if (data) {
544 return static_cast<nsThread*>(data);
547 // Keep this function working early during startup or late during shutdown on
548 // the main thread.
549 if (!AllowNewXPCOMThreads() || NS_IsMainThread()) {
550 return nullptr;
553 // OK, that's fine. We'll dynamically create one :-)
555 // We assume that if we're implicitly creating a thread here that it doesn't
556 // want an event queue. Any thread which wants an event queue should
557 // explicitly create its nsThread wrapper.
559 // nsThread::InitCurrentThread() will check AllowNewXPCOMThreads, and return
560 // an error if we're too late in shutdown to create new XPCOM threads.
561 RefPtr<nsThread> thread = new nsThread();
562 if (NS_FAILED(thread->InitCurrentThread())) {
563 return nullptr;
566 return thread.get(); // reference held in TLS
569 bool nsThreadManager::IsNSThread() const {
571 OffTheBooksMutexAutoLock lock(mMutex);
572 if (mState == State::eUninit) {
573 return false;
576 if (auto* thread = (nsThread*)PR_GetThreadPrivate(mCurThreadIndex)) {
577 return thread->EventQueue();
579 return false;
582 NS_IMETHODIMP
583 nsThreadManager::NewNamedThread(
584 const nsACString& aName, nsIThreadManager::ThreadCreationOptions aOptions,
585 nsIThread** aResult) {
586 // Note: can be called from arbitrary threads
588 [[maybe_unused]] TimeStamp startTime = TimeStamp::Now();
590 RefPtr<ThreadEventQueue> queue =
591 new ThreadEventQueue(MakeUnique<EventQueue>());
592 RefPtr<nsThread> thr =
593 new nsThread(WrapNotNull(queue), nsThread::NOT_MAIN_THREAD, aOptions);
595 // Note: nsThread::Init() will check AllowNewXPCOMThreads, and return an
596 // error if we're too late in shutdown to create new XPCOM threads. If we
597 // aren't, the thread will be synchronously added to mThreadList.
598 nsresult rv = thr->Init(aName);
599 if (NS_FAILED(rv)) {
600 return rv;
603 PROFILER_MARKER_TEXT(
604 "NewThread", OTHER,
605 MarkerOptions(MarkerStack::Capture(),
606 MarkerTiming::IntervalUntilNowFrom(startTime)),
607 aName);
608 if (!NS_IsMainThread()) {
609 PROFILER_MARKER_TEXT(
610 "NewThread (non-main thread)", OTHER,
611 MarkerOptions(MarkerStack::Capture(), MarkerThreadId::MainThread(),
612 MarkerTiming::IntervalUntilNowFrom(startTime)),
613 aName);
616 thr.forget(aResult);
617 return NS_OK;
620 NS_IMETHODIMP
621 nsThreadManager::GetMainThread(nsIThread** aResult) {
622 // Keep this functioning during Shutdown
623 if (!mMainThread) {
624 if (!NS_IsMainThread()) {
625 NS_WARNING(
626 "Called GetMainThread but there isn't a main thread and "
627 "we're not the main thread.");
629 return NS_ERROR_NOT_INITIALIZED;
631 NS_ADDREF(*aResult = mMainThread);
632 return NS_OK;
635 NS_IMETHODIMP
636 nsThreadManager::GetCurrentThread(nsIThread** aResult) {
637 // Keep this functioning during Shutdown
638 if (!mMainThread) {
639 return NS_ERROR_NOT_INITIALIZED;
641 *aResult = GetCurrentThread();
642 if (!*aResult) {
643 return NS_ERROR_OUT_OF_MEMORY;
645 NS_ADDREF(*aResult);
646 return NS_OK;
649 NS_IMETHODIMP
650 nsThreadManager::SpinEventLoopUntil(const nsACString& aVeryGoodReasonToDoThis,
651 nsINestedEventLoopCondition* aCondition) {
652 return SpinEventLoopUntilInternal(aVeryGoodReasonToDoThis, aCondition,
653 ShutdownPhase::NotInShutdown);
656 NS_IMETHODIMP
657 nsThreadManager::SpinEventLoopUntilOrQuit(
658 const nsACString& aVeryGoodReasonToDoThis,
659 nsINestedEventLoopCondition* aCondition) {
660 return SpinEventLoopUntilInternal(aVeryGoodReasonToDoThis, aCondition,
661 ShutdownPhase::AppShutdownConfirmed);
664 // statics from SpinEventLoopUntil.h
665 AutoNestedEventLoopAnnotation* AutoNestedEventLoopAnnotation::sCurrent =
666 nullptr;
667 StaticMutex AutoNestedEventLoopAnnotation::sStackMutex;
669 // static from SpinEventLoopUntil.h
670 void AutoNestedEventLoopAnnotation::AnnotateXPCOMSpinEventLoopStack(
671 const nsACString& aStack) {
672 if (aStack.Length() > 0) {
673 nsCString prefixedStack(XRE_GetProcessTypeString());
674 prefixedStack += ": "_ns + aStack;
675 CrashReporter::AnnotateCrashReport(
676 CrashReporter::Annotation::XPCOMSpinEventLoopStack, prefixedStack);
677 } else {
678 CrashReporter::AnnotateCrashReport(
679 CrashReporter::Annotation::XPCOMSpinEventLoopStack, ""_ns);
683 nsresult nsThreadManager::SpinEventLoopUntilInternal(
684 const nsACString& aVeryGoodReasonToDoThis,
685 nsINestedEventLoopCondition* aCondition,
686 ShutdownPhase aShutdownPhaseToCheck) {
687 // XXX: We would want to AssertIsOnMainThread(); but that breaks some GTest.
688 nsCOMPtr<nsINestedEventLoopCondition> condition(aCondition);
689 nsresult rv = NS_OK;
691 if (!mozilla::SpinEventLoopUntil(aVeryGoodReasonToDoThis, [&]() -> bool {
692 // Check if an ongoing shutdown reached our limits.
693 if (aShutdownPhaseToCheck > ShutdownPhase::NotInShutdown &&
694 AppShutdown::GetCurrentShutdownPhase() >= aShutdownPhaseToCheck) {
695 return true;
698 bool isDone = false;
699 rv = condition->IsDone(&isDone);
700 // JS failure should be unusual, but we need to stop and propagate
701 // the error back to the caller.
702 if (NS_FAILED(rv)) {
703 return true;
706 return isDone;
707 })) {
708 // We stopped early for some reason, which is unexpected.
709 return NS_ERROR_UNEXPECTED;
712 // If we exited when the condition told us to, we need to return whether
713 // the condition encountered failure when executing.
714 return rv;
717 NS_IMETHODIMP
718 nsThreadManager::SpinEventLoopUntilEmpty() {
719 nsIThread* thread = NS_GetCurrentThread();
721 while (NS_HasPendingEvents(thread)) {
722 (void)NS_ProcessNextEvent(thread, false);
725 return NS_OK;
728 NS_IMETHODIMP
729 nsThreadManager::GetMainThreadEventTarget(nsIEventTarget** aTarget) {
730 nsCOMPtr<nsIEventTarget> target = GetMainThreadSerialEventTarget();
731 target.forget(aTarget);
732 return NS_OK;
735 NS_IMETHODIMP
736 nsThreadManager::DispatchToMainThread(nsIRunnable* aEvent, uint32_t aPriority,
737 uint8_t aArgc) {
738 // Note: C++ callers should instead use NS_DispatchToMainThread.
739 MOZ_ASSERT(NS_IsMainThread());
741 // Keep this functioning during Shutdown
742 if (NS_WARN_IF(!mMainThread)) {
743 return NS_ERROR_NOT_INITIALIZED;
745 // If aPriority wasn't explicitly passed, that means it should be treated as
746 // PRIORITY_NORMAL.
747 if (aArgc > 0 && aPriority != nsIRunnablePriority::PRIORITY_NORMAL) {
748 nsCOMPtr<nsIRunnable> event(aEvent);
749 return mMainThread->DispatchFromScript(
750 new PrioritizableRunnable(event.forget(), aPriority), 0);
752 return mMainThread->DispatchFromScript(aEvent, 0);
755 class AutoMicroTaskWrapperRunnable final : public Runnable {
756 public:
757 explicit AutoMicroTaskWrapperRunnable(nsIRunnable* aEvent)
758 : Runnable("AutoMicroTaskWrapperRunnable"), mEvent(aEvent) {
759 MOZ_ASSERT(aEvent);
762 private:
763 ~AutoMicroTaskWrapperRunnable() = default;
765 NS_IMETHOD Run() override {
766 nsAutoMicroTask mt;
768 return mEvent->Run();
771 RefPtr<nsIRunnable> mEvent;
774 NS_IMETHODIMP
775 nsThreadManager::DispatchToMainThreadWithMicroTask(nsIRunnable* aEvent,
776 uint32_t aPriority,
777 uint8_t aArgc) {
778 RefPtr<AutoMicroTaskWrapperRunnable> runnable =
779 new AutoMicroTaskWrapperRunnable(aEvent);
781 return DispatchToMainThread(runnable, aPriority, aArgc);
784 void nsThreadManager::EnableMainThreadEventPrioritization() {
785 MOZ_ASSERT(NS_IsMainThread());
786 InputTaskManager::Get()->EnableInputEventPrioritization();
789 void nsThreadManager::FlushInputEventPrioritization() {
790 MOZ_ASSERT(NS_IsMainThread());
791 InputTaskManager::Get()->FlushInputEventPrioritization();
794 void nsThreadManager::SuspendInputEventPrioritization() {
795 MOZ_ASSERT(NS_IsMainThread());
796 InputTaskManager::Get()->SuspendInputEventPrioritization();
799 void nsThreadManager::ResumeInputEventPrioritization() {
800 MOZ_ASSERT(NS_IsMainThread());
801 InputTaskManager::Get()->ResumeInputEventPrioritization();
804 // static
805 bool nsThreadManager::MainThreadHasPendingHighPriorityEvents() {
806 MOZ_ASSERT(NS_IsMainThread());
807 bool retVal = false;
808 if (get().mMainThread) {
809 get().mMainThread->HasPendingHighPriorityEvents(&retVal);
811 return retVal;
814 NS_IMETHODIMP
815 nsThreadManager::IdleDispatchToMainThread(nsIRunnable* aEvent,
816 uint32_t aTimeout) {
817 // Note: C++ callers should instead use NS_DispatchToThreadQueue or
818 // NS_DispatchToCurrentThreadQueue.
819 MOZ_ASSERT(NS_IsMainThread());
821 nsCOMPtr<nsIRunnable> event(aEvent);
822 if (aTimeout) {
823 return NS_DispatchToThreadQueue(event.forget(), aTimeout, mMainThread,
824 EventQueuePriority::Idle);
827 return NS_DispatchToThreadQueue(event.forget(), mMainThread,
828 EventQueuePriority::Idle);
831 NS_IMETHODIMP
832 nsThreadManager::DispatchDirectTaskToCurrentThread(nsIRunnable* aEvent) {
833 NS_ENSURE_STATE(aEvent);
834 nsCOMPtr<nsIRunnable> runnable = aEvent;
835 return GetCurrentThread()->DispatchDirectTask(runnable.forget());
838 bool nsThreadManager::AllowNewXPCOMThreads() {
839 mozilla::OffTheBooksMutexAutoLock lock(mMutex);
840 return AllowNewXPCOMThreadsLocked();