This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 4de3d8b7c3912bc626b5ea80d13288e4887e7684 Author: hujun5 <[email protected]> AuthorDate: Tue Jun 16 21:04:22 2026 +0800 spinlock: add CONFIG_SPINLOCK_INLINE to control inlining Spinlock helpers (spin_lock_notrace, rspin_lock_notrace, rspin_trylock, rspin_unlock_notrace, read_lock, read_trylock, write_lock) are always force-inlined today, which bloats every call site on small targets. disabling it emits a single callable copy of each helper from irq_spinlock.c, trading a function-call overhead for reduced code size. When CONFIG_SPINLOCK_INLINE is disabled, irq_spinlock.c defines SPINLOCK_INLINE as empty and forces CONFIG_SPINLOCK_INLINE on, causing the header bodies to compile as ordinary (non-static-inline) definitions. Additionally: - Inline read_lock_irqsave/read_unlock_irqrestore/write_lock_irqsave/ write_unlock_irqrestore in the header (trivial wrappers, previously extern in irq_spinlock.c) to avoid call overhead for simple irq_save + lock sequences. - Fix rspin_lock_is_hold to only save/restore IRQs under CONFIG_SPINLOCK, avoiding unnecessary irq_save on UP builds. Size reduction: 1400 bytes on a TriCore flat build with the option disabled. Assisted-By: mimo-v2.6-pro Signed-off-by: hujun5 <[email protected]> --- include/nuttx/spinlock.h | 84 ++++++++++++++++++++++++---- sched/Kconfig | 8 +++ sched/irq/irq_spinlock.c | 141 ++++------------------------------------------- 3 files changed, 91 insertions(+), 142 deletions(-) diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h index a49acebecf4..a874f5e58e5 100644 --- a/include/nuttx/spinlock.h +++ b/include/nuttx/spinlock.h @@ -46,6 +46,14 @@ extern "C" { #endif +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#if defined(CONFIG_SPINLOCK_INLINE) && !defined(SPINLOCK_INLINE) +# define SPINLOCK_INLINE static inline_function +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -133,7 +141,9 @@ static inline_function void rspin_lock_init(FAR rspinlock_t *lock) ****************************************************************************/ #ifdef CONFIG_SPINLOCK -static inline_function void spin_lock_notrace(FAR volatile spinlock_t *lock) +# ifdef CONFIG_SPINLOCK_INLINE +SPINLOCK_INLINE noinstrument_function +void spin_lock_notrace(FAR volatile spinlock_t *lock) { #ifdef CONFIG_TICKET_SPINLOCK int ticket = atomic_add_relaxed(&lock->next, 1); @@ -142,6 +152,10 @@ static inline_function void spin_lock_notrace(FAR volatile spinlock_t *lock) while (atomic_xchg_acquire((FAR atomic_t *)lock, SP_LOCKED) == SP_LOCKED); #endif } +# else +noinstrument_function +void spin_lock_notrace(FAR volatile spinlock_t *lock); +# endif #else # define spin_lock_notrace(lock) #endif /* CONFIG_SPINLOCK */ @@ -537,7 +551,8 @@ uint16_t rspin_lock_count(FAR volatile rspinlock_t *lock) ****************************************************************************/ #ifdef CONFIG_SPINLOCK -static inline_function +# ifdef CONFIG_SPINLOCK_INLINE +SPINLOCK_INLINE noinstrument_function void rspin_lock(FAR rspinlock_t *lock) { uint32_t new_val; @@ -574,6 +589,10 @@ void rspin_lock(FAR rspinlock_t *lock) compiler_assume(lock->local_val == new_val); } } +# else +noinstrument_function +void rspin_lock(FAR rspinlock_t *lock); +# endif static inline_function irqstate_t rspin_lock_irqsave(FAR rspinlock_t *lock) @@ -621,7 +640,8 @@ irqstate_t rspin_lock_irqsave_nopreempt(FAR rspinlock_t *lock) ****************************************************************************/ #ifdef CONFIG_SPINLOCK -static inline_function bool rspin_trylock(FAR rspinlock_t *lock) +# ifdef CONFIG_SPINLOCK_INLINE +SPINLOCK_INLINE bool rspin_trylock(FAR rspinlock_t *lock) { uint32_t old_val; bool ret; @@ -660,6 +680,9 @@ static inline_function bool rspin_trylock(FAR rspinlock_t *lock) return ret; } +# else +bool rspin_trylock(FAR rspinlock_t *lock); +# endif # define rspin_trylock_irqsave(l, f) \ ({ \ @@ -956,7 +979,7 @@ void rspin_restorelock(FAR rspinlock_t *lock, uint16_t count) # define rspin_restorelock(lock, count) #endif -#if defined(CONFIG_RW_SPINLOCK) +#ifdef CONFIG_RW_SPINLOCK /**************************************************************************** * Name: rwlock_init @@ -1001,7 +1024,8 @@ void rspin_restorelock(FAR rspinlock_t *lock, uint16_t count) * ****************************************************************************/ -static inline_function void read_lock(FAR volatile rwlock_t *lock) +# ifdef CONFIG_SPINLOCK_INLINE +SPINLOCK_INLINE void read_lock(FAR volatile rwlock_t *lock) { nxsched_critmon_busywait(true, return_address(0)); @@ -1018,6 +1042,9 @@ static inline_function void read_lock(FAR volatile rwlock_t *lock) nxsched_critmon_busywait(false, return_address(0)); } +# else +void read_lock(FAR volatile rwlock_t *lock); +# endif /**************************************************************************** * Name: read_trylock @@ -1044,7 +1071,8 @@ static inline_function void read_lock(FAR volatile rwlock_t *lock) * ****************************************************************************/ -static inline_function bool read_trylock(FAR volatile rwlock_t *lock) +# ifdef CONFIG_SPINLOCK_INLINE +SPINLOCK_INLINE bool read_trylock(FAR volatile rwlock_t *lock) { while (true) { @@ -1064,6 +1092,9 @@ static inline_function bool read_trylock(FAR volatile rwlock_t *lock) UP_DMB(); return true; } +# else +bool read_trylock(FAR volatile rwlock_t *lock); +# endif /**************************************************************************** * Name: read_unlock @@ -1115,7 +1146,8 @@ static inline_function void read_unlock(FAR volatile rwlock_t *lock) * ****************************************************************************/ -static inline_function void write_lock(FAR volatile rwlock_t *lock) +# ifdef CONFIG_SPINLOCK_INLINE +SPINLOCK_INLINE void write_lock(FAR volatile rwlock_t *lock) { nxsched_critmon_busywait(true, return_address(0)); @@ -1131,6 +1163,9 @@ static inline_function void write_lock(FAR volatile rwlock_t *lock) nxsched_critmon_busywait(false, return_address(0)); } +# else +void write_lock(FAR volatile rwlock_t *lock); +# endif /**************************************************************************** * Name: write_trylock @@ -1217,7 +1252,15 @@ static inline_function void write_unlock(FAR volatile rwlock_t *lock) ****************************************************************************/ #ifdef CONFIG_SPINLOCK -irqstate_t read_lock_irqsave(FAR rwlock_t *lock); +static inline_function irqstate_t read_lock_irqsave(FAR rwlock_t *lock) +{ + irqstate_t ret; + ret = up_irq_save(); + + read_lock(lock); + + return ret; +} #else # define read_lock_irqsave(l) ((void)(l), up_irq_save()) #endif @@ -1246,7 +1289,12 @@ irqstate_t read_lock_irqsave(FAR rwlock_t *lock); ****************************************************************************/ #ifdef CONFIG_SPINLOCK -void read_unlock_irqrestore(FAR rwlock_t *lock, irqstate_t flags); +static inline_function +void read_unlock_irqrestore(rwlock_t *lock, irqstate_t flags) +{ + read_unlock(lock); + up_irq_restore(flags); +} #else # define read_unlock_irqrestore(l, f) ((void)(l), up_irq_restore(f)) #endif @@ -1277,7 +1325,15 @@ void read_unlock_irqrestore(FAR rwlock_t *lock, irqstate_t flags); ****************************************************************************/ #ifdef CONFIG_SPINLOCK -irqstate_t write_lock_irqsave(FAR rwlock_t *lock); +static inline_function irqstate_t write_lock_irqsave(rwlock_t *lock) +{ + irqstate_t ret; + ret = up_irq_save(); + + write_lock(lock); + + return ret; +} #else # define write_lock_irqsave(l) ((void)(l), up_irq_save()) #endif @@ -1306,7 +1362,13 @@ irqstate_t write_lock_irqsave(FAR rwlock_t *lock); ****************************************************************************/ #ifdef CONFIG_SPINLOCK -void write_unlock_irqrestore(FAR rwlock_t *lock, irqstate_t flags); +static inline_function +void write_unlock_irqrestore(rwlock_t *lock, irqstate_t flags) +{ + write_unlock(lock); + + up_irq_restore(flags); +} #else # define write_unlock_irqrestore(l, f) ((void)(l), up_irq_restore(f)) #endif diff --git a/sched/Kconfig b/sched/Kconfig index b651b54f2d7..57c00029c12 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -298,6 +298,14 @@ config RW_SPINLOCK Reader can take read lock simultaneously and only one writer can take write lock. +config SPINLOCK_INLINE + bool "Inline spinlock helpers" + default !DEFAULT_SMALL + ---help--- + Inline spinlock helper functions to improve performance. Disable + this option to emit callable non-inline versions and reduce + header-inline code growth. + endif # SPINLOCK config IRQCHAIN diff --git a/sched/irq/irq_spinlock.c b/sched/irq/irq_spinlock.c index 3d68f6ec566..a7a8231bb64 100644 --- a/sched/irq/irq_spinlock.c +++ b/sched/irq/irq_spinlock.c @@ -25,6 +25,16 @@ ****************************************************************************/ #include <nuttx/config.h> + +/* When the inline spinlock helpers are disabled, emit callable non-inline + * versions of the helpers defined in <nuttx/spinlock.h> from this file. + */ + +#ifndef CONFIG_SPINLOCK_INLINE +# define CONFIG_SPINLOCK_INLINE +# define SPINLOCK_INLINE +#endif + #include <nuttx/spinlock.h> #include <assert.h> @@ -43,135 +53,4 @@ * Public Functions ****************************************************************************/ -#ifdef CONFIG_RW_SPINLOCK - -/**************************************************************************** - * Name: read_lock_irqsave - * - * Description: - * If SMP is enabled: - * The argument lock should be specified, - * disable local interrupts and take the lock spinlock and return - * the interrupt state. - * - * NOTE: This API is very simple to protect data (e.g. H/W register - * or internal data structure) in SMP mode. Do not use this API - * with kernel APIs which suspend a caller thread. (e.g. nxsem_wait) - * - * If SMP is not enabled: - * This function is equivalent to up_irq_save(). - * - * Input Parameters: - * lock - Caller specific spinlock, not NULL. - * - * Returned Value: - * An opaque, architecture-specific value that represents the state of - * the interrupts prior to the call to write_lock_irqsave(lock); - * - ****************************************************************************/ - -irqstate_t read_lock_irqsave(FAR rwlock_t *lock) -{ - irqstate_t ret; - ret = up_irq_save(); - - read_lock(lock); - - return ret; -} - -/**************************************************************************** - * Name: read_unlock_irqrestore - * - * Description: - * If SMP is enabled: - * The argument lock should be specified, release the lock and - * restore the interrupt state as it was prior to the previous call to - * read_lock_irqsave(lock). - * - * If SMP is not enabled: - * This function is equivalent to up_irq_restore(). - * - * Input Parameters: - * lock - Caller specific spinlock, not NULL. - * - * flags - The architecture-specific value that represents the state of - * the interrupts prior to the call to read_lock_irqsave(lock); - * - * Returned Value: - * None - * - ****************************************************************************/ - -void read_unlock_irqrestore(rwlock_t *lock, irqstate_t flags) -{ - read_unlock(lock); - up_irq_restore(flags); -} - -/**************************************************************************** - * Name: write_lock_irqsave - * - * Description: - * If SMP is enabled: - * The argument lock should be specified, - * disable local interrupts and take the lock spinlock and return - * the interrupt state. - * - * NOTE: This API is very simple to protect data (e.g. H/W register - * or internal data structure) in SMP mode. But do not use this API - * with kernel APIs which suspend a caller thread. (e.g. nxsem_wait) - * - * If SMP is not enabled: - * This function is equivalent to up_irq_save(). - * - * Input Parameters: - * lock - Caller specific spinlock, not NULL. - * - * Returned Value: - * An opaque, architecture-specific value that represents the state of - * the interrupts prior to the call to write_lock_irqsave(lock); - * - ****************************************************************************/ - -irqstate_t write_lock_irqsave(rwlock_t *lock) -{ - irqstate_t ret; - ret = up_irq_save(); - - write_lock(lock); - - return ret; -} - -/**************************************************************************** - * Name: write_unlock_irqrestore - * - * Description: - * If SMP is enabled: - * The argument lock should be specified, release the lock and - * restore the interrupt state as it was prior to the previous call to - * write_lock_irqsave(lock). - * - * If SMP is not enabled: - * This function is equivalent to up_irq_restore(). - * - * Input Parameters: - * lock - Caller specific spinlock, not NULL. - * - * flags - The architecture-specific value that represents the state of - * the interrupts prior to the call to write_lock_irqsave(lock); - * - * Returned Value: - * None - * - ****************************************************************************/ - -void write_unlock_irqrestore(rwlock_t *lock, irqstate_t flags) -{ - write_unlock(lock); - - up_irq_restore(flags); -} -#endif /* CONFIG_RW_SPINLOCK */ #endif /* CONFIG_SPINLOCK */
