-
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
sys/atomic_utils: Functions for atomic access #14331
Conversation
Nice one! Should we already add atomic single bit functions? |
Great idea. Do you have any good documentation for this? E.g. the few examples for using this I found that didn't use the bit-banding periph region did never allocate memory in the SRAM bit-banding region, but just created a wild pointer pointing somewhere in there. That might not be the best idea... I think there should be some The alternative would be to check within every |
I think it is |
@kaspar030: I implemented the atomic set_bit / clear_bit functions as suggested using bit banding. Thanks for pointing me to this. I added some unit tests for arithmetic correctness of the atomic functions; which directly has proven to be worth the effort. |
I added one more test, which currently is only applicable to the I still need to extend the test to also allow checking the |
7a903e9
to
27cf219
Compare
This is now ready for review. |
re-ACK. I gave the PR a last complete look. Ran benchmarks, test and unittests on nrf52840dk. No issues. Let's give murdock another run with tests in samr21-xpro and esp32. |
All tests seem to be ok except `tests/pkg_libhydrogen/samr21-xpro:llvm, seems like its OK to squash @maribu |
This tests can be used to test for implementations violating atomic attribute.
In the comparison of IRQs vs. atomics for performance, was this evaluated only with SEQ_CST or also with ACQUIRE and RELEASE? After all, some say (citation needed, I've read an article but can't find it any more) that if you do SEC_CST you're almost always asking more of the platform than you need. |
tl;dr: No, I currently don't intent to extent this API to offer other guarantees than sequentially consistence. IMO that would gain only limited benefit in our context, but we would pay for that with increased complexity and confused developers. The comparison is only for sequentially consistent. IMO, adding more variants lead to more complexity and confusion. I already had quite a few highly emotional, exhausting, and lengthy discussions on whether just adding Note that overhead for sequentially consistent atomic accesses in the context of embedded systems is often significantly less than it is in multi-core superscalar out-of-order CPUs. First, disabling IRQs briefly is much cheaper than a context switch to the OS and back for operations that lack lock-free implementations. And e.g. a lock on the memory bus that might cause other CPU cores to block during loads and stores from/to RAM is something we also don't need to worry about. (When we add support for multi-core MCUs, I think a high level primitive like an yet to implement |
Hmm, I broke Murdock when merging this one: the unittests don't fit anymore in arm7 boards (msba2 and mcb2388). I suggest to move the atomic function unittest to its own test application (instead of simply excluding these boards from the unittests) ? |
Let's see if #15438 is fixing the problem |
Contribution description
This PR provides a set of functions to perform atomic accesses. These functions closely resemble the C11 atomic library functions, but are implemented more efficiently:
atomic_fetch_or(&mask, 1 << bit)
oratomic_fetch_and(&mask, ~(1 << bit))
this can be implemented lock-free and efficiently with bit-banding on Cortex M3 / M4 MCUsTesting procedure
Note: This currently only works for CortexM MCUs.
Performance Check
tests/bench_atomic_util
README.md
. If the given documentation is not sufficient, I really should fix it there rather than hereArithmetic Correctness Check
BOARD=<SOME_CORTEX_M_BOARD> make -C tests/unittests/ tests-atomic_utils flash test
Testing "Atomic-ness"
tests/sys_atomic_utils
make test
, but you might want to manually test as wellREADME.md
and the in-application documentation should be sufficient. If not, I really should fix it there rather than here.Issues/PRs references
Useful for #9882