xref: /freebsd/sys/compat/linuxkpi/common/include/linux/llist.h (revision 307f78f3ed90a4145eeb2c8cc79bc95b2666f57a)
1ec25b6faSVladimir Kondratyev /* Public domain. */
2ec25b6faSVladimir Kondratyev 
3*307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_LLIST_H
4*307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_LLIST_H
5ec25b6faSVladimir Kondratyev 
6ec25b6faSVladimir Kondratyev #include <sys/types.h>
7ec25b6faSVladimir Kondratyev #include <machine/atomic.h>
8ec25b6faSVladimir Kondratyev 
9ec25b6faSVladimir Kondratyev struct llist_node {
10ec25b6faSVladimir Kondratyev 	struct llist_node *next;
11ec25b6faSVladimir Kondratyev };
12ec25b6faSVladimir Kondratyev 
13ec25b6faSVladimir Kondratyev struct llist_head {
14ec25b6faSVladimir Kondratyev 	struct llist_node *first;
15ec25b6faSVladimir Kondratyev };
16ec25b6faSVladimir Kondratyev 
17ec25b6faSVladimir Kondratyev #define	LLIST_HEAD_INIT(name)	{ NULL }
18ec25b6faSVladimir Kondratyev #define	LLIST_HEAD(name)	struct llist_head name = LLIST_HEAD_INIT(name)
19ec25b6faSVladimir Kondratyev 
20ec25b6faSVladimir Kondratyev #define llist_entry(ptr, type, member) \
21ec25b6faSVladimir Kondratyev 	((ptr) ? container_of(ptr, type, member) : NULL)
22ec25b6faSVladimir Kondratyev 
23ec25b6faSVladimir Kondratyev static inline struct llist_node *
llist_del_all(struct llist_head * head)24ec25b6faSVladimir Kondratyev llist_del_all(struct llist_head *head)
25ec25b6faSVladimir Kondratyev {
26ec25b6faSVladimir Kondratyev 	return ((void *)atomic_readandclear_ptr((uintptr_t *)&head->first));
27ec25b6faSVladimir Kondratyev }
28ec25b6faSVladimir Kondratyev 
29ec25b6faSVladimir Kondratyev static inline struct llist_node *
llist_del_first(struct llist_head * head)30ec25b6faSVladimir Kondratyev llist_del_first(struct llist_head *head)
31ec25b6faSVladimir Kondratyev {
32ec25b6faSVladimir Kondratyev 	struct llist_node *first, *next;
33ec25b6faSVladimir Kondratyev 
34ec25b6faSVladimir Kondratyev 	do {
35ec25b6faSVladimir Kondratyev 		first = head->first;
36ec25b6faSVladimir Kondratyev 		if (first == NULL)
37ec25b6faSVladimir Kondratyev 			return NULL;
38ec25b6faSVladimir Kondratyev 		next = first->next;
39ec25b6faSVladimir Kondratyev 	} while (atomic_cmpset_ptr((uintptr_t *)&head->first,
40ec25b6faSVladimir Kondratyev 	    (uintptr_t)first, (uintptr_t)next) == 0);
41ec25b6faSVladimir Kondratyev 
42ec25b6faSVladimir Kondratyev 	return (first);
43ec25b6faSVladimir Kondratyev }
44ec25b6faSVladimir Kondratyev 
45ec25b6faSVladimir Kondratyev static inline bool
llist_add(struct llist_node * new,struct llist_head * head)46ec25b6faSVladimir Kondratyev llist_add(struct llist_node *new, struct llist_head *head)
47ec25b6faSVladimir Kondratyev {
48ec25b6faSVladimir Kondratyev 	struct llist_node *first;
49ec25b6faSVladimir Kondratyev 
50ec25b6faSVladimir Kondratyev 	do {
51ec25b6faSVladimir Kondratyev 		new->next = first = head->first;
52ec25b6faSVladimir Kondratyev 	} while (atomic_cmpset_ptr((uintptr_t *)&head->first,
53ec25b6faSVladimir Kondratyev 	    (uintptr_t)first, (uintptr_t)new) == 0);
54ec25b6faSVladimir Kondratyev 
55ec25b6faSVladimir Kondratyev 	return (first == NULL);
56ec25b6faSVladimir Kondratyev }
57ec25b6faSVladimir Kondratyev 
58ec25b6faSVladimir Kondratyev static inline bool
llist_add_batch(struct llist_node * new_first,struct llist_node * new_last,struct llist_head * head)59ec25b6faSVladimir Kondratyev llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
60ec25b6faSVladimir Kondratyev     struct llist_head *head)
61ec25b6faSVladimir Kondratyev {
62ec25b6faSVladimir Kondratyev 	struct llist_node *first;
63ec25b6faSVladimir Kondratyev 
64ec25b6faSVladimir Kondratyev 	do {
65ec25b6faSVladimir Kondratyev 		new_last->next = first = head->first;
66ec25b6faSVladimir Kondratyev 	} while (atomic_cmpset_ptr((uintptr_t *)&head->first,
67ec25b6faSVladimir Kondratyev 	    (uintptr_t)first, (uintptr_t)new_first) == 0);
68ec25b6faSVladimir Kondratyev 
69ec25b6faSVladimir Kondratyev 	return (first == NULL);
70ec25b6faSVladimir Kondratyev }
71ec25b6faSVladimir Kondratyev 
72ec25b6faSVladimir Kondratyev static inline void
init_llist_head(struct llist_head * head)73ec25b6faSVladimir Kondratyev init_llist_head(struct llist_head *head)
74ec25b6faSVladimir Kondratyev {
75ec25b6faSVladimir Kondratyev 	head->first = NULL;
76ec25b6faSVladimir Kondratyev }
77ec25b6faSVladimir Kondratyev 
78ec25b6faSVladimir Kondratyev static inline bool
llist_empty(struct llist_head * head)79ec25b6faSVladimir Kondratyev llist_empty(struct llist_head *head)
80ec25b6faSVladimir Kondratyev {
81ec25b6faSVladimir Kondratyev 	return (head->first == NULL);
82ec25b6faSVladimir Kondratyev }
83ec25b6faSVladimir Kondratyev 
84ec25b6faSVladimir Kondratyev #define llist_for_each_safe(pos, n, node)				\
85ec25b6faSVladimir Kondratyev 	for ((pos) = (node);						\
86ec25b6faSVladimir Kondratyev 	    (pos) != NULL &&						\
87ec25b6faSVladimir Kondratyev 	    ((n) = (pos)->next, pos);					\
88ec25b6faSVladimir Kondratyev 	    (pos) = (n))
89ec25b6faSVladimir Kondratyev 
90ec25b6faSVladimir Kondratyev #define llist_for_each_entry_safe(pos, n, node, member) 		\
91ec25b6faSVladimir Kondratyev 	for (pos = llist_entry((node), __typeof(*pos), member); 	\
92ec25b6faSVladimir Kondratyev 	    pos != NULL &&						\
93ec25b6faSVladimir Kondratyev 	    (n = llist_entry(pos->member.next, __typeof(*pos), member), pos); \
94ec25b6faSVladimir Kondratyev 	    pos = n)
95ec25b6faSVladimir Kondratyev 
96ec25b6faSVladimir Kondratyev #define llist_for_each_entry(pos, node, member)				\
97ec25b6faSVladimir Kondratyev 	for ((pos) = llist_entry((node), __typeof(*(pos)), member);	\
98ec25b6faSVladimir Kondratyev 	    (pos) != NULL;						\
99ec25b6faSVladimir Kondratyev 	    (pos) = llist_entry((pos)->member.next, __typeof(*(pos)), member))
100ec25b6faSVladimir Kondratyev 
101ec25b6faSVladimir Kondratyev #endif
102