Skip to content

Conversation

@HiFiPhile
Copy link
Collaborator

Describe the PR
To allow it be used in nested functions, I think most OSs already have the counter.

Copilot AI review requested due to automatic review settings November 7, 2025 10:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds nested locking support to the bare-metal spinlock implementation in osal_none.h. The enhancement allows the same execution context to acquire a spinlock multiple times without deadlocking, which is crucial for single-core bare-metal systems where nested critical sections are common.

Key changes:

  • Added nested_count field to track lock depth and prevent premature interrupt re-enabling
  • Modified lock/unlock logic to only disable/enable interrupts on the outermost lock/unlock
  • Added underflow detection to catch mismatched lock/unlock pairs

TU_ATTR_ALWAYS_INLINE static inline void osal_spin_unlock(osal_spinlock_t *ctx, bool in_isr) {
if (!in_isr) {
// Check for underflow - unlock without lock
if (ctx->nested_count == 0) {
Copy link
Owner

Choose a reason for hiding this comment

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

let make it simpler, in case of 0, we just return since the spin is not locked to begin with

Copy link
Owner

@hathach hathach 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

@hathach hathach merged commit 284895b into hathach:master Nov 19, 2025
85 checks passed
@HiFiPhile HiFiPhile deleted the lock_cnt branch November 19, 2025 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants