Skip to content

Conversation

@hujun260
Copy link
Contributor

@hujun260 hujun260 commented Jan 24, 2026

Summary

This pull request refactors the NuttX critical section implementation to use rspinlock_t as the unified foundation while optimizing performance for both SMP and non-SMP configurations. The series introduces enhanced spinlock operations, consolidates the scheduler interface, and eliminates deprecated synchronization mechanisms.

should merge after apache/nuttx-apps#3375

Changes Made

  1. Spinlock Extensions - Add try-lock variants (rspin_trylock, rspin_trylock_irqsave, rspin_trylock_irqsave_nopreempt) to support non-blocking spinlock acquisition patterns with comprehensive nesting support.

  2. Consistent Rspinlock Interface - Implement rspinlock count/owner tracking even when CONFIG_SPINLOCK=n to provide uniform APIs across configurations and enable portable synchronization code.

  3. Critical Section Refactoring - Standardize interface naming, remove redundant declarations, and consolidate SMP/non-SMP code paths for improved maintainability and reduced compilation complexity.

  4. Performance Optimization - Optimize critical section scope by narrowing protection windows to essential operations only, reducing interrupt latency and improving system responsiveness.

  5. Architecture Consolidation - Replace up_switch_context with unified nxsched_switch interface across all architecture implementations for improved consistency and reduced code duplication.

  6. Cleanup and Removal - Remove deprecated irqcount field and unnecessary preprocessor directives, simplifying the TCB structure and reducing memory overhead.

Simplified PR Description (Concise Version)

Refactor NuttX critical section implementation using rspinlock_t foundation with optimizations for both SMP and non-SMP configurations. Consolidates scheduler interface, enhances spinlock operations, and eliminates deprecated mechanisms.

Key Changes

  • Add rspin_trylock/irqsave/nopreempt variants for flexible synchronization
  • Implement consistent rspinlock interface across all configurations
  • Optimize critical section scope for reduced latency
  • Replace up_switch_context with nxsched_switch for consistency
  • Remove deprecated irqcount and cleanup code

Benefits

  • Improved performance (reduced overhead in non-SMP mode)
  • Better code maintainability and consistency
  • Backward compatible with existing applications
  • Enhanced nesting support (up to UINT16_MAX)

Impact

  • Performance: Reduced context switch overhead in non-SMP mode and optimized critical section scope for better responsiveness
  • Code Quality: Improved consistency across SMP/non-SMP configurations and reduced code duplication in architecture layers
  • Maintainability: Simplified interfaces, standardized naming, and consolidated code paths reduce future maintenance burden
  • Compatibility: Maintains full backward compatibility with existing NuttX applications

Files Modified

Core Scheduler:

  • sched/sched/sched.h - Critical section interface refactoring
  • sched/irq/irq_csection.c - IRQ section cleanup
  • sched/sched_critmonitor.c - Monitoring code adaptation

Architecture Headers:

  • include/nuttx/spinlock.h - Spinlock extensions and rspinlock standardization
  • include/nuttx/irq.h - Critical section declaration updates
  • include/nuttx/sched.h - Interface updates

Utilities:

  • drivers/note/note_driver.c - Remove deprecated irqcount tracking
  • sched/task/task_exit.c - Adapt to refactored interface
  • sched/task/task_restart.c - Adapt to refactored interface

Performance:

  • Multiple scheduler and IPC files adjusted to optimize critical section scope

Testing

  1. Unit Tests: Verify critical section entry/exit on both SMP and non-SMP configurations
  2. Nesting Tests: Test maximum nesting depth (UINT16_MAX) with rspinlock operations
  3. Performance Tests: Measure context switch latency improvements on non-SMP and interrupt latency on SMP
  4. Compatibility Tests: Run full NuttX test suite to ensure no regressions
  5. Architecture Tests: Verify nxsched_switch implementation on all supported architectures

Verification Checklist

  • Code builds without warnings on all configurations
  • Critical section operations maintain consistent semantics
  • SMP and non-SMP code paths tested
  • Spinlock nesting depth validated
  • Context switch latency measurements verified
  • No memory leaks or resource leaks introduced
  • All existing tests pass
  • Documentation updated for new spinlock operations

@github-actions github-actions bot added Area: Tooling Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Area: Drivers Drivers issues Area: File System File System issues Area: OS Components OS Components issues Area: USB Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Jan 24, 2026
@hujun260 hujun260 force-pushed the apache_remove_csection branch from 4e30316 to 3435fb0 Compare January 24, 2026 09:04
@xiaoxiang781216
Copy link
Contributor

@hujun260 please fix:

788233a1fb nuttx/spinlock.h: add rspin_trylock/_irqsave/_nopreempt support
../nuttx/tools/checkpatch.sh -c -u -m -g fd67ef3af94ab9221be7442fbc73f5a1ec892972..HEAD
❌ Commit subject missing colon (e.g. 'subsystem: msg')
❌ Remove Gerrit Change-ID's before submitting upstream

Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @hujun260 + for perfect PR descrition :-)

Please review build errors. Are you sure this only depends on apache/nuttx-apps#3375 ?

do \
{ \
uint16_t count = rspin_lock_count(&g_schedlock); \
up_switch_context(tcb, rtcb); \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance will degrade on AMP

Copy link
Contributor Author

@hujun260 hujun260 Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

In amp restore_critical_section is a null implementation (no-op); the related logic will be optimized out, such as rspin_lock_count and up_interrupt_context.Performance will not degrade

@hujun260 hujun260 force-pushed the apache_remove_csection branch from 6d88755 to 5c16756 Compare January 26, 2026 08:41
wangzhi16 and others added 8 commits January 26, 2026 18:46
Standardize critical section function naming conventions and simplify
the interface by removing redundant function declarations and
consolidating SMP/non-SMP code paths for improved maintainability.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Remove unnecessary preprocessor #undef directives to improve code
clarity and reduce conditional compilation complexity. This cleanup
aligns with the refactored critical section interface.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Consolidate architecture-specific context switching by replacing
up_switch_context with unified nxsched_switch interface, reducing
code duplication and improving scheduler integration consistency
across all architecture implementations.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Optimize critical section usage by reducing protection scope to only
cover essential operations, improving system responsiveness and
reducing interrupt latency while maintaining synchronization safety.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Refactor critical section implementation to use rspinlock_t as the
foundation, providing consistent synchronization semantics across SMP
and non-SMP configurations with improved nesting support and reduced
overhead.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Clean up and remove the deprecated irqcount field from task structures
as it is no longer used by the refactored critical section implementation.
This simplifies the TCB structure and reduces memory overhead.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Inline enter_critical_section function calls in performance-critical
paths to reduce function call overhead while maintaining consistent
semantics, improving overall system latency and responsiveness in
real-time scenarios.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Optimize critical section implementation for non-SMP configurations by
directly using up_irq_save/restore instead of rspinlock operations,
reducing context switch overhead while maintaining compatibility with
SMP code paths.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Area: Drivers Drivers issues Area: File System File System issues Area: Networking Effects networking subsystem Area: OS Components OS Components issues Area: Tooling Area: USB Area: Video Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants