Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Send and Sync for softbuffer types #217

Merged
merged 10 commits into from
Jun 1, 2024
Merged

Implement Send and Sync for softbuffer types #217

merged 10 commits into from
Jun 1, 2024

Conversation

notgull
Copy link
Member

@notgull notgull commented May 8, 2024

Previously, types in softbuffer were !Send and !Sync. However it would
be nice if types could be shared across threads. Therefore I've made the
following changes:

  • Context is Send+Sync iff D is Send+Sync
  • Surface<D, W> is Send iff D is Send+Sync and W is Send
  • Buffer<'x, D, W> is Send iff D if Send+Sync and W is Send

Materially, I've made the following changes across the backends:

  • X11, Wayland and KMS use Arc for their displays instead of Rc.
  • MacOS uses MainThreadBound to secure windowing resources. This
    restriction was already implicitly enforced anyhow.
  • Windows creates a thread specifically for allocating and then deallocating device contexts. This lets us get around the thread affinity problem.
  • Web just isn't Send.

Closes #205, still WIP

Previously, types in softbuffer were !Send and !Sync. However it would
be nice if types could be shared across threads. Therefore I've made the
following changes:

- Context<D> is Send+Sync iff D is Send+Sync
- Surface<D, W> is Send iff D is Send+Sync and W is Send
- Buffer<'x, D, W> is Send iff D if Send+Sync and W is Send

Materially, I've made the following changes across the backends:

- X11, Wayland and KMS use Arc for their displays instead of Rc.
- MacOS uses MainThreadBound to secure windowing resources. This
  restriction was already implicitly enforced anyhow.
- Windows uses a subclassing strategy to manage creating and destroying
  resources. This subclass is applied to windows that are registered
  into softbuffer, and is then used to manage resources.

Closes #205

Signed-off-by: John Nunley <[email protected]>
Signed-off-by: John Nunley <[email protected]>
@notgull
Copy link
Member Author

notgull commented May 8, 2024

@jackpot51 Please advise on Redox failures. I'm not familiar enough with Orbital's internals to know if it is thread safe or not.

@notgull
Copy link
Member Author

notgull commented May 19, 2024

@jackpot51 Friendly reminder. I'm not familiar enough with Orbital to know how to proceed here.

@jackpot51 confirmed in chat that Orbital's types should be thread-safe.

Signed-off-by: John Nunley <[email protected]>
@notgull notgull marked this pull request as ready for review May 19, 2024 23:39
@notgull notgull requested a review from john01dav as a code owner May 19, 2024 23:39
Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

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

CG impl approved with suggestion applied

src/backends/cg.rs Outdated Show resolved Hide resolved
Co-authored-by: Mads Marquart <[email protected]>
Copy link
Member

@jackpot51 jackpot51 left a comment

Choose a reason for hiding this comment

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

Orbital change looks fine

Copy link
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

Confirming that Wasm can't be Send or Sync.

src/lib.rs Outdated Show resolved Hide resolved
@notgull
Copy link
Member Author

notgull commented May 30, 2024

It seems that there are still bugs in the Windows backend. Sometimes sending a message to a window on another thread doesn't work. Standby while I investigate.

This new strategy discards the old subclassing strategy, which led to deadlocks
when the main thread was blocked. Instead we instantiate a new thread
specifically for creating and destroying DC's. Then we set up a channel so that
all DCs are created and then destroyed on that thread.

I am open to suggestions as to ways to avoid allocating the new thread.

Signed-off-by: John Nunley <[email protected]>
@notgull
Copy link
Member Author

notgull commented Jun 1, 2024

It seems that there are still bugs in the Windows backend. Sometimes sending a message to a window on another thread doesn't work. Standby while I investigate.

Okay, I've set up a new strategy for multithreading on Windows. It's a little hacky but it seems to work.

Signed-off-by: John Nunley <[email protected]>
Signed-off-by: John Nunley <[email protected]>
@notgull
Copy link
Member Author

notgull commented Jun 1, 2024

I think we have approval from all platforms, so I'll go ahead and merge this. But I'll wait a week before releasing, that way the people using the master branch directly over Git have a breathing period to find bugs.

@notgull notgull merged commit 56ae973 into master Jun 1, 2024
38 checks passed
@notgull notgull deleted the notgull/send branch June 1, 2024 18:53
@daxpedda daxpedda mentioned this pull request Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Multi-threaded
5 participants