-
Notifications
You must be signed in to change notification settings - Fork 2k
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
core/rmutex: use atomic utils #16919
Conversation
On my Nucleo-F767ZI:
|
And also:
This PR:
So this increases ROM consumption. The reason is that previously relaxed memory order was used, atomic utils always use sequentially consistent memory accesses. I think this is actually fixing a race condition in the code, as otherwise the compiler could load the owner of the rmutex before checking if it is already locked. |
While I'm waiting for the tools I'll need to run the test: Do you know if that test ever passed? Were mutexes ever usable on AVR? On the more general topic: Are we moving off c11 atomics towards using the helper everywhere? (It'd make my life a lot easier, given that atomics are a pain to transpile right now, but that's off topic here). How does seqcst vs. relaxed impact the code size by 40 bytes? (Played around with godbolt; "relaxed" is 0 and "seq_cst", is it just that in may code locations?) |
Yeah. Also, is that a good idea, are C11 atomics that broken? Just to not open another NIH issue here. Are there pointers to reasoning? |
Don't get me wrong, I'm for this. I think the reasons in #14331 are sufficient. We should write something somewhere stating that in-tree, atomic_utils should be preferred and why. |
@@ -24,11 +24,6 @@ | |||
#define RMUTEX_H | |||
|
|||
#include <stdint.h> | |||
#ifdef __cplusplus | |||
#include "c11_atomics_compat.hpp" |
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.
The cross language issues with C11 atomics are not unique to rust, btw. C++ also has its own atomics, which are inherently incompatible. The c11_atomics_compat.hpp
could be dropped if we would move away from C11 atomics in-tree.
But I guess I should try to improve the ROM costs of the atomic utils first. There is one issue with GCC not optimizing unused variables across memory barriers - which LLVM just does if the variable could be (at least in theory) be allocated in registers, which LLVM reasons is not memory and thus not affected by memory barriers. I won't get past this issue, but GCC's optimizer is generally freaking out when it sees memory barriers or volatile
and stops optimizing things that are not affected. (E.g. I often saw that pointer arithmetic done for every access, despite the memory the pointer points to was marked as volatile
, not the pointer itself.) Making atomic utils consistently performing equally good or better than C11 atomics in every aspect will make them easy to sell.
Are you sure there is much to optimize? I see the same 24 bytes difference, 8 in rmutex_unlock and 16 in _lock, that's not nothing but not the end of the world either, especially if it does indeed fix a race condition as you mention. I don't see the race condition clearly yet, but comparing the the old and the new disassembled code (where By the way, I still can't build the test (even with #16923 cherry-picked, |
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.
Code looks good - anything that speaks against this?
A slight increase in code size, that some micro-optimizations in the atomic utils could improve again. But my work on that has stalled, sadly. Too much areas to work on, too little time ... |
Looks like there are some problems on |
Ah, yes. It has a separate implementation of ztimer{,64}_rmutex_unlock() that also needs to be adapted. I'll rebase and do so once the kids are in bed. |
This will make it easier to change the width of kernel_pid_t if needed without breaking code.
Replace use of C11 atomics with atomic utils. This fixes > error: address argument to atomic operation must be a pointer to a > trivially-copyable type ('_Atomic(int) *' invalid) error when compiling on AVR with LLVM.
A single unrelated test failed. I'm restarting the CI without tests again. |
Contribution description
Replace use of C11 atomics with atomic utils. This fixes
error when compiling on AVR with LLVM.
Testing procedure
tests/rmutex
should still passIssues/PRs references
None