llist.h revision 1.5 1 /* $NetBSD: llist.h,v 1.5 2021/12/19 11:39:24 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LINUX_LLIST_H_
33 #define _LINUX_LLIST_H_
34
35 #include <sys/atomic.h>
36 #include <sys/null.h>
37
38 struct llist_head {
39 struct llist_node *first;
40 };
41
42 struct llist_node {
43 struct llist_node *next;
44 };
45
46 static inline void
47 init_llist_head(struct llist_head *head)
48 {
49
50 head->first = NULL;
51 }
52
53 #define llist_entry(NODE, TYPE, FIELD) container_of(NODE, TYPE, FIELD)
54
55 static inline bool
56 llist_empty(struct llist_head *head)
57 {
58 bool empty;
59
60 empty = (atomic_load_acquire(&head->first) == NULL);
61
62 return empty;
63 }
64
65 static inline bool
66 llist_add(struct llist_node *node, struct llist_head *head)
67 {
68 struct llist_node *first;
69
70 do {
71 first = head->first;
72 node->next = first;
73 membar_exit();
74 } while (atomic_cas_ptr(&head->first, first, node) != first);
75
76 return first == NULL;
77 }
78
79 static inline struct llist_node *
80 llist_del_all(struct llist_head *head)
81 {
82 struct llist_node *first;
83
84 first = atomic_swap_ptr(&head->first, NULL);
85 membar_enter();
86
87 return first;
88 }
89
90 static inline struct llist_node *
91 llist_del_first(struct llist_head *head)
92 {
93 struct llist_node *first;
94
95 do {
96 first = atomic_load_consume(&head->first);
97 if (first == NULL)
98 return NULL;
99 } while (atomic_cas_ptr(&head->first, first, first->next)
100 != first);
101
102 return first;
103 }
104
105 #define _llist_next(ENTRY, FIELD) \
106 ({ \
107 __typeof__((ENTRY)->FIELD.next) _NODE = \
108 atomic_load_consume(&(ENTRY)->FIELD.next); \
109 (_NODE == NULL ? NULL : \
110 llist_entry(_NODE, __typeof__(*(ENTRY)), FIELD)); \
111 })
112
113 #define llist_for_each_safe(NODE, TMP, HEAD) \
114 for ((NODE) = (HEAD); \
115 (NODE) && ((TMP) = (NODE)->next, 1); \
116 (NODE) = (TMP))
117
118 #define llist_for_each_entry(ENTRY, NODE, FIELD) \
119 for ((ENTRY) = ((NODE) == NULL ? NULL : \
120 (membar_datadep_consumer(), \
121 llist_entry(NODE, typeof(*(ENTRY)), FIELD))); \
122 (ENTRY) != NULL; \
123 (ENTRY) = _llist_next(ENTRY, FIELD))
124
125 #define llist_for_each_entry_safe(ENTRY, TMP, NODE, FIELD) \
126 for ((ENTRY) = ((NODE) == NULL ? NULL : \
127 (membar_datadep_consumer(), \
128 llist_entry(NODE, typeof(*(ENTRY)), FIELD))); \
129 ((ENTRY) == NULL ? 0 : \
130 ((TMP) = _llist_next(ENTRY, FIELD), 1)); \
131 (ENTRY) = (TMP))
132
133 #endif /* _LINUX_LLIST_H_ */
134