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


##########
libs/libc/pthread/pthread_spinlock.c:
##########
@@ -52,267 +54,161 @@
  * Name: pthread_spin_init
  *
  * Description:
- *   The pthread_spin_init() function will allocate any resources required
- *   to use the spin lock referenced by lock and initialize the lock to an
- *   unlocked state.
- *
- *   The results are undefined if pthread_spin_init() is called specifying
- *   an already initialized spin lock. The results are undefined if a spin
- *   lock is used without first being initialized.
- *
- *   If the pthread_spin_init() function fails, the lock is not initialized
- *   and the contents of lock are undefined.
- *
- *   Only the object referenced by lock may be used for performing
- *   synchronization.  The result of referring to copies of that object in
- *   calls to pthread_spin_destroy(), pthread_spin_lock(),
- *   pthread_spin_trylock(), or pthread_spin_unlock() is undefined.
+ *   Initialize the spin lock to the unlocked state.  The lock is an atomic
+ *   variable, so it also works in memory that processes share
+ *   (PTHREAD_PROCESS_SHARED).
  *
  * Input Parameters:
- *   lock - A reference to the spinlock object to be initialized.
- *   pshared - Unused
+ *   lock    - A reference to the spinlock object to be initialized.
+ *   pshared - PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED
  *
  * Returned Value:
- *   Zero (OK) is returned if a valid lock value is provided.
- *
- * POSIX Compatibility:
- *   Not supported: "If the Thread Process-Shared Synchronization option is
- *   supported and the value of pshared is PTHREAD_PROCESS_SHARED, the
- *   implementation shall permit the spin lock to be operated upon by any
- *   thread that has access to the memory where the spin lock is allocated,
- *   even if it is allocated in memory that is shared by multiple
- *   processes."
+ *   Zero (OK) on success, EINVAL if an argument is not valid.
  *
  ****************************************************************************/
 
 int pthread_spin_init(FAR pthread_spinlock_t *lock, int pshared)
 {
-  int ret = EINVAL;
-
-  DEBUGASSERT(lock != NULL);
-  if (lock != NULL)
+  if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
+                       pshared != PTHREAD_PROCESS_SHARED))
     {
-      spin_lock_init(&lock->sp_lock);
-      lock->sp_holder = IMPOSSIBLE_THREAD;
-      ret             = OK;
+      return EINVAL;
     }
 
-  return ret;
+  atomic_set(SPIN_WORD(lock), 0);
+  lock->sp_holder = IMPOSSIBLE_THREAD;
+  return OK;
 }
 
 /****************************************************************************
  * Name: pthread_spin_destroy
  *
  * Description:
- *   The pthread_spin_destroy() function will destroy the spin lock
- *   referenced by lock and release any resources used by the lock.  The
- *   effect of subsequent use of the lock is undefined until the lock is
- *   reinitialized by another call to pthread_spin_init(). The results are
- *   undefined if pthread_spin_destroy() is called when a thread holds the
- *   lock, or if this function is called with an uninitialized thread spin
- *   lock.
+ *   Destroy the spin lock.  A spin lock has no resources to release.
  *
  * Input Parameters:
- *   lock    - A reference to the spinlock object to be initialized.
- *   pshared - Unused
+ *   lock - A reference to the spinlock object.
  *
  * Returned Value:
  *   Zero (OK) is always returned.
  *
- * POSIX Compatibility:
- *   Not supported: "If the Thread Process-Shared Synchronization option is
- *   supported and the value of pshared is PTHREAD_PROCESS_SHARED, the
- *   implementation shall permit the spin lock to be operated upon by any
- *   thread that has access to the memory where the spin lock is allocated,
- *   even if it is allocated in memory that is shared by multiple
- *   processes."
- *
  ****************************************************************************/
 
