Skip to content

Commit

Permalink
Bug 1843568 - Part 3: Annotate nsIEventTarget as rust_sync, r=xpcom…
Browse files Browse the repository at this point in the history
…-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
  • Loading branch information
mystor committed Jul 20, 2023
1 parent 5ab65aa commit 820d5f0
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions mfbt/RefCounted.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define mozilla_RefCounted_h

#include <utility>
#include <type_traits>

#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Assertions.h"
Expand Down Expand Up @@ -256,6 +257,9 @@ class RefCounted {
}
}

using HasThreadSafeRefCnt =
std::integral_constant<bool, Atomicity == AtomicRefCount>;

// Compatibility with wtf::RefPtr.
void ref() { AddRef(); }
void deref() { Release(); }
Expand Down
2 changes: 2 additions & 0 deletions mfbt/ThreadSafeWeakPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
2 changes: 2 additions & 0 deletions xpcom/threads/SharedThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define SharedThreadPool_h_

#include <utility>
#include <type_traits>
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/RefCountType.h"
#include "nsCOMPtr.h"
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion xpcom/threads/nsIEventTarget.idl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class nsITargetShutdownTask;
native alreadyAddRefed_nsIRunnable(already_AddRefed<nsIRunnable>);
[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 */
Expand Down
2 changes: 1 addition & 1 deletion xpcom/threads/nsISerialEventTarget.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
};
2 changes: 1 addition & 1 deletion xpcom/threads/nsIThread.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
2 changes: 1 addition & 1 deletion xpcom/threads/nsIThreadInternal.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
2 changes: 1 addition & 1 deletion xpcom/threads/nsIThreadPool.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down

0 comments on commit 820d5f0

Please sign in to comment.