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


##########
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);

Review Comment:
   > There can be multiple implementations of spinlocks to improve SMP 
performance, such as queued spinlocks or MCS spinlocks. If we use atomic 
operations, do we plan to implement a similar locking mechanism for seqlock in 
the future, or is it better to directly use spinlocks instead?
   
   1. Firstly, the performance of queuing-based locks cannot be higher than 
that of contention-based locks; this is determined by the characteristics of 
the bus.
   
   2. Part of my graduation thesis presents a composable blocking 
synchronization framework with zero performance overhead. If there is a need 
for composable locks in the future, I can implement my design in NuttX.
   



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