xiaoxiang781216 commented on code in PR #10605:
URL: https://github.com/apache/nuttx/pull/10605#discussion_r1348216222


##########
sched/semaphore/spinlock.c:
##########
@@ -137,32 +155,52 @@ void spin_lock_wo_note(FAR volatile spinlock_t *lock)
  *
  ****************************************************************************/
 
-spinlock_t spin_trylock(FAR volatile spinlock_t *lock)
+bool spin_trylock(FAR volatile spinlock_t *lock)
 {
 #ifdef CONFIG_SCHED_INSTRUMENTATION_SPINLOCKS
   /* Notify that we are waiting for a spinlock */
 
   sched_note_spinlock(this_task(), lock, NOTE_SPINLOCK_LOCK);
 #endif
 
-  if (up_testset(lock) == SP_LOCKED)
+#ifdef CONFIG_TICKET_SPINLOCK
+  uint16_t ticket = atomic_load(&lock->tickets.next);
+
+  spinlock_t old =
+    {
+    {
+        ticket, ticket
+    }
+    };
+
+  spinlock_t new =
+    {
+    {
+        ticket, ticket + 1
+    }
+    };
+
+  if (atomic_compare_exchange_strong(&lock->value, &old.value, new.value))
+#else /* CONFIG_TICKET_SPINLOCK */
+  if (up_testset(lock) == SP_UNLOCKED)
+#endif /* CONFIG_TICKET_SPINLOCK */ 
     {
 #ifdef CONFIG_SCHED_INSTRUMENTATION_SPINLOCKS
       /* Notify that we abort for a spinlock */
 
       sched_note_spinlock(this_task(), lock, NOTE_SPINLOCK_ABORT);
 #endif
-      SP_DSB();
-      return SP_LOCKED;
+      SP_DMB();
+      return SP_UNLOCKED;
     }
 
 #ifdef CONFIG_SCHED_INSTRUMENTATION_SPINLOCKS
   /* Notify that we have the spinlock */
 
   sched_note_spinlock(this_task(), lock, NOTE_SPINLOCK_LOCKED);
 #endif
-  SP_DMB();
-  return SP_UNLOCKED;
+  SP_DSB();
+  return SP_LOCKED;

Review Comment:
   yes, which makes bool more meanful.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to