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

various fixups for atomic operation APIs #22915

Merged
merged 3 commits into from
Mar 10, 2020

Conversation

andrewboie
Copy link
Contributor

@andrewboie andrewboie commented Feb 18, 2020

  • fix return value type of atomic_cas() being bool in some cases, int in others
  • add atomic_ptr_t typedef and atomic_ptr_cas(), atomic_ptr_set(), atomic_ptr_get(), atomic_ptr_clear() APIs
  • fix a type safety issue in shell

Fixes: #22887

@andrewboie andrewboie added this to the v2.3.0 milestone Feb 18, 2020
@zephyrbot zephyrbot added area: Networking area: API Changes to public APIs area: Tests Issues related to a particular existing or missing test area: Kernel labels Feb 18, 2020
@zephyrbot
Copy link
Collaborator

zephyrbot commented Feb 18, 2020

All checks are passing now.

checkpatch (informational only, not a failure)

-:82: WARNING:NEW_TYPEDEFS: do not add new typedefs
#82: FILE: include/sys/atomic.h:24:
+typedef void *atomic_ptr_t;

-:204: WARNING:SPACING: space prohibited between function name and open parenthesis '('
#204: FILE: include/sys/atomic.h:293:
+	defined (CONFIG_ATOMIC_OPERATIONS_C)

- total: 0 errors, 2 warnings, 392 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Your patch has style problems, please review.

NOTE: Ignored message types: AVOID_EXTERNS BRACES CONFIG_EXPERIMENTAL CONST_STRUCT DATE_TIME FILE_PATH_CHANGES MINMAX NETWORKING_BLOCK_COMMENT_STYLE PRINTK_WITHOUT_KERN_LEVEL SPLIT_STRING VOLATILE

NOTE: If any of the errors are false positives, please report
      them to the maintainers.

Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages.

@andrewboie andrewboie requested a review from ceolin February 18, 2020 20:25
@andrewboie andrewboie marked this pull request as ready for review February 18, 2020 20:25
@nordic-krch
Copy link
Contributor

@andrewboie given your comment (#22887 (comment)) should then atomic_t become uintptr_t or slong_t? What's pros and cons? They are same size on 32bit and 64bit so maybe it does not matter much.

@nordic-krch
Copy link
Contributor

I think you can add "fixes #22887" to the description.

@andrewboie andrewboie force-pushed the atomic-ops-fixups branch 2 times, most recently from fd382ce to b399ffd Compare February 20, 2020 01:01
@andrewboie andrewboie requested a review from Vudentz as a code owner February 20, 2020 01:01
@andrewboie
Copy link
Contributor Author

@andrewboie given your comment (#22887 (comment)) should then atomic_t become uintptr_t or slong_t? What's pros and cons? They are same size on 32bit and 64bit so maybe it does not matter much.

atomic_t is currently a signed integer. using uintptr_t would convert it to an unsigned value, which I do not want to do in this PR.

@joerchan
Copy link
Contributor

@andrewboie given your comment (#22887 (comment)) should then atomic_t become uintptr_t or slong_t? What's pros and cons? They are same size on 32bit and 64bit so maybe it does not matter much.

atomic_t is currently a signed integer. using uintptr_t would convert it to an unsigned value, which I do not want to do in this PR.

Is a long guaranteed to be the same size as a pointer? AFAIK it is only guaranteed to be 32 bits or more.

@andrewboie
Copy link
Contributor Author

@andrewboie given your comment (#22887 (comment)) should then atomic_t become uintptr_t or slong_t? What's pros and cons? They are same size on 32bit and 64bit so maybe it does not matter much.

atomic_t is currently a signed integer. using uintptr_t would convert it to an unsigned value, which I do not want to do in this PR.

Is a long guaranteed to be the same size as a pointer? AFAIK it is only guaranteed to be 32 bits or more.

I used one of zephyr's derived types in zephyr/types.h. I did not typedef atomic_t to long.

slong_t / ulong_t are defined as being able to hold a pointer. They are that header's version of uintptr_t / intptr_t

@andyross
Copy link
Contributor

We can't make atomic_t be an architecture-dependent size. Users need to be able to store known quantities in it. And on a practical level: 64 bit platforms need to be able to make atomic updates to 32 bit ints too.

@andrewboie
Copy link
Contributor Author

We can't make atomic_t be an architecture-dependent size. Users need to be able to store known quantities in it. And on a practical level: 64 bit platforms need to be able to make atomic updates to 32 bit ints too.

What do you suggest we do?
I'm loath to introduce a parallel set of 64-bit wide atomic APIs, signed vs unsigned, etc

@andyross
Copy link
Contributor

I don't see that we have much choice. Apps want a consistent data type for atomic operations (e.g lock counts) that isn't architecture-dependent. But they also want to be able to store a pointer/address in them. I think having a separate "atomic_addr_t" (which on 32 bit systems would just be implemented as a atomic_t) is reasonable enough.

And I take no position on signed/unsigned. From my perspective it's perfectly OK to have it be any integer type and let the applications cast as needed. The feature being provided is about the memory underneath the type, not the bytes themselves.

@joerchan
Copy link
Contributor

What about the original suggestion:

Create atomic_ptr_t type and set of atomic_ptr_cas(), atomic_ptr_set(), atomic_ptr_get() and atomic_ptr_clear() functions.

@andrewboie
Copy link
Contributor Author

What about the original suggestion:

Create atomic_ptr_t type and set of atomic_ptr_cas(), atomic_ptr_set(), atomic_ptr_get() and atomic_ptr_clear() functions.

Yeah, you guys have convinced me ... updated PR in a bit

Andrew Boie added 2 commits February 20, 2020 12:27
The variable needs to be an atomic_t, and in one case it was
being incremented outside of an atomic_inc/atomic_add.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
In some cases this was a bool, in some cases an integer value
of 1 or 0. Standardize on bool.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
@andrewboie
Copy link
Contributor Author

Updated PR ... adds atomic_ptr_* APIs for cas, get, set, clear.

@zephyrbot zephyrbot added the area: Xtensa Xtensa Architecture label Feb 20, 2020
The existing APIs are insufficient on 64-bit systems as atomic_t
is 32-bits wide.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
@nashif nashif merged commit 9f0acd4 into zephyrproject-rtos:master Mar 10, 2020
@andrewboie andrewboie deleted the atomic-ops-fixups branch September 24, 2020 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Bluetooth area: Kernel area: Networking area: Shell Shell subsystem area: Tests Issues related to a particular existing or missing test area: Xtensa Xtensa Architecture
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Atomic operations on pointers
10 participants