Skip to content

Add builtin implementation for std targets.#22

Merged
Dirbaio merged 7 commits into
mainfrom
std-impl
Aug 17, 2022
Merged

Add builtin implementation for std targets.#22
Dirbaio merged 7 commits into
mainfrom
std-impl

Conversation

@Dirbaio

@Dirbaio Dirbaio commented Aug 14, 2022

Copy link
Copy Markdown
Member

This adds an std Cargo feature that enables a critical-section implementation for std.

IMO this is the only impl that we should have "builtin". Rust's std is essentially "stable forever" so the impl won't need breaking changes, and it should work soundly on all targets where std is supported.

Note this uses Rust 1.63's "const Mutex::new()", so this makes our MSRV effectively that for std targets. I think it should be acceptable.

@eldruin eldruin left a comment

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.

Seems fine to me but I am no expert on CS. Maybe @adamgreig could review?
I noticed now that there is no MSRV notice or CI job at all here.
Being such a foundational crate I think we should have one, even if it is 1.63.
It's definitely not a breaking change if there was none before, right? 😉

@Dirbaio

Dirbaio commented Aug 15, 2022

Copy link
Copy Markdown
Member Author

Added 1.63 MSRV notice to the README.

It's definitely not a breaking change if there was none before, right?

😂 It's not breaking anyway because 1.63 is only actually required if you enable the std feature, which didn't exist before.

@reitermarkus reitermarkus left a comment

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.

Some small suggestions, but in general looks good to me.

Comment thread src/std.rs Outdated
Comment thread src/std.rs Outdated
Comment thread src/std.rs Outdated
Comment thread src/std.rs Outdated
Comment thread src/std.rs Outdated
Comment thread Cargo.toml Outdated
Comment thread .github/workflows/clippy.yml Outdated
@Dirbaio

Dirbaio commented Aug 16, 2022

Copy link
Copy Markdown
Member Author

Thanks @reitermarkus for the review, I've incorporated the feedback.

@eldruin eldruin left a comment

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.

Could you also add this feature to the test CI workflow?

@adamgreig

Copy link
Copy Markdown
Member
  • I don't like the asymmetry in acquire being "lock, then store true to cell" while release is "unlock, then store false to cell", but I appreciate that since the cell is thread-local and the thread can't pre-empt itself it probably doesn't matter that another thread might have taken the mutex between us unlocking it and us storing false back to the cell. Indeed it means we hold the mutex lock for very slightly less time which is probably beneficial. But, it's more "obvious" to keep the lock until we've cleared the cell. What do you think?
  • Otherwise the std impl all seems fine to me 👍
  • We should document the new feature in the README so that it also appears in the library documentation. Probably in the "providing an implementation" section. It could briefly explain how it's implemented too.
  • I think it's fine for the std impl to require 1.63 and obviously therefore bump the MSRV, but I wonder if it would be kind to have a lower MSRV without the std feature, or at least mention it, as 1.63 is so new and we're basically rolling this out across the whole embedded ecosystem. Since we'll probably add a CI test case for the feature, that one can build with the higher MSRV, and the other case could use the lower one. Dunno.

@Dirbaio

Dirbaio commented Aug 17, 2022

Copy link
Copy Markdown
Member Author

(@eldruin) Could you also add this feature to the test CI workflow?

I didn't because it runs the doctests showing how to set an impl, which then fails duplicate symbol errors because there's also the std impl. IMO it should be fine since there are no tests other than the doctests anyway.

(@adamgreig) But, it's more "obvious" to keep the lock until we've cleared the cell. What do you think?

Actually, we can also set the flag before acquiring the mutex. This way it's symmetric! :)
I've added some comments highlighting the fact that it's fine due to being thread local.

(@adamgreig) We should document the new feature in the README so that it also appears in the library documentation. Probably in the "providing an implementation" section. It could briefly explain how it's implemented too.

Done

(@adamgreig) I think it's fine for the std impl to require 1.63 and obviously therefore bump the MSRV, but I wonder if it would be kind to have a lower MSRV without the std feature

SGTM. Documented MSRV as 1.54 without std, 1.63 with. Added CI checks for both. (1.54 needed for #[doc(include_str!())] support)

@Dirbaio

Dirbaio commented Aug 17, 2022

Copy link
Copy Markdown
Member Author

For some reason 1.54 doesn't like #[transparent] on RestoreState, so I've removed it.


error[E0690]: transparent struct needs exactly one non-zero-sized field, but has 0
   --> src/lib.rs:123:1
    |
123 | pub struct RestoreState(RawRestoreState);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0

For some reason 1.54 doesn't like it.

```
error[E0690]: transparent struct needs exactly one non-zero-sized field, but has 0
   --> src/lib.rs:123:1
    |
123 | pub struct RestoreState(RawRestoreState);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0
```
@notgull

notgull commented Aug 17, 2022

Copy link
Copy Markdown

I think it's because RawRestoreState can be a (), and ZSTs can't be the only field in a transparent struct.

@Dirbaio

Dirbaio commented Aug 17, 2022

Copy link
Copy Markdown
Member Author

Yeah, but that was fine in 1.63 🤷 Either way, I think not having it is fine, it's very unlikely it'll result in worse code. Ssee #20 (comment)

@eldruin

eldruin commented Aug 17, 2022

Copy link
Copy Markdown
Member

(@eldruin) Could you also add this feature to the test CI workflow?

I didn't because it runs the doctests showing how to set an impl, which then fails duplicate symbol errors because there's also the std impl. IMO it should be fine since there are no tests other than the doctests anyway.

Hmm, you can gate the doctest code with something like:

# #[cfg(not(feature = "std"))]
# {
// ... actual doctest code
# }

@Dirbaio

Dirbaio commented Aug 17, 2022

Copy link
Copy Markdown
Member Author

Done (it's slightly abominable, but works...)

@adamgreig

Copy link
Copy Markdown
Member

Thanks for the updates, LGTM.

@eldruin eldruin left a comment

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.

Alright, thank you everybody!
bors r+

@bors

bors Bot commented Aug 17, 2022

Copy link
Copy Markdown
Contributor

👎 Rejected by code reviews

@eldruin

eldruin commented Aug 17, 2022

Copy link
Copy Markdown
Member

Ach, @reitermarkus could you review this again?

@Dirbaio

Dirbaio commented Aug 17, 2022

Copy link
Copy Markdown
Member Author

bors r+

@bors

bors Bot commented Aug 17, 2022

Copy link
Copy Markdown
Contributor

👎 Rejected by code reviews

@Dirbaio

Dirbaio commented Aug 17, 2022

Copy link
Copy Markdown
Member Author

ugh, why does a "neutral" review with comments count as "rejected"?

@Dirbaio Dirbaio requested a review from reitermarkus August 17, 2022 16:15
@Dirbaio

Dirbaio commented Aug 17, 2022

Copy link
Copy Markdown
Member Author

bors r+

@bors

bors Bot commented Aug 17, 2022

Copy link
Copy Markdown
Contributor

👎 Rejected by code reviews

@Dirbaio Dirbaio merged commit b6ae622 into main Aug 17, 2022
@bors bors Bot deleted the std-impl branch August 17, 2022 16:18
@Dirbaio Dirbaio mentioned this pull request Aug 17, 2022
bors Bot added a commit to embassy-rs/embassy that referenced this pull request Aug 17, 2022
901: Update to critical-section 1.0, atomic-polyfill 1.0 r=Dirbaio a=Dirbaio

TODO
- [x] Wait for cortex-m 0.7.6 release rust-embedded/cortex-m#449
- [x] ~~Wait for defmt-rtt 0.4 release knurling-rs/defmt#689 we're still going to use defmt 0.3 for now, which will use the `critical-section` 0.2 default impl, which works.
- [x] Wait for critical-secton `std` impl rust-embedded/critical-section#22

Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
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.

5 participants