-
Notifications
You must be signed in to change notification settings - Fork 2k
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
sys/{tsrb, oorb}: Thread-safe ringbuffers #15103
Conversation
The thread-safe ringbuffer assumes that only one thread writes to it and only one thread reads from it. This rename makes it more obvious and allows to additionally add a (less efficient) thread-safe ringbuffer implementation that works with multiple readers and/or writers.
Based on the tsrb code just renamed to oorb, implement a thread-safe ring buffer that works for multiple readers and/or writers by disabling IRQs during critical phases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the TSRB could just do e.g.
static inline int tsrb_empty(const oorb_t *rb)
{
int retval;
unsigned status = irq_disable();
retval = oorb_empty(rb);
irq_restore(status);
return retval;
}
to avoid code duplication
volatile unsigned reads; /**< total number of reads */ | ||
volatile unsigned writes; /**< total number of writes */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do those need to be volatie
?`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, those should be atomic. Or more precisely, semi-atomic :-D
(As this is the two-threads-one-buffer mode, only the reader will write to reads
and only the writer will write to writes
. So as long as the read and the write access to them is atomically, everything is fine. This means explicitly that a read-modify-write operation could consist of three operations with only the read and write being atomic. Real atomic accesses would make the whole read-modify-write atomically. The atomic utils helper functions introduce just the semi-atomic functions this needs - which at least for word sized variables should be lock free.)
But I intentionally left the implementation of oorb unchanged. Fixing it efficiently would require the atomic utils PR being merged. And fixing it inefficiently is done in tsrb.
Btw: As boths contributions have been split out, there is no need to keep this open. |
Contribution description
tsrb
implementation tooorb
tsrb
code, but insertirq_disable()
...irq_restore()
where needed to provide thready-safety for multiple readers and writersoorb
Testing procedure
E.g. stdio over UART on STM32 should still work. And now concurrent stdio should not result in corrupting the output (only in mixing it).
Issues/PRs references
Addresses #9882. (But the same issues still apply to the new
oorb
. However, with the documentation pointing those issues out, I think we can consider them as features :-D. And those "features" should be addressed in a follow up soonish.)