-
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
periph: GPIO drivers are not thread safe #4866
Comments
I would start by deciding whether it should be thread-safe. Right now the documentation does not say anything about this topic. Also - if it is decided it should be thread-safe, please document the realtime side-effects of the implementation. |
For most ARM MCU's we could use bit-banding. This would make it thread safe
without extra side effects.
|
Good point. I am not quite sure though which ones to support bit banding, I think not all of the cortexes do... @LudwigKnuepfer: Yes, they need to be thread safe, otherwise we can always end up with bad behavior as described above. (see also -> #4758). Real-time side effects are almost non existant, as the intervals for which we might have to disable interrupts are very (20 instructions max) short. |
All STM32 >= Cortex-M3 support Bit-Banding :) Freescale to I belief.
|
This is the invariant of operations which are not thread safe, not a reason why this particular interface should be thread safe. If an interface is known not to be thread safe, it can still be used in a thread-safe manner through external measurements.
If they are present they may add up, so please document them unless there is good reason not to do so. I don't understand why you try to argue against this in the first place. |
The relevant part seems to be @haukepetersen writing there is a need for thread-safety of these APIs. I could not see any objections to this proposal in that place at the time of this writing. |
That does uglify the code a lot... |
Thing is, there's a lot of work in writing the actual code. Standing in row two and shouting, e.g., "document realtime-sideeffects!" will always be the right thing to do, but at the same time, always be annoying and taking wind out of the effort. |
@kaspar030, so what's your message? People should keep their silence when they observe that changes may have an effect on real-time guarantees? I agree 100% with you that it is sometimes annoying that you want to do one contribution and are suddenly facing several other more or less related issues that slow down the process of your original task. However, I really appreciate people looking thoroughly at these implications and stating them instead of simply waving things through. Disclaimer: this has little to do with the actual PR here. |
We have ~150 "disableIRQ()" in master, and none of them documents real-time effects. Suddenly insisting on documenting them in a random PR feels - annoying. I was merely answering the question why @haukepetersen argued against documenting them. |
"Things have always been bad, let's not start to improve them." That's your message then? Seriously? |
Whatever. |
@LudwigKnuepfer: I think my answers came out the wrong way, sorry. regarding real-time side-effects: regarding thread-safety:
|
For the set/clear/toggle it seems like most MCUs have special registers for only setting or clearing GPIOs (as opposed to "writing" a value to the pins) to work around this read-modify-write race, which means that it will not require any locking. Suggested plan:
Cortex-M3/M4 bit banding can be used to avoid locking for single bit flips but the code becomes quite long if you need to modify more than one or two bits. |
Sure, we can split the realtime side-effects documentation to a separate issue. Maybe it's more purposeful to provide some tool for the task of identifying the possible side-effects anyways. |
@kaspar030 did create an issue already: |
won't be addressed for the current release -> postponed (though still highly relevant) |
Did you make a note somewhere to put into the known issues for the release notes? |
put it into the release note draft |
I'm afraid that we need to postpone this again, anyone knows if the new GPIO API takes into account this issue? Maybe @PeterKietzmann ? |
Not that I know. IMO the issue is not about the API design but more about (i) the driver implementation itself and/or (ii) documentation about the programming model |
This issue would affect gpio expanders, isn't it @ZetaR60 ? |
@kYc0o Thanks for the heads up on this. For the gpio_exp interface in #9190 gpio_toggle is affected because it is implemented as a read followed by a write to reduce the number of callbacks stored. For other gpio_exp functions, it depends on the implementation of the specific GPIO expander (#9054 is affected). |
The issue with the extension API is fixed in #9860. |
Should we close this one? |
Note: My statement above only pertains to the GPIO extension API (which is not yet merged), and not to GPIO drivers in general. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
GPIO LL is thread safe and clearly communicates the requirements. Once |
From #4830: The implementation of almost all our GPIO drivers is not thread safe. Though we have not seen the effect of this somewhere, there are possibilities that pin configurations (or for some boards also set/clear operations) might be lost. Example are lines like these:
This is compiled into a non atomic read-modify-store sequence. So if interrupted and the same register is modified by a different thread, this modification will be lost.
It would be good to find a global solution/best practice on how to mitigate this. My first quick thought would be to actually disable interrupts while writing this reg, as these sequences are very small and mutexes would be too much overhead.
NOTE: The same issue might also effect other peripheral driver than GPIO ones, but as most work on a device-level without (many) shared global registers, the chances are not quite as high.
The text was updated successfully, but these errors were encountered: