Skip to content

sched/spinlock: Refactor critical section implementation with rspinlock foundation and SMP optimization - #18135

Closed
hujun260 wants to merge 8 commits into
apache:masterfrom
hujun260:apache_remove_csection
Closed

hujun260 wants to merge 8 commits into
apache:masterfrom
hujun260:apache_remove_csection

Conversation

@hujun260

@hujun260 hujun260 commented Jan 24, 2026 •

Copy link
Copy Markdown
Contributor

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. Critical Section Refactoring - Standardize interface naming, remove redundant declarations, and consolidate SMP/non-SMP code paths for improved maintainability and reduced compilation complexity.

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

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

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

TEST

OSTEST pass

@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
@hujun260
hujun260 force-pushed the apache_remove_csection branch 9 times, most recently from f35b8c9 to 39a11bd Compare January 28, 2026 13:14

@linguini1 linguini1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR is going to need significant testing and proof of testing, including across multiple hardware targets.

Also, please stop including "files changed" in your PR descriptions. We can see the list of changed files with the diff.

Your description claims docs were updated, but the diff doesn't show any docs changes.

@hujun260
hujun260 force-pushed the apache_remove_csection branch 4 times, most recently from b2c13a0 to 4d525d0 Compare January 29, 2026 07:24
@cederom

cederom commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

@hujun260 can you please slow down pushing changes a bit this overloads our CI dramatically as each push triggers expensive build process (see #17914).. would it be possible to test more locally and then push verified changes?

@hujun260

Copy link
Copy Markdown
Contributor Author

@hujun260 can you please slow down pushing changes a bit this overloads our CI dramatically as each push triggers expensive build process (see #17914).. would it be possible to test more locally and then push verified changes?

Since this patch series involves adding numerous headers based on CI compilation errors, I need to trigger the CI pipeline repeatedly.

@linguini1

Copy link
Copy Markdown
Contributor

Since this patch series involves adding numerous headers based on CI compilation errors, I need to trigger the CI pipeline repeatedly.

Would you consider running the CI locally? I think @lupyuen or @simbit18 may know where the instructions for that is.

@hujun260

hujun260 commented Jan 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Since this patch series involves adding numerous headers based on CI compilation errors, I need to trigger the CI pipeline repeatedly.

Would you consider running the CI locally? I think @lupyuen or @simbit18 may know where the instructions for that is.

Since this patch series involves adding numerous headers based on CI compilation errors, I need to trigger the CI pipeline repeatedly.

Would you consider running the CI locally? I think @lupyuen or @simbit18 may know where the instructions for that is.

I tried running it locally, but the behavior doesn't exactly match the CI server, and I couldn't get it to pass completely.

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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>

@jerpelea jerpelea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please split the PR in multple PRs that can be understood and reviewed

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

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

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

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

Add #include <nuttx/spinlock.h> to source files across avr, mips,
renesas, and sparc architectures that use irqstate_t and critical
section functions but were missing the required header file.

Signed-off-by: hujun5 <[email protected]>
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: avr Issues related to all AVR(8-bit or 32-bit) architectures Arch: mips Issues related to the MIPS architecture Arch: renesas Issues related to the Renesas chips 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: Audio Area: Drivers Drivers issues Area: File System File System issues Area: Networking Effects networking subsystem Area: OS Components OS Components issues Area: Sensors Sensors issues Area: Tooling Area: USB Area: Video Board: arm Board: mips Board: risc-v Board: xtensa Size: L The size of the change in this PR is large Size: M The size of the change in this PR is medium 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.

6 participants