Skip to content

Make PhysicalMapping safely Send / Sync - #361

Open
martin-hughes wants to merge 2 commits into
rust-osdev:mainfrom
martin-hughes:safer-physical-mapping
Open

martin-hughes wants to merge 2 commits into
rust-osdev:mainfrom
martin-hughes:safer-physical-mapping

Conversation

@martin-hughes

Copy link
Copy Markdown
Contributor

The aim of this PR is to close #324 by getting the correct Send/Sync annotations on relevant types (such as PhysicalMapping and WrappedObject).

This is a work-in-progress. The outstanding work is to figure out what to do with NativeMethod.

As mentioned in #324, PhysicalMapping presented an interface that was not truly Send or Sync due to the public access to the virtual_start: NonNull<T>.

I've made it correct by hiding that field (* although access is still allowed if needed through an unsafe function!) / only allowing access through the deref methods, and I've increased the difficulty of misusing PhysicalMapping and RawPhysicalMapping by making their fields be private.

That would fix @ChocolateLoverRaj's initial request in #324 to have Sync for FixedRegisters.

If WrappedObject is Send + Sync then this would allow Interpreter to truly be Send + Sync, thus fixing @ChocolateLoverRaj's request to be able to store Interpreter globally. However, via Object and NativeMethod, WrappedObject is not Send + Sync.

In this PR I've marked it as though it is - it tries to be, but isn't. For this PR to be ready, those lines really need removing.

Happy to take thoughts/comments from anyone interested: @ChocolateLoverRaj, @IsaacWoods and anyone else.

This is a prerequisite to removing the `pub` modifiers. Without moving
these structs, the fields are visible to the whole crate, which removes
much of the benefit of making the interface safer.
@martin-hughes

Copy link
Copy Markdown
Contributor Author

Definitely should have tested this after rebasing onto the newest main 😭

The internals of `RawPhysicalMapping` and `PhysicalMapping` have been
hidden, and can now only ba accessed via accessors.

This is largely for the purpose of hiding `virtual_start`, to allow for
an interface that can be made Send & Sync. The other fields are hidden
for consistency.
@martin-hughes martin-hughes changed the title Draft: Add correct Send/Sync annotations to relevant types Make PhysicalMapping safely Send / Sync Sep 23, 2026
@martin-hughes

Copy link
Copy Markdown
Contributor Author

I've had a think about it, and decided that the WrappedObject and NativeMethod changes needed would be a bit too much and clutter up this PR. So I've focussed it purely on PhysicalMapping, which also makes FixedRegisters Sync.

Ready for review.

@martin-hughes
martin-hughes marked this pull request as ready for review September 23, 2026 19:52

// If all other types are correctly labelled with Send and/or Sync, then Interpreter should
// naturally become Send/Sync.
assert_impl_all!(Interpreter<NullHandler>: Send, Sync);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably superfluous given that we've added unsafe impls, but I've left it in as a statement of intent / so it won't be forgotten.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

impl Sync for FixedRegisters?

1 participant