Skip to content

Commit 5819e64

Browse files
committed
Fix race condition and message loss in Cortex-M IAR ports (#516)
- Added compiler memory barriers to BASEPRI management functions in tx_port.h. - Added architectural barriers (DSB/ISB) to scheduler return paths in tx_port.h and tx_thread_system_return.s to prevent fall-through before context switch. - These changes address spurious thread resumption and lost messages, especially when TX_NOT_INTERRUPTABLE is enabled. Assisted-by: Gemini (Gemini 2.0 Flash)
1 parent 6c45adf commit 5819e64

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

ports/cortex_m33/iar/inc/tx_port.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* SPDX-License-Identifier: MIT
1010
**************************************************************************/
1111

12+
// Some portions generated by Gemini (Gemini 2.0 Flash).
13+
1214

1315
/**************************************************************************/
1416
/**************************************************************************/
@@ -541,7 +543,7 @@ UINT posture;
541543
#ifdef TX_PORT_USE_BASEPRI
542544
__attribute__( ( always_inline ) ) static inline void __set_basepri_value(UINT basepri_value)
543545
{
544-
__asm__ volatile ("MSR BASEPRI,%0 ": : "r" (basepri_value));
546+
__asm__ volatile ("MSR BASEPRI,%0 ": : "r" (basepri_value) : "memory");
545547
}
546548
#else
547549
__attribute__( ( always_inline ) ) static inline void __enable_interrupts(void)
@@ -554,6 +556,7 @@ __attribute__( ( always_inline ) ) static inline void __restore_interrupt(UINT i
554556
{
555557
#ifdef TX_PORT_USE_BASEPRI
556558
__set_basepri_value(int_posture);
559+
__asm__ volatile ("" : : : "memory");
557560
#else
558561
__asm__ volatile ("MSR PRIMASK,%0": : "r" (int_posture): "memory");
559562
#endif
@@ -579,6 +582,7 @@ UINT interrupt_save;
579582

580583
/* Set PendSV to invoke ThreadX scheduler. */
581584
*((volatile ULONG *) 0xE000ED04) = ((ULONG) 0x10000000);
585+
__asm__ volatile ("dsb 0xF \n isb 0xF " : : : "memory");
582586
if (_tx_ipsr_get() == 0)
583587
{
584588
interrupt_save = __get_interrupt_posture();

ports/cortex_m33/iar/src/tx_thread_system_return.s

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
* Copyright (c) 2026-present Eclipse ThreadX contributors
4-
*
5-
* This program and the accompanying materials are made available under the
6-
* terms of the MIT License which is available at
7-
* https://opensource.org/licenses/MIT.
8-
*
9-
* SPDX-License-Identifier: MIT
10-
**************************************************************************/
1+
;***************************************************************************
2+
;* Copyright (c) 2024 Microsoft Corporation
3+
;* Copyright (c) 2026-present Eclipse ThreadX contributors
4+
;*
5+
;* This program and the accompanying materials are made available under the
6+
;* terms of the MIT License which is available at
7+
;* https://opensource.org/licenses/MIT.
8+
;*
9+
;* SPDX-License-Identifier: MIT
10+
;**************************************************************************
1111

12+
; Some portions generated by Gemini (Gemini 2.0 Flash).
13+
14+
15+
;**************************************************************************
1216

13-
/**************************************************************************/
1417
/**************************************************************************/
1518
/** */
1619
/** ThreadX Component */
@@ -69,6 +72,8 @@ _tx_thread_system_return:
6972
MOV r0, #0x10000000 // Load PENDSVSET bit
7073
MOV r1, #0xE000E000 // Load NVIC base
7174
STR r0, [r1, #0xD04] // Set PENDSVBIT in ICSR
75+
DSB SY // Ensure memory access is complete
76+
ISB SY // Flush pipeline
7277
MRS r0, IPSR // Pickup IPSR
7378
CMP r0, #0 // Is it a thread returning?
7479
BNE _isr_context // If ISR, skip interrupt enable

0 commit comments

Comments
 (0)