diff options
author | Damien George <damien.p.george@gmail.com> | 2017-03-22 12:44:04 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-03-22 12:44:04 +1100 |
commit | 96c35d0ac4b63dfbb746b6391f0850a923ed87d6 (patch) | |
tree | 2a31ca6224d62f09ec286348f9f824cc7ba3a849 | |
parent | 080210ddc60cdfcb33992c20104dd3b2b817861e (diff) | |
download | micropython-96c35d0ac4b63dfbb746b6391f0850a923ed87d6.tar.gz micropython-96c35d0ac4b63dfbb746b6391f0850a923ed87d6.zip |
stmhal/pybthread: Allow interrupts to work during lock/unlock of mutex.
When locking/unlocking a mutex we only need to protect against a thread
switch, not general interrupts.
-rw-r--r-- | stmhal/pybthread.c | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/stmhal/pybthread.c b/stmhal/pybthread.c index 51e9a738d0..6baf88f66b 100644 --- a/stmhal/pybthread.c +++ b/stmhal/pybthread.c @@ -37,6 +37,11 @@ #define PYB_MUTEX_UNLOCKED ((void*)0) #define PYB_MUTEX_LOCKED ((void*)1) +// These macros are used when we only need to protect against a thread +// switch; other interrupts are still allowed to proceed. +#define RAISE_IRQ_PRI() raise_irq_pri(IRQ_PRI_PENDSV) +#define RESTORE_IRQ_PRI(state) restore_irq_pri(state) + extern void __fatal_error(const char*); volatile int pyb_thread_enabled; @@ -176,15 +181,15 @@ void pyb_mutex_init(pyb_mutex_t *m) { } int pyb_mutex_lock(pyb_mutex_t *m, int wait) { - uint32_t irq_state = disable_irq(); + uint32_t irq_state = RAISE_IRQ_PRI(); if (*m == PYB_MUTEX_UNLOCKED) { // mutex is available *m = PYB_MUTEX_LOCKED; - enable_irq(irq_state); + RESTORE_IRQ_PRI(irq_state); } else { // mutex is locked if (!wait) { - enable_irq(irq_state); + RESTORE_IRQ_PRI(irq_state); return 0; // failed to lock mutex } if (*m == PYB_MUTEX_LOCKED) { @@ -202,14 +207,14 @@ int pyb_mutex_lock(pyb_mutex_t *m, int wait) { pyb_thread_remove_from_runable(pyb_thread_cur); // thread switch will occur after we enable irqs SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; - enable_irq(irq_state); + RESTORE_IRQ_PRI(irq_state); // when we come back we have the mutex } return 1; // have mutex } void pyb_mutex_unlock(pyb_mutex_t *m) { - uint32_t irq_state = disable_irq(); + uint32_t irq_state = RAISE_IRQ_PRI(); if (*m == PYB_MUTEX_LOCKED) { // no threads are blocked on the mutex *m = PYB_MUTEX_UNLOCKED; @@ -226,7 +231,7 @@ void pyb_mutex_unlock(pyb_mutex_t *m) { // put unblocked thread on runable list pyb_thread_add_to_runable(th); } - enable_irq(irq_state); + RESTORE_IRQ_PRI(irq_state); } #endif // MICROPY_PY_THREAD |