You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/08/08 10:27:03 UTC

(nuttx) branch master updated: spinlock: add support of spin_trylock_irqsave()

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 9cc25523b1 spinlock: add support of spin_trylock_irqsave()
9cc25523b1 is described below

commit 9cc25523b175b4e9f4b16a7cb6db231194113eb2
Author: chao an <an...@lixiang.com>
AuthorDate: Sun Aug 4 22:30:16 2024 +0800

    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 <an...@lixiang.com>
---
 include/nuttx/spinlock.h | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h
index 1d915a6314..ebb5d91328 100644
--- a/include/nuttx/spinlock.h
+++ b/include/nuttx/spinlock.h
@@ -626,6 +626,81 @@ irqstate_t spin_lock_irqsave(FAR volatile 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
  *