-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix race in gpio_set_irq_enabled_with_callback #2635
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
Conversation
The order depends on whether you're enabling or disabling
| // when enabling, first set callback, then enable the interrupt | ||
| // when disabling, first disable the interrupt, then clear callback | ||
| if (enabled) gpio_set_irq_callback(callback); | ||
| gpio_set_irq_enabled(gpio, events, enabled); | ||
| if (!enabled) gpio_set_irq_callback(callback); | ||
| if (enabled) irq_set_enabled(IO_IRQ_BANK0, true); |
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.
Is this function likely to get used in performance-critical code? If it is, perhaps it's worth reducing the branches? 🤔 (or is this one of those cases where the compiler is able to do this optimisation automatically?)
| // when enabling, first set callback, then enable the interrupt | |
| // when disabling, first disable the interrupt, then clear callback | |
| if (enabled) gpio_set_irq_callback(callback); | |
| gpio_set_irq_enabled(gpio, events, enabled); | |
| if (!enabled) gpio_set_irq_callback(callback); | |
| if (enabled) irq_set_enabled(IO_IRQ_BANK0, true); | |
| // when enabling, first set callback, then enable the interrupt | |
| // when disabling, first disable the interrupt, then clear callback | |
| if (enabled) { | |
| gpio_set_irq_callback(callback); | |
| gpio_set_irq_enabled(gpio, events, enabled); | |
| irq_set_enabled(IO_IRQ_BANK0, true); | |
| } else { | |
| gpio_set_irq_enabled(gpio, events, enabled); | |
| gpio_set_irq_callback(callback); | |
| } |
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'll defer to @kilograham on this - I'm happy with either
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.
it is fine; the compiler will sort it out
The order depends on whether you're enabling or disabling
When disabling, if you get an interrupt after
gpio_set_irq_callbackbeforegpio_set_irq_enabledthen you get anunhandled_user_irqFix is to change where you
gpio_set_irq_callbackdepending on whether you're enabling or disabling