Skip to content

Make RegionHandlers be Send + Sync by default#318

Open
martin-hughes wants to merge 1 commit into
rust-osdev:mainfrom
martin-hughes:i301-send-sync-regions
Open

Make RegionHandlers be Send + Sync by default#318
martin-hughes wants to merge 1 commit into
rust-osdev:mainfrom
martin-hughes:i301-send-sync-regions

Conversation

@martin-hughes

Copy link
Copy Markdown
Contributor

This fixes #301 in my preferred way (the Send interpreter remains called Interpreter) but still allows for an easy non-send interpreter as well.

NB: The function signature of install_region_handler changes to require a Box to be passed to it. This was the simplest way of avoiding a compiler error about Sized. Plus it relieves that function of the responsibility to construct a Box 😉

I've done a bunch of testing to make sure this works as expected, but there's no easy way to write "compile fail" tests except as doctests - and the need for a dummy RegionHandler just makes it look very messy. So I assert to you that it works correctly!

Ideally I'd like to remove the unsafe impl blocks for Interpreter, but there's more work to do first (see also #307). This PR addresses probably the most obvious unsafety of pretending that all RegionHandlers are Send + Sync.

Preserve the option to have non Send/Sync RegionHandlers for anyone who
finds that useful.

There is an API change in `install_region_handler` - but region handlers
are not currently supported so this should not be an issue.
Comment thread src/aml/mod.rs
impl<H, R> BaseInterpreter<H, R>
where
H: Handler,
R: RegionHandler + ?Sized,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm I don't think I've ever used this pattern myself where you constrain a generic to be a dyn Trait in an aliasing type like this - does this constrain R to being a single type (i.e. all region handlers in the map have to be of the same type), or does it mean R represents a trait object like before?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f484a5f3198489d5b17de891509b2874

Unfortunately looks like this is the former. The Box does need to contain some kind of trait object to push it into being a fat pointer with a vtable etc.

Overall, I don't think it's an unreasonable requirement for RegionHandlers to be Send+Sync. I wonder if we should just constrain all of them to be?

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.

That's odd... The attached shows it working for me at least. Mind copying this to tests and seeing if it's just something weird in my config? region_handlers.rs.txt (drop the .txt of course)

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.

Oh - on line 18 you mean Baz instead of Bar, plus you need a ?Sized constraint on line 5.

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.

Overall, I don't think it's an unreasonable requirement for RegionHandlers to be Send+Sync. I wonder if we should just constrain all of them to be?

I did wonder about this quite a bit... I think there's a counter-argument of two parts

  1. A micro-kernel system would (presumably) run acpi in a separate process. In which case it might well be single-threaded.
  2. If it is single-threaded, then it may be useful to use non-send types like Rc in a RegionHandler, and since there's a low-cost way for us to enable that, it's worth us enabling it.

But as always I'm open to your thoughts!

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.

Interpreter<H> isn't safely Send or Sync because of dyn RegionHandler

2 participants