Skip to content

Commit

Permalink
spinlock: add support of spin_trylock_irqsave()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
anchao committed Aug 4, 2024
1 parent d8a3b6c commit e4fdef0
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions include/nuttx/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit e4fdef0

Please sign in to comment.