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

bug: reference counter increment/decrement not atomic #6

Open
t-bre opened this issue Feb 4, 2023 · 3 comments
Open

bug: reference counter increment/decrement not atomic #6

t-bre opened this issue Feb 4, 2023 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@t-bre
Copy link
Member

t-bre commented Feb 4, 2023

Description

Incrementing and decrementing of the reference count in rtcan_msg_t is not atomic and therefore not thread safe.

Expected Behaviour

Reference count implemented in thread-safe manner.

Additional Notes

  • Could use a semaphore for each RTCAN message, though there is a large memory cost there.
  • Could also have a "reference count mutex" which locks ALL reference counts, this is less costly in terms of memory but could slow things down.
@t-bre t-bre added the bug Something isn't working label Feb 4, 2023
@t-bre t-bre added this to the STAG 9 milestone Feb 8, 2023
@t-bre
Copy link
Member Author

t-bre commented Feb 27, 2023

Possibly useful: https://nullprogram.com/blog/2015/02/17/

We are building with C11, so we should be able to make use of stdatomic.h.

@t-bre
Copy link
Member Author

t-bre commented Mar 3, 2023

stdatomic.h has been added as part of #18. The relevant documentation is here.

I think what we need to do is:

  • Change the type of the reference count to atomic_uint.
  • Use atomic_store to initialise / set the reference count.
  • Use atomic_load to read the reference count.
  • Use atomic_fetch_add to increment the reference count.
  • Use atomic_fetch_sub to decrement the reference count.

@kruti99
Copy link

kruti99 commented Mar 7, 2023

yes, working on it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants