Closed Bug 1843568 Opened 2 years ago Closed 2 years ago

Add an xpidl attribute to mark an interface as 'Sync' in rust

Categories

(Core :: XPCOM, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
117 Branch
Tracking Status
firefox117 --- fixed

People

(Reporter: nika, Assigned: nika)

Details

Attachments

(5 files, 1 obsolete file)

Currently rust-xpcom marks all generated interface types as non-send and non-sync to reflect the fact that these interfaces are likely not safe to send across threads. However, there are some interfaces which are generally safe to send across threads, and should be Send and Sync because all implementations use a threadsafe implementation.

We should have an attribute we can put on the XPCOM interface to indicate that it satisfies Sync requirements, and relaxes the codegen on the Rust side to match. Ideally this would also come with assertions on the C++ side to ensure that every class implementing the interface is actually at least using threadsafe reference counting.

This attribute indicates that all implementations of the interface must
implement the Sync trait in Rust, meaning that they're safe to share between
threads. This will make storing these values in threadsafe types in Rust more
ergonomic.

To implement this, the vtable types were changed to be &'static references
rather than raw pointers. This should be OK as they are always valid non-null
pointers to the VTable, and avoids the need for manual unsafe impls for the
relevant interfaces.

Other interfaces will remain non-send and non-sync due to the marker type.

This adds c++-side assertions to annotated interfaces, requiring them to use a
threadsafe reference count. This ended up being quite gross due to interface
table entries making it difficult to add a static_assert into the code, so it
ended up being done through a constexpr constant which validates before
returning the IID.

Depends on D183590

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.

Depends on D183591

This attribute promises that the interface is threadsafe, and validates it with
some assertions in generated code. As these interfaces were already only
implemented in Rust code, and always threadsafe, the assertions were easy to
add as a example use-case for the new attributes.

I'm not sure if this is a thing we'd want to start using outside of
clang-plugins, as it's very internal-clang-specific. This does appear to
work however.

The logic is derived from the testing framework we use for clang-plugin
testing.

Attachment #9344269 - Attachment is obsolete: true
Pushed by nlayzell@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/8faeb75f1161 Part 1: Add a `rust_sync` attribute to XPIDL, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/3597df96ba38 Part 2: Validate that rust_sync interfaces have threadsafe refcounts in c++, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/50d6b858ee6e Part 3: Annotate `nsIEventTarget` as rust_sync, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/26047645c009 Part 4: Document the rust_sync attribute, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/a4cb1e2b9e3d Part 5: Mark some nsIKeyValue types as rust_sync, r=lina
Pushed by nlayzell@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/0fb686987260 Part 1: Add a `rust_sync` attribute to XPIDL, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/322cee5ec13d Part 2: Validate that rust_sync interfaces have threadsafe refcounts in c++, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/4e50ed5a7a1f Part 3: Annotate `nsIEventTarget` as rust_sync, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/a6691f035a70 Part 4: Document the rust_sync attribute, r=xpcom-reviewers,barret https://hg.mozilla.org/integration/autoland/rev/2aad3896d42a Part 5: Mark some nsIKeyValue types as rust_sync, r=lina
Flags: needinfo?(nika)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: