From d14d2919ade7b01c2c15a28509ade2e32b5a4076 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Thu, 20 Jul 2023 17:39:03 +0000 Subject: [PATCH] Bug 1843568 - Part 3: Annotate `nsIEventTarget` as rust_sync, r=xpcom-reviewers,barret All event targets should be threadsafe and implemented in C++, and so should be able to be used in `Sync` types in Rust code. This also required annotating all interfaces deriving from `nsIEventTarget`, as well as adding some associated constants to specific types to indicate to the static assertion that they have threadsafe reference counts. Differential Revision: https://phabricator.services.mozilla.com/D183592 --- mfbt/RefCounted.h | 4 ++++ mfbt/ThreadSafeWeakPtr.h | 2 ++ xpcom/threads/SharedThreadPool.h | 2 ++ xpcom/threads/nsIEventTarget.idl | 2 +- xpcom/threads/nsISerialEventTarget.idl | 2 +- xpcom/threads/nsIThread.idl | 2 +- xpcom/threads/nsIThreadInternal.idl | 2 +- xpcom/threads/nsIThreadPool.idl | 2 +- 8 files changed, 13 insertions(+), 5 deletions(-) diff --git a/mfbt/RefCounted.h b/mfbt/RefCounted.h index e0458ac6bcb5..5c083f3524ff 100644 --- a/mfbt/RefCounted.h +++ b/mfbt/RefCounted.h @@ -10,6 +10,7 @@ #define mozilla_RefCounted_h #include +#include #include "mozilla/AlreadyAddRefed.h" #include "mozilla/Assertions.h" @@ -256,6 +257,9 @@ class RefCounted { } } + using HasThreadSafeRefCnt = + std::integral_constant; + // Compatibility with wtf::RefPtr. void ref() { AddRef(); } void deref() { Release(); } diff --git a/mfbt/ThreadSafeWeakPtr.h b/mfbt/ThreadSafeWeakPtr.h index c3121d5981bd..d5176f5ffa2a 100644 --- a/mfbt/ThreadSafeWeakPtr.h +++ b/mfbt/ThreadSafeWeakPtr.h @@ -181,6 +181,8 @@ class SupportsThreadSafeWeakPtr : public detail::SupportsThreadSafeWeakPtrBase { return cnt; } + using HasThreadSafeRefCnt = std::true_type; + // Compatibility with wtf::RefPtr void ref() { AddRef(); } void deref() { Release(); } diff --git a/xpcom/threads/SharedThreadPool.h b/xpcom/threads/SharedThreadPool.h index 5c29ba11f2e6..f2c706805125 100644 --- a/xpcom/threads/SharedThreadPool.h +++ b/xpcom/threads/SharedThreadPool.h @@ -8,6 +8,7 @@ #define SharedThreadPool_h_ #include +#include #include "mozilla/AlreadyAddRefed.h" #include "mozilla/RefCountType.h" #include "nsCOMPtr.h" @@ -52,6 +53,7 @@ class SharedThreadPool : public nsIThreadPool { NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override; NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override; NS_IMETHOD_(MozExternalRefCountType) Release(void) override; + using HasThreadSafeRefCnt = std::true_type; // Forward behaviour to wrapped thread pool implementation. NS_FORWARD_SAFE_NSITHREADPOOL(mPool); diff --git a/xpcom/threads/nsIEventTarget.idl b/xpcom/threads/nsIEventTarget.idl index 2139dc5ab385..edd49f306df5 100644 --- a/xpcom/threads/nsIEventTarget.idl +++ b/xpcom/threads/nsIEventTarget.idl @@ -17,7 +17,7 @@ class nsITargetShutdownTask; native alreadyAddRefed_nsIRunnable(already_AddRefed); [ptr] native nsITargetShutdownTask(nsITargetShutdownTask); -[builtinclass, scriptable, uuid(a03b8b63-af8b-4164-b0e5-c41e8b2b7cfa)] +[builtinclass, scriptable, rust_sync, uuid(a03b8b63-af8b-4164-b0e5-c41e8b2b7cfa)] interface nsIEventTarget : nsISupports { /* until we can get rid of all uses, keep the non-alreadyAddRefed<> version */ diff --git a/xpcom/threads/nsISerialEventTarget.idl b/xpcom/threads/nsISerialEventTarget.idl index 9cf7768b370b..9cc8ff90663a 100644 --- a/xpcom/threads/nsISerialEventTarget.idl +++ b/xpcom/threads/nsISerialEventTarget.idl @@ -21,7 +21,7 @@ * - However, one can "convert" a thread pool into an nsISerialEventTarget * by putting a TaskQueue in front of it. */ -[builtinclass, scriptable, uuid(9f982380-24b4-49f3-88f6-45e2952036c7)] +[builtinclass, scriptable, rust_sync, uuid(9f982380-24b4-49f3-88f6-45e2952036c7)] interface nsISerialEventTarget : nsIEventTarget { }; diff --git a/xpcom/threads/nsIThread.idl b/xpcom/threads/nsIThread.idl index e6735d5d6464..3ba764ac1288 100644 --- a/xpcom/threads/nsIThread.idl +++ b/xpcom/threads/nsIThread.idl @@ -36,7 +36,7 @@ native TimeDuration(mozilla::TimeDuration); * * See nsIThreadManager for the API used to create and locate threads. */ -[builtinclass, scriptable, uuid(5801d193-29d1-4964-a6b7-70eb697ddf2b)] +[builtinclass, scriptable, rust_sync, uuid(5801d193-29d1-4964-a6b7-70eb697ddf2b)] interface nsIThread : nsISerialEventTarget { /** diff --git a/xpcom/threads/nsIThreadInternal.idl b/xpcom/threads/nsIThreadInternal.idl index fca9e711b0aa..ecc0f540f1cf 100644 --- a/xpcom/threads/nsIThreadInternal.idl +++ b/xpcom/threads/nsIThreadInternal.idl @@ -13,7 +13,7 @@ interface nsIThreadObserver; * The XPCOM thread object implements this interface, which allows a consumer * to observe dispatch activity on the thread. */ -[builtinclass, scriptable, uuid(a3a72e5f-71d9-4add-8f30-59a78fb6d5eb)] +[builtinclass, scriptable, rust_sync, uuid(a3a72e5f-71d9-4add-8f30-59a78fb6d5eb)] interface nsIThreadInternal : nsIThread { /** diff --git a/xpcom/threads/nsIThreadPool.idl b/xpcom/threads/nsIThreadPool.idl index 65162779fa84..48b4a465d858 100644 --- a/xpcom/threads/nsIThreadPool.idl +++ b/xpcom/threads/nsIThreadPool.idl @@ -28,7 +28,7 @@ interface nsIThreadPoolListener : nsISupports * anonymous (unnamed) worker threads. An event dispatched to the thread pool * will be run on the next available worker thread. */ -[uuid(76ce99c9-8e43-489a-9789-f27cc4424965)] +[rust_sync, uuid(76ce99c9-8e43-489a-9789-f27cc4424965)] interface nsIThreadPool : nsIEventTarget { /**