Skip to content

Commit 939c831

Browse files
Fix deadlock during attachInterrupt (#879)
Fixes #878
1 parent 3768aa7 commit 939c831

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

cores/rp2040/wiring_private.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ void _gpioInterruptDispatcher(uint gpio, uint32_t events) {
8181
}
8282
}
8383

84+
// To be called when appropriately protected w/IRQ and mutex protects
85+
static void _detachInterruptInternal(pin_size_t pin) {
86+
auto irq = _map.find(pin);
87+
if (irq != _map.end()) {
88+
gpio_set_irq_enabled(pin, 0x0f /* all */, false);
89+
_map.erase(pin);
90+
}
91+
}
92+
8493
extern "C" void attachInterrupt(pin_size_t pin, voidFuncPtr callback, PinStatus mode) {
8594
CoreMutex m(&_irqMutex);
8695
if (!m) {
@@ -97,7 +106,7 @@ extern "C" void attachInterrupt(pin_size_t pin, voidFuncPtr callback, PinStatus
97106
default: return; // ERROR
98107
}
99108
noInterrupts();
100-
detachInterrupt(pin);
109+
_detachInterruptInternal(pin);
101110
CBInfo cb(callback);
102111
_map.insert({pin, cb});
103112
gpio_set_irq_enabled_with_callback(pin, events, true, _gpioInterruptDispatcher);
@@ -134,10 +143,6 @@ extern "C" void detachInterrupt(pin_size_t pin) {
134143
}
135144

136145
noInterrupts();
137-
auto irq = _map.find(pin);
138-
if (irq != _map.end()) {
139-
gpio_set_irq_enabled(pin, 0x0f /* all */, false);
140-
_map.erase(pin);
141-
}
146+
_detachInterruptInternal(pin);
142147
interrupts();
143148
}

0 commit comments

Comments
 (0)