Skip to content

Commit

Permalink
spinlock: add sched_lock to spin_lock_irqsave
Browse files Browse the repository at this point in the history
reason:
We aim to replace big locks with smaller ones. So we will use spin_lock_irqsave extensively to
replace enter_critical_section in the subsequent process. We imitate the implementation of Linux
by adding sched_lock to spin_lock_irqsave in order to address scenarios where sem_post occurs
within spin_lock_irqsave, which can lead to spinlock failures and deadlocks.

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Nov 27, 2024
1 parent b845963 commit 96d42c5
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions include/nuttx/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,17 @@ irqstate_t spin_lock_irqsave_wo_note(FAR volatile spinlock_t *lock)
spin_lock_wo_note(lock);
}

sched_lock_wo_note();
return ret;
}
#else
# define spin_lock_irqsave_wo_note(l) ((void)(l), up_irq_save())
static inline_function
irqstate_t spin_lock_irqsave_wo_note(FAR volatile spinlock_t *lock)
{
irqstate_t flags = up_irq_save();
sched_lock_wo_note();
return flags;
}
#endif

/****************************************************************************
Expand Down Expand Up @@ -586,7 +593,13 @@ irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock)
return flags;
}
#else
# define spin_lock_irqsave(l) ((void)(l), up_irq_save())
static inline_function
irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock)
{
irqstate_t flags = up_irq_save();
sched_lock_wo_note();
return flags;
}
#endif

/****************************************************************************
Expand Down Expand Up @@ -694,9 +707,10 @@ void spin_unlock_irqrestore_wo_note(FAR volatile spinlock_t *lock,
}

up_irq_restore(flags);
sched_unlock_wo_note();
}
#else
# define spin_unlock_irqrestore_wo_note(l, f) ((void)(l), up_irq_restore(f))
# define spin_unlock_irqrestore_wo_note(l, f) ((void)(l), up_irq_restore(f), sched_unlock_wo_note())
#endif

/****************************************************************************
Expand Down Expand Up @@ -742,7 +756,7 @@ void spin_unlock_irqrestore(FAR volatile spinlock_t *lock,
sched_note_spinlock_unlock(lock);
}
#else
# define spin_unlock_irqrestore(l, f) ((void)(l), up_irq_restore(f))
# define spin_unlock_irqrestore(l, f) ((void)(l), up_irq_restore(f), sched_unlock_wo_note())
#endif

#if defined(CONFIG_RW_SPINLOCK)
Expand Down

0 comments on commit 96d42c5

Please sign in to comment.