Skip to content

Commit 285fee9

Browse files
polarvidRbb666
authored andcommitted
feat: rtatomic: lockless single linked list
Signed-off-by: Shell <[email protected]>
1 parent 40cd8cc commit 285fee9

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

include/rtatomic.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,39 @@ rt_inline rt_bool_t rt_atomic_inc_not_zero(volatile rt_atomic_t *ptr)
230230
return rt_atomic_add_unless(ptr, 1, 0);
231231
}
232232

233+
/**
234+
* @brief initialize a lock-less single list
235+
*
236+
* @param l the single list to be initialized
237+
*/
238+
rt_inline void rt_ll_slist_init(rt_ll_slist_t *l)
239+
{
240+
l->next = 0;
241+
}
242+
243+
rt_inline void rt_ll_slist_enqueue(rt_ll_slist_t *l, rt_ll_slist_t *n)
244+
{
245+
rt_base_t exp;
246+
exp = rt_atomic_load(&l->next);
247+
do
248+
{
249+
n->next = exp;
250+
} while (!rt_atomic_compare_exchange_strong(&l->next, &exp, (rt_base_t)n));
251+
}
252+
253+
rt_inline rt_ll_slist_t *rt_ll_slist_dequeue(rt_ll_slist_t *l)
254+
{
255+
rt_base_t exp;
256+
rt_ll_slist_t *head;
257+
258+
exp = rt_atomic_load(&l->next);
259+
do
260+
{
261+
head = (rt_ll_slist_t *)exp;
262+
} while (head && !rt_atomic_compare_exchange_strong(&l->next, &exp, rt_atomic_load(&head->next)));
263+
return head;
264+
}
265+
233266
#endif /* __cplusplus */
234267

235268
#endif /* __RT_ATOMIC_H__ */

include/rttypes.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ struct rt_slist_node
132132
};
133133
typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
134134

135+
/**
136+
* Lock-less Single List structure
137+
*/
138+
struct rt_lockless_slist_node
139+
{
140+
rt_atomic_t next; /**< point to next node. */
141+
};
142+
typedef struct rt_lockless_slist_node rt_ll_slist_t; /**< Type for lock-les single list. */
143+
135144
/**
136145
* Spinlock
137146
*/

0 commit comments

Comments
 (0)