Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
acassis merged PR #17569: URL: https://github.com/apache/nuttx/pull/17569 -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
xiaoxiang781216 commented on PR #17569: URL: https://github.com/apache/nuttx/pull/17569#issuecomment-3682052182 @acassis please review the documentation -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639577166
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639555647
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639555647
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639553459
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639263287
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
xiaoxiang781216 commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639267890
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
xiaoxiang781216 commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639260014
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639185107
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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);
Review Comment:
Thank you for the reminder, already fixed.
--
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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639167372
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639099258
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2639096427
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569: URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638921208 ## sched/Kconfig: ## @@ -328,6 +328,13 @@ config RW_SPINLOCK Reader can take read lock simultaneously and only one writer can take write lock. +config SPINLOCK_BASED_SEQCOUNT Review Comment: OK, removed. -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638894633
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
> I'm afraid I can't agree with your view. This stems from different design
considerations:
>
> * NuttX targets for **embedded devices, where memory is precious**. Even a
4-byte overhead per seqlock should be optimized. **Linux is designed for
general-purpose systems**; sacrificing some performance and memory footprint
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638894633
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
> Even so, I added a spinlock-based seqcount implementation that can be
enabled via macros `CONFIG_SPINLOCK_BASED_SEQCOUNT`. This is valuable when
seqcount writers are required to be non-starving.
Even so, I added a spinlock-based seqcount implementation that can be
enabled via macros `CONFIG_SPINLOCK_BASED_SEQ
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638894633
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
> Contributor
Even so, I added a spinlock-based seqcount implementation that can be
enabled via macros `CONFIG_SPINLOCK_BASED_SEQCOUNT`. This is valuable when
seqcount writers are required to be non-starving.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to Git
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
xiaoxiang781216 commented on code in PR #17569: URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638894602 ## sched/Kconfig: ## @@ -328,6 +328,13 @@ config RW_SPINLOCK Reader can take read lock simultaneously and only one writer can take write lock. +config SPINLOCK_BASED_SEQCOUNT Review Comment: what's benifit to add a new config? either using spinlock or atomic is fine, but don't add the new option to make the thing unnecessary complex. -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
xiaoxiang781216 commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638874887
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638856780
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
I'm afraid I can't agree with your view. This stems from different design
considerations:
- NuttX targets for **embedded devices, where memory is precious**. Even a
4-byte overhead per seqlock should be optimized. **Linux is designed for
general-purpose systems**; sacrificing some performance and memory footprint to
impro
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638824326
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638819875
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
Since you already understand this rationale, I also believe you know that
Linux implements seqlocks using independent spinlocks as well. In that case,
would it be better to replace the atomic operations here with spinlocks? This
way, we won’t have to rework the code in the future.
--
This is an automated message from the Apa
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638817369
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
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
+#include
+#include
+#include
+#include
+
+/
+ * 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 queuin
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638794016
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638569005
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638569005
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638562783
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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?
--
This is an automated message from the Apac
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638559336
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638549148
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
From the write lock performance perspective, the current `seqcount`
implementation is not significantly different from the `spinlock`
implementation. According to the performance test results, with `SMP_NCPUS=2
and 3`, it even outperforms `spinlock` in throughput while using 4 fewer bytes
of memory. The code size increase is
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638548545
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638545595
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638531384
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
For support SMP CPUs, I think this memory overhead is well within control.
My concern is that if we implement this solely with atomic operations,
whether the introduction of efficient spinlock implementation in the future can
be applied to the current seqlock.
--
This is an automated message from the Apache Git Service
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on PR #17569: URL: https://github.com/apache/nuttx/pull/17569#issuecomment-3680256688 > I support your point. If all these commits were completed by me alone, I would definitely consolidate them into a few commits (as I've done in past PRs). > > However, these PR also include commits of some of my colleagues, such as @hujun260 (who proposed the first seqcount implementation in this PR), @wangzhi16 and @zhangyuduck consolidating their commits would be disrespectful to their work. > > So I'm sorry that I can not make this change. Okay, let's respect the work achievements of every individual. -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on PR #17569: URL: https://github.com/apache/nuttx/pull/17569#issuecomment-3680243041 > This commit has too many patches. Could it be consolidated into several focused commits based on feature sets, such as arch, seqlock, and doc? This is just a suggestion; you can keep it if you wish. > > https://private-user-images.githubusercontent.com/758493/529058953-421a440b-2c02-4e80-afa5-33828f90c17b.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjYzNzI1ODUsIm5iZiI6MTc2NjM3MjI4NSwicGF0aCI6Ii83NTg0OTMvNTI5MDU4OTUzLTQyMWE0NDBiLTJjMDItNGU4MC1hZmE1LTMzODI4ZjkwYzE3Yi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUxMjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MTIyMlQwMjU4MDVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05Y2E4ZjAwY2I5Y2VmNWI1NGRlYWE2MmEwYzVmMjRiMjBmMGZmZTcxY2Y4YmIxNDE5NmNlNDlmYjZkZTlhNWUzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.QZbt2N34boILPzjx6eJDVsyeNn76_6k1B7DTIU7C_Q4";> https://private-user-images.githubusercontent.com/758493/529058986-f17bb454-5673-4274-95d8-bbb6cbfa9c34.png?jwt=eyJ0eXAiOiJ KV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjYzNzI1ODUsIm5iZiI6MTc2NjM3MjI4NSwicGF0aCI6Ii83NTg0OTMvNTI5MDU4OTg2LWYxN2JiNDU0LTU2NzMtNDI3NC05NWQ4LWJiYjZjYmZhOWMzNC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUxMjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MTIyMlQwMjU4MDVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jY2NiN2RiYTU4NTg4Y2VmZTkyYzcxMTNiMzE2ZTgxMDFmMGZmMzVlMWRiY2UzMjc0YzEzYjRhZmExNWFlYzc3JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.PU285h0MgBcqe1AcaD7CNHzzdXdyWTETPihZ1iuQdcY"> I support your point. If all these commits were completed by me alone, I would definitely consolidate them into a few commits (as I've done in past PRs). However, these PR also include commits of some of my colleagues, such as @hujun260, @wangzhi16 and @zhangyuduck consolidating their commits would be disrespectful to their work. So I'm sorry that I can not make this change. -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638510297
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
Sorry, I'm not sure if I misunderstood: Are you suggesting adding a
`spinlock` to the seqcount and using it as the write lock?
We actually tried this design in an early prototype of our `seqcount`, but
we found it resulted in higher memory overhead (extra 4 bytes per `seqcount`).
--
This is an automated message from the
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638504249
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638479419
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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;
+}
+
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on PR #17569: URL: https://github.com/apache/nuttx/pull/17569#issuecomment-3680143455 This commit has too many patches. Could it be consolidated into several focused commits based on feature sets, such as arch, seqlock, and doc? This is just a suggestion; you can keep it if you wish. https://github.com/user-attachments/assets/421a440b-2c02-4e80-afa5-33828f90c17b"; /> https://github.com/user-attachments/assets/f17bb454-5673-4274-95d8-bbb6cbfa9c34"; /> -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
anchao commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2638463722
##
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
+#include
+#include
+#include
+#include
+
+/
+ * 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:
why not implement atomic opt by spinlock? just treat sequence as lock to
save lock member?
##
include/nuttx/seqlock.h:
##
@@ -0,0 +1,214 @@
+/
+ * include/nuttx/seqlock.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to t
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on PR #17569: URL: https://github.com/apache/nuttx/pull/17569#issuecomment-3680126059 > @Fix-Point thank you very much for this well detailed Summary and testing, however new feature requires new Documentation, otherwise we ending with more hidden features. The documentation has been added. Please check if it is sufficiently detailed. If anything needs to be added, please point it out. -- 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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
xiaoxiang781216 commented on code in PR #17569:
URL: https://github.com/apache/nuttx/pull/17569#discussion_r2637662334
##
include/nuttx/spinlock_type.h:
##
@@ -92,6 +96,13 @@ typedef union rspinlock_u
};
} rspinlock_t;
+#define SEQLOCK_INITIALIZER { 0u }
+
+typedef struct seqclock
+{
+ uint32_t sequence;
Review Comment:
why change from atomic_t to uint32_t
--
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]
Re: [PR] include/nuttx: Introduce seqlock, a sequential count based read-write lock. [nuttx]
Fix-Point commented on PR #17569: URL: https://github.com/apache/nuttx/pull/17569#issuecomment-3677405224 > @Fix-Point thank you very much for this well detailed Summary and testing, however new feature requires new Documentation, otherwise we ending with more hidden features. Thank you for the reminder. I will add the document in the next few days. -- 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]
