cc3200: Fix thread mutex's so threading works with interrupts.

Running Python code on a hard interrupt is incompatible with having a GIL,
because most of the time the GIL will be held by the user thread when the
interrupt arrives.  Hard interrupts mean that we should process them right
away and hence can't wait until the GIL is released.

The problem with the current code is that a hard interrupt will try to
exit/enter the GIL while it is still held by the user thread, hence leading
to a deadlock.

This patch works around such a problem by just making GIL exit/enter a
no-op when in an interrupt context, or when interrupts are disabled.

See issue #2406.
This commit is contained in:
Damien George 2016-10-19 14:24:56 +11:00
parent 204222653e
commit 17ba6ef5fa

View File

@ -31,8 +31,10 @@
#include "py/runtime.h"
#include "py/gc.h"
#include "py/mpthread.h"
#include "py/mphal.h"
#include "mptask.h"
#include "task.h"
#include "irq.h"
#if MICROPY_PY_THREAD
@ -166,14 +168,23 @@ void mp_thread_mutex_init(mp_thread_mutex_t *mutex) {
mutex->handle = xSemaphoreCreateMutexStatic(&mutex->buffer);
}
// To allow hard interrupts to work with threading we only take/give the semaphore
// if we are not within an interrupt context and interrupts are enabled.
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) {
int ret = xSemaphoreTake(mutex->handle, wait ? portMAX_DELAY : 0);
return ret == pdTRUE;
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
int ret = xSemaphoreTake(mutex->handle, wait ? portMAX_DELAY : 0);
return ret == pdTRUE;
} else {
return 1;
}
}
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
xSemaphoreGive(mutex->handle);
// TODO check return value
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
xSemaphoreGive(mutex->handle);
// TODO check return value
}
}
#endif // MICROPY_PY_THREAD