fune/xpcom/threads/ThreadEventTarget.h
Nika Layzell 3065034574 Bug 1764119 - Part 2: Don't check gXPCOMThreadsShutdown in ThreadEventTarget::Dispatch, r=xpcom-reviewers,kmag,jstutte
We already have a mechanism for ending threads accepting new messages
when they are shut down, so this only allows tasks started from thread
shutdown tasks during xpcom shutdown to behave consistently.

We already didn't prevent dispatching to the background thread pool at
this time, so it should make little difference there as well, and may
just instead save us from deadlocks where code expects a dispatch to
succeed and it does not during actor shutdown.

This patch both relaxes the check to only be a NS_ASSERTION, and relaxes
it to allow dispatching to the current thread even after
xpcom-shutdown-threads as that thread is definitely still alive.

Differential Revision: https://phabricator.services.mozilla.com/D144592
2022-05-02 20:38:43 +00:00

57 lines
1.7 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_ThreadEventTarget_h
#define mozilla_ThreadEventTarget_h
#include "mozilla/MemoryReporting.h"
#include "mozilla/Mutex.h"
#include "mozilla/SynchronizedEventQueue.h" // for ThreadTargetSink
#include "nsISerialEventTarget.h"
namespace mozilla {
class DelayedRunnable;
// ThreadEventTarget handles the details of posting an event to a thread. It can
// be used with any ThreadTargetSink implementation.
class ThreadEventTarget final : public nsISerialEventTarget {
public:
ThreadEventTarget(ThreadTargetSink* aSink, bool aIsMainThread);
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIEVENTTARGET_FULL
// Disconnects the target so that it can no longer post events.
void Disconnect(const MutexAutoLock& aProofOfLock) {
mSink->Disconnect(aProofOfLock);
}
// Sets the thread for which IsOnCurrentThread returns true to the current
// thread.
void SetCurrentThread(PRThread* aThread);
// Call ClearCurrentThread() before the PRThread is deleted on thread join.
void ClearCurrentThread();
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
size_t n = 0;
if (mSink) {
n += mSink->SizeOfIncludingThis(aMallocSizeOf);
}
return aMallocSizeOf(this) + n;
}
private:
~ThreadEventTarget();
RefPtr<ThreadTargetSink> mSink;
#ifdef DEBUG
bool mIsMainThread;
#endif
};
} // namespace mozilla
#endif // mozilla_ThreadEventTarget_h