Skip to content

core: Provide all C11 atomic operations#5688

Merged
jnohlgard merged 19 commits intoRIOT-OS:masterfrom
jnohlgard:pr/atomics-refactor
Feb 9, 2017
Merged

core: Provide all C11 atomic operations#5688
jnohlgard merged 19 commits intoRIOT-OS:masterfrom
jnohlgard:pr/atomics-refactor

Conversation

@jnohlgard
Copy link
Copy Markdown
Member

@jnohlgard jnohlgard commented Jul 25, 2016

This PR adds the remaining __atomic library functions for C11 support and also provides the __sync functions for older GCC versions (msp430).

All atomic.h uses in the tree have been replaced by their C11 equivalents.

The functions provided here are only used when the compiler doesn't know how to do a lockless atomic operation for the given target architecture. Therefore, with a recent GCC, most of these functions are only used on Cortex-M0, MSP430.

Note: This PR works fine with -std=gnu99 (the current setting), even though the title says C11

@jnohlgard jnohlgard added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: core Area: RIOT kernel. Handle PRs marked with this with care! labels Jul 25, 2016
@jnohlgard jnohlgard added this to the Release 2016.10 milestone Jul 25, 2016
@jnohlgard jnohlgard force-pushed the pr/atomics-refactor branch from 8812c3c to 618c595 Compare July 25, 2016 13:45
@jnohlgard
Copy link
Copy Markdown
Member Author

(void) memorder;
unsigned int mask = irq_disable();
memcpy(ret, ptr, size);
memcpy(ptr, val, size);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No line memcpy(val, ret, size);?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is implemented as the manual specifies it:

https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

@jnohlgard
Copy link
Copy Markdown
Member Author

msp430-libc was missing stdatomic.h so I copied Newlib's header and the required support headers to msp430-common (BSD license).

Full C11 atomic functionality is still provided when compiling with -std=gnu99, at least for the newlib platforms, so we don't need to switch to -std=gnu11 yet.

@kaspar030
Copy link
Copy Markdown
Contributor

Nice! Did you do some codesize comparisons?

@jnohlgard jnohlgard force-pushed the pr/atomics-refactor branch from 618c595 to 76707eb Compare August 8, 2016 22:23
@jnohlgard
Copy link
Copy Markdown
Member Author

@kaspar030 I did not, it's difficult to find anything to compare since the unit tests had to be modified. The emb6 test app saved 28 bytes of ROM on samr21-xpro. Almost nothing was using atomic.h anymore before this PR so its removal was well deserved.

@DipSwitch
Copy link
Copy Markdown
Member

DipSwitch commented Aug 9, 2016

Does this override the GCC buildins which for ARM use LDREX and STREX by default? Instead of disabling and enabling the interrupt?

@jnohlgard
Copy link
Copy Markdown
Member Author

jnohlgard commented Aug 9, 2016

@DipSwitch GCC only generates library calls if there is no lock-free hardware implementation available. If building for a platform which have atomic operations in hardware then GCC will generate the proper instructions instead (LDREX, STREX on Cortex-M3,M4)

You can verify by building the tests-core in tests/unittests and inspecting the disassembly (make tests-core && arm-none-eabi-objdump -d bin/$BOARD/tests-core/tests-core-atomic.o)

@josephnoir
Copy link
Copy Markdown
Contributor

josephnoir commented Aug 30, 2016

Admittedly, I don't understand most of the changes and thus have a hard time reviewing the changes.

Edit: I noticed that the files I have most problems with are imported from newlib. Do we assume those to be correct?

@jnohlgard jnohlgard assigned kaspar030 and unassigned josephnoir Aug 30, 2016
@jnohlgard
Copy link
Copy Markdown
Member Author

@josephnoir that's OK, I'll reassign.
@kaspar030 do you mind taking a look at this?

@jnohlgard jnohlgard added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 14, 2016
@miri64
Copy link
Copy Markdown
Member

miri64 commented Oct 31, 2016

Kaspar is on vacation, so I need to postpone this one :(

@miri64 miri64 modified the milestones: Release 2017.01, Release 2016.10 Oct 31, 2016
@jnohlgard
Copy link
Copy Markdown
Member Author

Ping @kaspar030
Any other maintainer who would be willing to review this in time for christmas?
It cleans up the implementation quite a lot and it is not overly complex, despite the seemingly large change set.

@jnohlgard
Copy link
Copy Markdown
Member Author

ping maintainers!
Can we get this merged in time for 2017.01?

@jnohlgard jnohlgard force-pushed the pr/atomics-refactor branch from be63739 to f055137 Compare February 8, 2017 15:24
@jnohlgard
Copy link
Copy Markdown
Member Author

@OlegHahm ping!

@kaspar030
Copy link
Copy Markdown
Contributor

I just tested the core unittests on msb-430h, looks good!

2017-02-08 16:37:06,321 - INFO # RIOT MSP430 hardware initialization complete.
2017-02-08 16:37:06,322 - INFO #
2017-02-08 16:37:06,330 - INFO # main(): This is RIOT! (Version: 2017.04-devel-131-gf0551-6b9ed7c519ae-pr5688)
2017-02-08 16:37:08,213 - INFO # .....................................................................................
2017-02-08 16:37:08,216 - INFO # OK (85 tests)

Copy link
Copy Markdown
Contributor

@kaspar030 kaspar030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK from my side. Thanks for all the work here, must have been awful to keep the "old" platforms supported!

@jnohlgard
Copy link
Copy Markdown
Member Author

Fixes #6491
(to let GitHub auto-close the issue)

@jnohlgard
Copy link
Copy Markdown
Member Author

Core change needs a second ACK. @melshuber, @OlegHahm, @Kijewski?

@kaspar030 Thanks, I have felt responsible for this since I was the one who introduced the old incomplete atomic.h API. It feels nice to finally make the support complete, and also standards compliant.

The resulting assembly code for a simple load is quite heavy (see #6491 (comment)), but I guess that's an optimization we can postpone until it becomes an issue.

Copy link
Copy Markdown

@melshuber melshuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

One could argue to leave the old API (maybe by calling the implementation) for compaibility issues

@jnohlgard
Copy link
Copy Markdown
Member Author

@melshuber I would prefer to not leave the old implementation in, since it was not really working as intended (ATOMIC_VALUE on avr for example)

@jnohlgard jnohlgard merged commit 0941078 into RIOT-OS:master Feb 9, 2017
@jnohlgard jnohlgard deleted the pr/atomics-refactor branch February 9, 2017 08:00
@melshuber
Copy link
Copy Markdown

@gebart well enough, I just wanted to mention it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: core Area: RIOT kernel. Handle PRs marked with this with care! CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants