Skip to content

Move spin to bevy_platform_support out of other crates #17470

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

Merged
merged 6 commits into from
Jan 23, 2025

Conversation

bushrat011899
Copy link
Contributor

Objective

Solution

  • Expanded bevy_platform_support::sync module to provide API-compatible replacements for std items such as RwLock, Mutex, and OnceLock.
  • Removed spin from all crates except bevy_platform_support.

Testing

  • CI

Notes

  • The sync primitives, while verbose, entirely rely on spin for their implementation requiring no unsafe and not changing the status-quo on how locks actually work within Bevy. This is just a refactoring to consolidate the "hacks" and workarounds required to get a consistent experience when either using std::sync or spin.
  • I have opted to rely on std::sync for std compatible locks, maintaining the status quo. However, now that we have these locks factored out into the own module, it would be trivial to investigate alternate locking backends, such as parking_lot.
  • API for these locking types is entirely based on std. I have implemented methods and types which aren't currently in use within Bevy (e.g., LazyLock and Once) for the sake of completeness. As the standard library is highly stable, I don't expect the Bevy and std implementations to drift apart much if at all.

@bushrat011899 bushrat011899 added C-Code-Quality A section of code that is hard to understand or change A-Utils Utility functions and types X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 21, 2025
@bushrat011899
Copy link
Contributor Author

Marking as X-Blessed for the same reasons outlined here.

Copy link
Contributor

@BenjaminBrienen BenjaminBrienen left a comment

Choose a reason for hiding this comment

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

Looks pretty good. I'm impressed with how much progress is being made and I like how it makes engine development easier by making the code less noisy with feature gates.

Comment on lines -146 to -163
#[cfg(feature = "std")]
let set = self.0.read().unwrap_or_else(PoisonError::into_inner);

#[cfg(not(feature = "std"))]
let set = self.0.read();

if let Some(value) = set.get(value) {
return Interned(*value);
}
}

{
#[cfg(feature = "std")]
let mut set = self.0.write().unwrap_or_else(PoisonError::into_inner);

#[cfg(not(feature = "std"))]
let mut set = self.0.write();

Copy link
Contributor

Choose a reason for hiding this comment

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

I like how this cleans up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's exactly my goal here. It's technically more lines of code total, but it's far easier to reason about.

Copy link
Contributor

Choose a reason for hiding this comment

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

So clean!

Copy link
Member

@BD103 BD103 left a comment

Choose a reason for hiding this comment

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

Implementation is cleanly done, love it! 👍

bushrat011899 and others added 2 commits January 22, 2025 08:19
Most importantly, highlighting which items _would_ be in `std` which _aren't_ available.

Co-Authored-By: BD103 <[email protected]>
Co-Authored-By: Benjamin Brienen <[email protected]>
@bushrat011899 bushrat011899 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 21, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 23, 2025
Merged via the queue into bevyengine:main with commit 04990fc Jan 23, 2025
33 checks passed
mrchantey pushed a commit to mrchantey/bevy that referenced this pull request Feb 4, 2025
…e#17470)

# Objective

- Contributes to bevyengine#16877

## Solution

- Expanded `bevy_platform_support::sync` module to provide
API-compatible replacements for `std` items such as `RwLock`, `Mutex`,
and `OnceLock`.
- Removed `spin` from all crates except `bevy_platform_support`.

## Testing

- CI

---

## Notes

- The sync primitives, while verbose, entirely rely on `spin` for their
implementation requiring no `unsafe` and not changing the status-quo on
_how_ locks actually work within Bevy. This is just a refactoring to
consolidate the "hacks" and workarounds required to get a consistent
experience when either using `std::sync` or `spin`.
- I have opted to rely on `std::sync` for `std` compatible locks,
maintaining the status quo. However, now that we have these locks
factored out into the own module, it would be trivial to investigate
alternate locking backends, such as `parking_lot`.
- API for these locking types is entirely based on `std`. I have
implemented methods and types which aren't currently in use within Bevy
(e.g., `LazyLock` and `Once`) for the sake of completeness. As the
standard library is highly stable, I don't expect the Bevy and `std`
implementations to drift apart much if at all.

---------

Co-authored-by: BD103 <[email protected]>
Co-authored-by: Benjamin Brienen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Utils Utility functions and types C-Code-Quality A section of code that is hard to understand or change D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants