Skip to content

Conversation

kjbracey
Copy link
Contributor

Description

Code clean up - the atomic code is confused about the use and meaning of volatile. It's using casts internally where not necessary, and potentially forcing users to insert casts.

Only visible change to application code is that

volatile uint8_t my_volatile;
core_util_atomic_incr_u8(&my_volatile, 1);

will now work - previously it would have required

core_util_atomic_incr_u8((uint8_t *)&my_volatile, 1);

This will continue to work:

uint8_t my_non_volatile;
core_util_atomic_incr_u8(&my_non_volatile, 1);

Change broken into two individual commits to try to avoid impression that the net change is just expecting users to be providing volatile objects - see commit messages for more detail.

(Given the lack of a full suite of atomic operations including load and store, it's probably the case that users will be currently needing to mark stuff volatile just to avoid compiler optimisations. If we do get a full suite of atomic operations, and that would be required for SMP, then there would usually be no need for volatile, but could be in some cases. This change makes us consistent with C11/C++11 regardless).

Pull request type

  • Fix
  • Refactor
  • New target
  • Feature
  • Breaking change

The volatile qualifier on the __LDREX/__STREX prototypes only means that
it's safe to use them on volatile objects. Doesn't mean you actually
have to pass them volatile pointers.

Adding the volatile is a bit like doing strlen((const char *) ptr)
because you've got a non-const pointer.
The atomic functions preserve volatile semantics - they only perform the
accesses specified. Add the volatile qualifier to the value pointer to
reflect this. This does not change existing caller code - it's
equivalent to adding a const qualifier to indicate we don't write to
a pointer - it means people can pass us qualified pointers without
casts, letting the compile check const- or volatile-correctness.

This is consistent with C11 <stdatomic.h>, which volatile-qualifies its
equivalent functions.

Note that this useage of volatile has nothing to do with the atomicity -
objects accessed via the atomic functions do not need to be volatile.
But it does permit these calls to be used on objects which have been
declared volatile.
@kjbracey
Copy link
Contributor Author

Do shout if you can see a way this would break any existing code - as far as I can see it's equivalent to adding a missing const qualifier.

Only thing I can think of is someone having a local declaration, or a dummy definition in a unit test.

Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

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

Looks fine to me; it should not break existing code (even at assembly level; I add a quick look and it is identical).
Alignment with c11 standard is much appreciated 👍 .

Copy link
Contributor

@geky geky left a comment

Choose a reason for hiding this comment

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

Good find 👍

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 20, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Mar 20, 2018

Build : FAILURE

Build number : 1491
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6368/

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 20, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Mar 20, 2018

Build : FAILURE

Build number : 1492
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6368/

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 20, 2018

License failure, will restart soon

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 20, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Mar 20, 2018

Build : SUCCESS

Build number : 1495
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6368/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Mar 20, 2018

@mbed-ci
Copy link

mbed-ci commented Mar 21, 2018

@cmonr cmonr merged commit 3ddca11 into ARMmbed:master Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants