-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add Cargo feature to use critical-section. #51
Conversation
c3c1dea
to
094b354
Compare
It looks like you have some commits from #50 in your branch by accident. I also wonder if a feature is the right way of doing this, since one of the things that |
094b354
to
de040ba
Compare
Thanks for the PR! I'm on board with merging this.
I'll write more about feature vs cfg later, but I think it's probably fine to add this as a feature. (I'm still a bit torn about this though.) |
As for build errors on non-embedded targets, this patch should fix it: d946bc2 |
Thanks for the
Done.
Done. re the last point: what should msp430 + critical-section do? Use critical-section, or keep using the msp430-specific impl ignoring |
btw @taiki-e could you set GHA approval to "Require approval for first-time contributors who are new to GitHub" in repo settings? This way contributors don't have to wait for the GHA approval on their first PR. |
96984d5
to
0077d2e
Compare
I've made it so critical-section is always used if requested, in any arch, including msp430. It's necessary for soundness, since the |
Done.
What we wanted to guarantee on MSP430 was the behavior of not disabling interrupts in load/store, add/sub/and/or/xor (added by #47), not (added by #54), etc. (See also msp430-atomic's readme) However, I did not know such behavior was incompatible with critical-section (cc @cr1901). There is a user who depends on these guarantees, so if these guarantees cannot be provided with critical-section, I think we will need to disable critical-section support on MSP430. |
For example, one valid implementation is locking a global RTOS mutex. The RTOS is allowed to preempt a task that's holding the mutex, it's sound because no other task can acquire the mutex concurrently (if it tries to, the RTOS would deschedule it, reschedule the original task to let it finish, then carry on). If we impl
I don't know how common it is to use RTOSs on MSP430, and how common would it be to want to use a RTOS mutex instead of disabling interrupts (I imagine it'd be slower). I don't think we should forbid using c-s on msp430 though. Supporting it is no extra effort, it's the same code as other archs. The operations you mention are still guaranteed to not disable interrupts on msp430 if you don't enable c-s support. |
AVR?
I think the idea for a preemptive @Dirbaio's solution to "unconditionally use critical sections for all operations" is a workaround. But I still want the optimizations.1 I think if the For instance, the critical section implementation of msp430 crate disables interrupts. This is functionally identical to the msp430-specific impl in
Agreed, FWIW.
|
Why is it an issue to disable the optimizations when c-s is enabled? If the user wants these optimizations, they can leave the if we document the optimization, and the fact that the optimizations are gone if you enable c-s, and give a guideline like "if all your c-s implementation does is disable interrupts (which is the most likely), there's no advantage in enabling c-s, keep it disabled so you can keep the optimizations", then it shouldn't be a big issue IMO. |
Dependencies may enable the
That said, I'm fine with this. Hopefully not many downstream crates from |
How's this going? |
If we can agree on a tentative direction on the platforms for which we are providing atomic implementations by default (MSP430 and AVR), I think we can move forward with this. (No need to worry about CI failures and the above platform handling, as these can be taken care of on my end before merging if needed. )
The problem is that (if we do this) enabling the critical-section feature drops the guarantee provided by default1. As far as I know, a sound solution to that problem is one of the following:
I had the second thing in mind when I first commented, but the third is also fine. Also even if you want to allow them eventually, we can start with deny for now and discuss it in a follow-up issue. (since deny->allow is not a breaking change)
Yeah, AVR is also always single-core, but that comment was about optimizations such as load/store, and only MSP430 needed to mention that optimization since AVR always disables interrupts even in load/store.
I think such a cfg would make sense, but I would like to discuss this in a follow-up issue as well.
The methods that benefit from such optimization have documentation describing them (except for load/store), but it seems to make sense to explain them comprehensively in the interrupt module's readme. Footnotes
|
Can you give an example of code that would rely on that guarantee for soundness? The semantics of the API are the same with both the msp430-specific and the critical-section implementations. I don't see how some piece of code could be sound with the first and not with the second. |
The specific use case I know of was the one discussed in the msp430-atomic issue. (However, I have not confirmed to see if that would cause a soundness issue.)
I believe the semantics of That said, I'm not an MSP430 expert, so if @cr1901 or/and @YuhanLiin are sure that calling interrupt-related instructions (or something used in the |
I don't think that causes a soundness issue, it's just slower. The
I mean the semantics of the actual public API. For example: the semantics of The HOW it does that (disabling irqs, locking a RTOS mutex, whatever) is not part of the semantics, it's an implementation detail. All implementations we have (use the |
Thanks for the clarification. Some implementation details in the |
So, I now think that the current implementation is (probably) fine as it is. And, I think the remaining things needed to merge this are docs and CI fixes.
|
- Update cspell dictionary - Exclude critical-section feature in MSRV check - Exclude critical-section feature in pre-1.54 build - Exclude critical-section feature in build with single-core cfg - Reduce memory usage in miri test by reducing QUICKCHECK_TESTS - Allow dead_code in imp::{msp430,riscv} modules when critical-section feature enabled - Fix cfg for riscv without atomics
0077d2e
to
6dda277
Compare
@taiki-e thanks for the CI fixes! I've pulled those in. I've also added docs to the README. |
f4f5629
to
8b4a183
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks @Dirbaio!
bors r+
Opened #61 to track the new release includes this. I think I can release this during this weekend. |
Published in portable-atomic v1.0.0. |
I want to depreecate
atomic-polyfill
in favor ofportable-atomic
. This is the only thing left thatportable-atomic
can't do, so here it goes.PR is still missing updating docs. I'll do it if you confirm you're onboard with the idea of supporting using
critical-section
.