From e4fdef042a81032f65012ed218fa152d53885052 Mon Sep 17 00:00:00 2001 From: chao an Date: Sun, 4 Aug 2024 22:30:16 +0800 Subject: [PATCH] spinlock: add support of spin_trylock_irqsave() trylock spinlock in critical section: bool spin_trylock_irqsave(FAR volatile spinlock_t *lock, irqstate_t flags); bool spin_trylock_irqsave_wo_note(FAR volatile spinlock_t *lock, irqstate_t flags); Signed-off-by: chao an --- include/nuttx/spinlock.h | 75 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h index 4937887a64ef3..928094983ba18 100644 --- a/include/nuttx/spinlock.h +++ b/include/nuttx/spinlock.h @@ -626,6 +626,81 @@ irqstate_t spin_lock_irqsave(FAR spinlock_t *lock) # define spin_lock_irqsave(l) ((void)(l), up_irq_save()) #endif +/**************************************************************************** + * Name: spin_trylock_irqsave_wo_note + * + * Description: + * Try once to lock the spinlock. Do not wait if the spinlock is already + * locked. + * + * This implementation is the same as the above spin_trylock() except that + * it does not perform instrumentation logic. + * + * Input Parameters: + * lock - A reference to the spinlock object to lock. + * flags - flag of interrupts status + * + * Returned Value: + * SP_LOCKED - Failure, the spinlock was already locked + * SP_UNLOCKED - Success, the spinlock was successfully locked + * + * Assumptions: + * Not running at the interrupt level. + * + ****************************************************************************/ + +#ifdef CONFIG_SPINLOCK +# define spin_trylock_irqsave_wo_note(l, f) \ +({ \ + f = up_irq_save(); \ + spin_trylock_wo_note(l) ? \ + true : ({ up_irq_restore(f); false; }); \ +}) +#else +# define spin_trylock_irqsave_wo_note(l, f) \ +({ \ + (void)(l); \ + f = up_irq_save(); \ + true; \ +}) +#endif /* CONFIG_SPINLOCK */ + +/**************************************************************************** + * Name: spin_trylock_irqsave + * + * Description: + * Try once to lock the spinlock. Do not wait if the spinlock is already + * locked. + * + * Input Parameters: + * lock - A reference to the spinlock object to lock. + * flags - flag of interrupts status + * + * Returned Value: + * SP_LOCKED - Failure, the spinlock was already locked + * SP_UNLOCKED - Success, the spinlock was successfully locked + * + * Assumptions: + * Not running at the interrupt level. + * + ****************************************************************************/ + +#ifdef CONFIG_SPINLOCK +# define spin_trylock_irqsave(l, f) \ +({ \ + f = up_irq_save(); \ + spin_trylock(l) ? \ + true : ({ up_irq_restore(f); false; }); \ +}) +#else +# define spin_trylock_irqsave(l, f) \ +({ \ + (void)(l); \ + f = up_irq_save(); \ + true; \ +}) +#endif /* CONFIG_SPINLOCK */ + /**************************************************************************** * Name: spin_unlock_irqrestore_wo_note *