From 2703ecc9e98819ab4d13bdb6da6e0d02ee840d86 Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Tue, 21 Jun 2022 00:24:53 +0200 Subject: [BUG] Fix deadlocks on disconnected secondary half (#17423) --- platforms/chibios/drivers/serial.c | 11 ++++------- platforms/chibios/drivers/serial_protocol.c | 8 ++++---- 2 files changed, 8 insertions(+), 11 deletions(-) (limited to 'platforms/chibios') diff --git a/platforms/chibios/drivers/serial.c b/platforms/chibios/drivers/serial.c index e5f346ba33..3fae5cd3a4 100644 --- a/platforms/chibios/drivers/serial.c +++ b/platforms/chibios/drivers/serial.c @@ -87,10 +87,7 @@ static THD_FUNCTION(Thread1, arg) { chRegSetThreadName("blinker"); while (true) { palWaitLineTimeout(SOFT_SERIAL_PIN, TIME_INFINITE); - - split_shared_memory_lock(); interrupt_handler(NULL); - split_shared_memory_unlock(); } } @@ -155,6 +152,7 @@ static void __attribute__((noinline)) serial_write_byte(uint8_t data) { // interrupt handle to be used by the slave device void interrupt_handler(void *arg) { + split_shared_memory_lock_autounlock(); chSysLockFromISR(); sync_send(); @@ -212,6 +210,8 @@ void interrupt_handler(void *arg) { static inline bool initiate_transaction(uint8_t sstd_index) { if (sstd_index > NUM_TOTAL_TRANSACTIONS) return false; + split_shared_memory_lock_autounlock(); + split_transaction_desc_t *trans = &split_transaction_table[sstd_index]; // TODO: remove extra delay between transactions @@ -292,8 +292,5 @@ static inline bool initiate_transaction(uint8_t sstd_index) { // // this code is very time dependent, so we need to disable interrupts bool soft_serial_transaction(int sstd_index) { - split_shared_memory_lock(); - bool result = initiate_transaction((uint8_t)sstd_index); - split_shared_memory_unlock(); - return result; + return initiate_transaction((uint8_t)sstd_index); } diff --git a/platforms/chibios/drivers/serial_protocol.c b/platforms/chibios/drivers/serial_protocol.c index 3e160e0c5c..c95aed9885 100644 --- a/platforms/chibios/drivers/serial_protocol.c +++ b/platforms/chibios/drivers/serial_protocol.c @@ -22,13 +22,11 @@ static THD_FUNCTION(SlaveThread, arg) { chRegSetThreadName("split_protocol_tx_rx"); while (true) { - split_shared_memory_lock(); if (unlikely(!react_to_transaction())) { /* Clear the receive queue, to start with a clean slate. * Parts of failed transactions or spurious bytes could still be in it. */ serial_transport_driver_clear(); } - split_shared_memory_unlock(); } } @@ -64,6 +62,8 @@ static inline bool react_to_transaction(void) { return false; } + split_shared_memory_lock_autounlock(); + split_transaction_desc_t* transaction = &split_transaction_table[transaction_id]; /* Send back the handshake which is XORed as a simple checksum, @@ -102,9 +102,7 @@ static inline bool react_to_transaction(void) { * @return bool Indicates success of transaction. */ bool soft_serial_transaction(int index) { - split_shared_memory_lock(); bool result = initiate_transaction((uint8_t)index); - split_shared_memory_unlock(); if (unlikely(!result)) { /* Clear the receive queue, to start with a clean slate. @@ -125,6 +123,8 @@ static inline bool initiate_transaction(uint8_t transaction_id) { return false; } + split_shared_memory_lock_autounlock(); + split_transaction_desc_t* transaction = &split_transaction_table[transaction_id]; /* Send transaction table index to the slave, which doubles as basic handshake token. */ -- cgit v1.2.3