-
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
tsrb: add drop function #9869
tsrb: add drop function #9869
Conversation
The get function does not support passing NULL as an input buffer. to be able to drop bytes from the buffer, a dedicated drop function is required
@@ -49,6 +49,16 @@ int tsrb_get(tsrb_t *rb, char *dst, size_t n) | |||
return (n - tmp); | |||
} | |||
|
|||
int tsrb_drop(tsrb_t *rb, size_t n) |
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.
how would it look if this would use tsrb_get_one() in a loop?
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.
Something like:
size_t tmp = n;
while (tmp && (tsrb_get_one(rb) != -1)) {
tmp--;
}
return (n - tmp);
But I'm not sure whether tsrb_get_one
is bug free. What happens if _pop
returns -1 because that's what's in the buffer? Is that sign extended and thus interpreted as if the buffer is empty? (Do you want an issue for this?)
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.
What happens if _pop returns -1 because that's what's in the buffer?
That is indeed a serious bug. Seems like slipdev is the only user of tsrb_get_one(), but how could that ever work? :)
We might have to change the whole API to use unsigned char.
Then why not make it support it? If something that was previously not allowed is then allowed, you should not be breaking any code. |
Because I expect that the impact of adding a separate function has less impact than adding the necessary checks to the function. In this case I would have to add a check to the |
That is only necessary because both functions are implemented with a loop. |
Feel free to build a pull request against this PR :) |
Whatever you do, keep in mind that tsrb is supposed to be lock-free. The code is thread safe when there's one producer and one consumer. It is also used for ISR-to-thread-communication, thus mutexes cannot be used for synchronization. Cutting corners by using memcpy might break some assumptions. |
That's exactly the reason why I don't want to burn myself implementing this with |
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.
ACK.
Even if I do, once the drop function makes it into the API we are stuck with it. |
And when writing to an |
Yes. Is this a general statement like "it cannot be guaranteed on every platform in existence", or "the way it is used here it does not work as expected on RIOT supported architecture X"? (Hint: aligned volatile unsigned int writes are atomic on MSP430 and ARM, not atomic on AVR 8bit. I'd assume MIPS and RISC-V have atomic word-size memory writes, but I don't know). |
It means: the C language does not guarantee it. Even if the architecture has single instruction word-write and word-reads, the C language does not guarantee anything and the compiler is free to do whatever it chooses. Finding a case of breakage in a 16-bit or 32-bit platform is harder than it is to fix it. For 8-bit platforms you are stuck with saving and restoring the interrupt mask. |
What's the message here? never go outside of what C guarantees? |
Of course!! That's the whole point of having a language specification, and when you need to do something that cannot be done with plain standard C, you use compiler-specific extensions (like 'asm' statements, builtins, etc) which are guaranteed (by the compiler). Going back to the main theme of this PR, my points are:
|
@jcarrano so are you blocking this PR or can we merge? |
@miri64 Yes. I think we should try to keep the API simple. If allowing a NULL pointer to Also, considering that this "drop" function did not exist until now (meaning no-one was dropping bytes, or they were doing it with "get_one") there is no performance impact to existing code. |
Funny, this is basically the opposite of what is requested in #9832. edit with "this" I mean "I think we should try to keep the API simple." by making one function to two things. |
@OlegHahm could you do some magic here? |
By magic you mean solving the deadlock? I can try... |
Okay, a first recap after I read the discussion:
|
@bergzand I will not bikeshed you. Go ahead and add the function. |
@jcarrano, thanks for giving in and helping to resolve this issue. However, I wonder if all your concerns were covered or if you would like to open a new issue regarding your concerns with the hardware-independent thread-safety of the module. |
There is: #9882. |
Contribution description
The get function does not support passing NULL as an input buffer. to be
able to drop bytes from the buffer, a dedicated drop function is
required
Issues/PRs references
required for #9870