Skip to content
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

Why nuttx didn't support atomic instruction? #10642

Closed
TaiJuWu opened this issue Sep 15, 2023 · 5 comments
Closed

Why nuttx didn't support atomic instruction? #10642

TaiJuWu opened this issue Sep 15, 2023 · 5 comments

Comments

@TaiJuWu
Copy link
Contributor

TaiJuWu commented Sep 15, 2023

Many architecture offer atomic instruction like compare and swap, test and set....
But I didn't find any atomic operation in nuttx kernel except atomic testing.

Is there any reason so we don't use them?
Because recently I want to optimize fair spinlock #10605 with lock-free skill.
But the basic requirements are atomic instruction.

@TaiJuWu TaiJuWu changed the title nuttx didn't use atomic instruction? Why nuttx didn't support atomic instruction? Sep 16, 2023
@xiaoxiang781216
Copy link
Contributor

atomic operation is defined since c11(https://en.cppreference.com/w/c/language/atomic), and the implementation is provided by toolchain already(e.g, libgcc.a from gcc). That's why you can't find the implementation from nuttx, an atmic test was added recently to help you check the atomic support with your toolchain:
https://github.com/apache/nuttx-apps/tree/master/testing/atomic.
For the old compiler, nuttx provide a simple implementation which ensure the atomic by disabling interrupt temporarily:
https://github.com/apache/nuttx/blob/master/libs/libc/machine/arch_atomic.c
Of cause, this implementation just works with one CPU case.

@TaiJuWu
Copy link
Contributor Author

TaiJuWu commented Sep 16, 2023

atomic operation is defined since c11(https://en.cppreference.com/w/c/language/atomic), and the implementation is provided by toolchain already(e.g, libgcc.a from gcc). That's why you can't find the implementation from nuttx, an atmic test was added recently to help you check the atomic support with your toolchain:
https://github.com/apache/nuttx-apps/tree/master/testing/atomic.
For the old compiler, nuttx provide a simple implementation which ensure the atomic by disabling interrupt temporarily:
https://github.com/apache/nuttx/blob/master/libs/libc/machine/arch_atomic.c
Of cause, this implementation just works with one CPU case.

First thank you for your reply.

https://github.com/apache/nuttx/blob/master/libs/libc/machine/arch_atomic.c
It seems to work on multi-core.
The share variable already is protected by spin lock and disable interrupt.
Why did you say only work on one CPU?

@TaiJuWu TaiJuWu closed this as completed Sep 16, 2023
@TaiJuWu TaiJuWu reopened this Sep 16, 2023
@mu578
Copy link

mu578 commented Sep 16, 2023

atomic operation is defined since c11(https://en.cppreference.com/w/c/language/atomic), and the implementation is provided by toolchain already(e.g, libgcc.a from gcc). That's why you can't find the implementation from nuttx, an atmic test was added recently to help you check the atomic support with your toolchain:
https://github.com/apache/nuttx-apps/tree/master/testing/atomic.
For the old compiler, nuttx provide a simple implementation which ensure the atomic by disabling interrupt temporarily:
https://github.com/apache/nuttx/blob/master/libs/libc/machine/arch_atomic.c
Of cause, this implementation just works with one CPU case.

First thank you for your reply.

https://github.com/apache/nuttx/blob/master/libs/libc/machine/arch_atomic.c It seems to work on multi-core. The share variable already is protected by spin lock and disable interrupt. Why did you say only work on one CPU?

yes here atomic are builtin with a spin lock ... where you want to build your spin lock relaying on atomic ops ehehe ; gcc and clang have builtin atomic ops, however for gcc not prior 4.7, have a look here, that's pasta dig up (1), you may still dump assembly and see how barriers are emitted, moslty spin lock only need safe write, read so a compare_swap op.

-1
https://gist.github.com/mu578/b7bd9ebd2131c02d73d25f1a6f927751
and here a more experimental take
https://github.com/mu578/mu0/blob/master/mu0/mu0_definition/mu0_atomic.h

@xiaoxiang781216
Copy link
Contributor

https://github.com/apache/nuttx/blob/master/libs/libc/machine/arch_atomic.c It seems to work on multi-core. The share variable already is protected by spin lock and disable interrupt. Why did you say only work on one CPU?

Since the code can just disable the current core interrupt, not other cores. Even it can disable other cores, it doesn't work as your expect, because other cores are still running and may try to acquire the same lock.

@TaiJuWu
Copy link
Contributor Author

TaiJuWu commented Sep 17, 2023

https://github.com/apache/nuttx/blob/master/libs/libc/machine/arch_atomic.c It seems to work on multi-core. The share variable already is protected by spin lock and disable interrupt. Why did you say only work on one CPU?

Since the code can just disable the current core interrupt, not other cores. Even it can disable other cores, it doesn't work as your expect, because other cores are still running and may try to acquire the same lock.

Yes, It acquire same lock and spin here.
I guess that even if disable core interrupt but the shared variable maybe happen in global interrupt context.
So the library only works on single core.

@TaiJuWu TaiJuWu closed this as completed Sep 17, 2023
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

No branches or pull requests

3 participants