summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShell <[email protected]>2024-10-31 11:56:23 +0800
committerRbb666 <[email protected]>2024-11-03 10:08:45 +0800
commit285fee9d4cb6647148476bbdd3bf0796f9e3c6f9 (patch)
treed3a4a2dbe731d0ba2a96801f9a2d589a78d29555
parent40cd8cce995e24ea265664c5db127c662a747032 (diff)
feat: rtatomic: lockless single linked list
Signed-off-by: Shell <[email protected]>
-rw-r--r--include/rtatomic.h33
-rw-r--r--include/rttypes.h9
2 files changed, 42 insertions, 0 deletions
diff --git a/include/rtatomic.h b/include/rtatomic.h
index 1c8cde5396..4719339d26 100644
--- a/include/rtatomic.h
+++ b/include/rtatomic.h
@@ -230,6 +230,39 @@ rt_inline rt_bool_t rt_atomic_inc_not_zero(volatile rt_atomic_t *ptr)
return rt_atomic_add_unless(ptr, 1, 0);
}
+/**
+ * @brief initialize a lock-less single list
+ *
+ * @param l the single list to be initialized
+ */
+rt_inline void rt_ll_slist_init(rt_ll_slist_t *l)
+{
+ l->next = 0;
+}
+
+rt_inline void rt_ll_slist_enqueue(rt_ll_slist_t *l, rt_ll_slist_t *n)
+{
+ rt_base_t exp;
+ exp = rt_atomic_load(&l->next);
+ do
+ {
+ n->next = exp;
+ } while (!rt_atomic_compare_exchange_strong(&l->next, &exp, (rt_base_t)n));
+}
+
+rt_inline rt_ll_slist_t *rt_ll_slist_dequeue(rt_ll_slist_t *l)
+{
+ rt_base_t exp;
+ rt_ll_slist_t *head;
+
+ exp = rt_atomic_load(&l->next);
+ do
+ {
+ head = (rt_ll_slist_t *)exp;
+ } while (head && !rt_atomic_compare_exchange_strong(&l->next, &exp, rt_atomic_load(&head->next)));
+ return head;
+}
+
#endif /* __cplusplus */
#endif /* __RT_ATOMIC_H__ */
diff --git a/include/rttypes.h b/include/rttypes.h
index 0b614a492a..75bf58d52f 100644
--- a/include/rttypes.h
+++ b/include/rttypes.h
@@ -133,6 +133,15 @@ struct rt_slist_node
typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
/**
+ * Lock-less Single List structure
+ */
+struct rt_lockless_slist_node
+{
+ rt_atomic_t next; /**< point to next node. */
+};
+typedef struct rt_lockless_slist_node rt_ll_slist_t; /**< Type for lock-les single list. */
+
+/**
* Spinlock
*/
#ifdef RT_USING_SMP