-int pthread_spin_destroy(pthread_spinlock_t *lock)
+int pthread_spin_destroy(FAR pthread_spinlock_t *lock)
 {
   return OK;
 }
 
 /****************************************************************************
- * Name: pthread_spin_lock
+ * Name: pthread_spin_trylock
  *
  * Description:
- *   The pthread_spin_lock() function will lock the spin lock referenced by
- *   lock. The calling thread will acquire the lock if it is not held by
- *   another thread. Otherwise, the thread will spin (that is, will not
- *   return from the pthread_spin_lock() call) until the lock becomes
- *   available.
- *
- *   If it is determined the value specified by the lock argument to
- *   pthread_spin_lock() refers to a spin lock object for which the calling
- *   thread already holds the lock, it is recommended that the function will
- *   fail and report an EDEADLK error.
+ *   Lock the spin lock if no thread holds it.
  *
  * Input Parameters:
  *   lock - A reference to the spinlock object to lock.
  *
  * Returned Value:
- *   Zero is returned if the lock was successfully acquired.  Otherwise one
- *   of the following errno values are returned:
- *
- *   EINVAL    - 'lock' does not refer to a valid spinlock object
- *   EDEADLOCK - The caller already holds the spinlock
+ *   Zero if the lock was acquired; EINVAL if 'lock' is not valid; EBUSY if
+ *   a thread (also the caller) holds the lock.
  *
  ****************************************************************************/
 
-int pthread_spin_lock(pthread_spinlock_t *lock)
+int pthread_spin_trylock(FAR pthread_spinlock_t *lock)
 {
-  struct boardioc_spinlock_s spinlock;
-  pthread_t me = pthread_self();
-  int ret;
+  int32_t expected = 0;
 
-  DEBUGASSERT(lock != NULL);
   if (lock == NULL)
     {
       return EINVAL;
     }
-  else if (lock->sp_holder == me)
-    {
-      return EDEADLOCK;
-    }
-
-  spinlock.action = BOARDIOC_SPINLOCK_LOCK;
-  spinlock.lock = &lock->sp_lock;
-  spinlock.flags = NULL;
-  ret = boardctl(BOARDIOC_SPINLOCK, (uintptr_t)&spinlock);
-
-  /* Check for success (previous state was SP_UNLOCKED) */
 
-  if (ret == 0)
+  if (!atomic_cmpxchg_acquire(SPIN_WORD(lock), &expected, 1))
     {
-      lock->sp_holder = me;
-    }
-  else
-    {
-      /* An error of some kind is the only other possibility */
-
-      DEBUGASSERT(ret < 0);
-      ret = get_errno();
+      return EBUSY;
     }
 
-  return ret;
+  lock->sp_holder = pthread_self();
+  return OK;
 }
 
 /****************************************************************************
- * Name: pthread_spin_trylock
+ * Name: pthread_spin_lock
  *
  * Description:
- *   The pthread_spin_trylock() function will lock the spin lock referenced
- *   by lock. The calling thread will acquire the lock if it is not held by
- *   another thread. Otherwise, The pthread_spin_trylock() will return a
- *   failure.
+ *   Lock the spin lock.  The thread spins while another thread holds the
+ *   lock, and yields the CPU after SPIN_TRIES tests, so that a holder that
+ *   was preempted can run.
  *
  * Input Parameters:
  *   lock - A reference to the spinlock object to lock.
  *
  * Returned Value:
- *   Zero is returned if the lock was successfully acquired.  Otherwise one
- *   of the following errno values are returned:
- *
- *   EINVAL - 'lock' does not refer to a valid spinlock object
- *   EBUSY  - The spinlock is held by a thread, also by the caller
+ *   Zero if the lock was acquired; EINVAL if 'lock' is not valid; EDEADLK
+ *   if the caller already holds the lock.
  *
  ****************************************************************************/
 
-int pthread_spin_trylock(pthread_spinlock_t *lock)
+int pthread_spin_lock(FAR pthread_spinlock_t *lock)
 {
-  struct boardioc_spinlock_s spinlock;
-  pthread_t me = pthread_self();
-  int ret;
+  int tries = 0;
 
-  DEBUGASSERT(lock != NULL);
   if (lock == NULL)
     {
-      ret = EINVAL;
+      return EINVAL;
     }
-  else if (lock->sp_holder == me)
+
+  if (lock->sp_holder == pthread_self())
     {
-      ret = EBUSY;
+      return EDEADLK;
     }
-  else
+
+  while (pthread_spin_trylock(lock) != OK)
     {
-      /* Perform the test/set operation via boardctl() */
+      /* Wait with plain reads, not with atomic writes */
 
-      spinlock.action = BOARDIOC_SPINLOCK_TRYLOCK;

Review Comment:
   let's remove BOARDIOC_SPINLOCK_ related code with a new patch too



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to