Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638829200


##########
include/nuttx/seqlock.h:
##########
@@ -0,0 +1,214 @@
+/****************************************************************************
+ * include/nuttx/seqlock.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_SEQLOCK_H
+#define __INCLUDE_NUTTX_SEQLOCK_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/arch.h>
+#include <nuttx/atomic.h>
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <nuttx/spinlock_type.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: seqlock_init
+ *
+ * Description:
+ *   init seqlock
+ *
+ * Input Parameters:
+ *   seqcount_t
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static inline_function void seqlock_init(FAR seqcount_t *s)
+{
+  s->sequence = 0u;
+#ifdef CONFIG_SMP
+  SMP_WMB();
+#endif
+}
+
+/****************************************************************************
+ * Name: read_seqbegin
+ *
+ * Description:
+ *   This is a primitive counting synchronization mechanism
+ *   that enables lock-free reading.
+ *
+ * Input Parameters:
+ *   seqcount_t
+ *
+ * Returned Value:
+ *   seq - Used to determine whether the state has changed during reading.
+ *
+ ****************************************************************************/
+
+static inline_function
+uint32_t read_seqbegin(FAR const seqcount_t *s)
+{
+  uint32_t seq;
+
+#ifdef CONFIG_SMP
+  seq = atomic_read_acquire((atomic_t *)&s->sequence) & (~1u);
+#else
+  seq = s->sequence;
+  SMP_RMB();
+#endif
+  return seq;
+}
+
+/****************************************************************************
+ * Name: read_seqretry
+ *
+ * Description:
+ *   This is a primitive counting synchronization mechanism
+ *   that enables lock-free reading.
+ *
+ * Input Parameters:
+ *   seqcount_t
+ *   start - Used to determine whether the state has changed during reading.
+ *
+ * Returned Value:
+ *   0 indicate need retry
+ *
+ ****************************************************************************/
+
+static inline_function
+uint32_t read_seqretry(FAR const seqcount_t *s, uint32_t start)
+{
+  uint32_t seq;
+
+  /* Ensure all load operations before are completed. */
+
+  SMP_RMB();
+
+#ifdef CONFIG_SMP
+  seq = atomic_read((atomic_t *)&s->sequence);
+#else
+  seq = s->sequence;
+#endif
+
+  return predict_false(seq != start);
+}
+
+/****************************************************************************
+ * Name: write_seqlock_irqsave
+ *
+ * Description:
+ *   This is a primitive counting synchronization mechanism
+ *   that enables lock-free reading. write need spinlock to protect
+ *
+ * Input Parameters:
+ *   seqcount_t
+ *
+ * Returned Value:
+ *   irqstate
+ *
+ ****************************************************************************/
+
+static inline_function
+irqstate_t write_seqlock_irqsave(FAR seqcount_t *s)
+{
+  irqstate_t flags = up_irq_save();
+
+#ifdef CONFIG_SMP
+  for (; ; )
+    {
+      uint32_t sequence = atomic_read((atomic_t *)&s->sequence);
+
+      if (predict_true((sequence & 1u) == 0u))
+        {
+          /* Try to acquire the lock ownership. */
+
+          if (atomic_cmpxchg_acquire((atomic_t *)&s->sequence,
+                                     (atomic_t *)&sequence,
+                                     sequence + 1u))
+            {
+              break;
+            }
+        }
+
+      /* CPU Relax and retry. */
+    }
+#else
+  s->sequence++;

Review Comment:
   > ```shell
   > Thread A (writer)    | Thread B (Reader)
   > acuqired seqlock     |
   >                      | Read Shared data A.low 0xDEADBEEF
   >                      | Interrupted.
   > update A.low to 0x1  | 
   > update A.high to 0x2 | Read Shared data A.high 0x2 (Get A = 0x2DEADBEEF, 
which is incorrect).
   > release seqlock      |
   > ```
   
   Please note that the reader does not acquire a lock. In your implementation, 
the reader needs to acquire a lock (interrupts disabled). This clears the CPU 
pipeline, impacting reader performance, which contradicts the goal of seqcount 
in optimizing reader performance.
   
   If you do not disable IRQ for the reader, there is still incorrect 
interleaving:
   
   ```bash
   Thread A (writer)    | Thread B (Reader)
                        | Read Shared data A.low 0xDEADBEEF
                        | Interrupted to Thread A
   acuqire seqlock      |
   update A.low to 0x1  |
   update A.high to 0x2 |
   release seqlock      |
   ....                 |
   Return to thread B   |
                        | Read Shared data A.high 0x2 (Get A = 0x2DEADBEEF, 
which is incorrect).
   ```



-- 
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