From 32e0f7f6213648449f3a74da1ea897d3f10e74a9 Mon Sep 17 00:00:00 2001 From: ownedbywuigi Date: Mon, 30 Mar 2026 00:30:51 +0100 Subject: [PATCH] Add infallibleInit method to ThreadLocal and AssertOwns methods to Mutex --- mfbt/ThreadLocal.h | 11 +++++++++++ xpcom/glue/Mutex.h | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/mfbt/ThreadLocal.h b/mfbt/ThreadLocal.h index f1f8264e8e..7da87f248c 100644 --- a/mfbt/ThreadLocal.h +++ b/mfbt/ThreadLocal.h @@ -132,6 +132,8 @@ public: MOZ_MUST_USE inline bool init(); + inline void infallibleInit(); + inline T get() const; inline void set(const T aValue); @@ -171,6 +173,15 @@ ThreadLocal::init() #endif } +template +inline void +ThreadLocal::infallibleInit() +{ + if (!init()) { + MOZ_CRASH("unable to initialize ThreadLocal"); + } +} + template inline T ThreadLocal::get() const diff --git a/xpcom/glue/Mutex.h b/xpcom/glue/Mutex.h index 895721f06c..f4556f3315 100644 --- a/xpcom/glue/Mutex.h +++ b/xpcom/glue/Mutex.h @@ -172,6 +172,21 @@ public: mLock->Unlock(); } + // Assert that the current thread owns the lock right now. + void AssertOwns() const + { + mLock->AssertCurrentThreadOwns(); + } + + // Assert that aLock is the lock passed to the constructor and that the + // current thread owns it. Pointer equality alone is not sufficient because + // lock ownership may be temporarily released with BaseAutoUnlock. + void AssertOwns(const T& aLock) const + { + MOZ_ASSERT(&aLock == mLock); + AssertOwns(); + } + private: BaseAutoLock(); BaseAutoLock(BaseAutoLock&);