1 /*
2 * Copyright (c) 2019-2020 Jonathan Gray <jsg@openbsd.org>
3 * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #ifndef _LINUX_LLIST_H_
26 #define _LINUX_LLIST_H_
27
28 #include <linux/atomic.h>
29 #include <linux/kernel.h>
30
31 struct llist_node {
32 struct llist_node *next;
33 };
34
35 struct llist_head {
36 struct llist_node *first;
37 };
38
39 #define llist_entry(ptr, type, member) \
40 ((ptr) ? container_of(ptr, type, member) : NULL)
41
42 static inline struct llist_node *
llist_del_all(struct llist_head * head)43 llist_del_all(struct llist_head *head)
44 {
45 return atomic_swap_ptr((void *)&head->first, NULL);
46 }
47
48 static inline struct llist_node *
llist_del_first(struct llist_head * head)49 llist_del_first(struct llist_head *head)
50 {
51 struct llist_node *first, *next;
52
53 do {
54 first = head->first;
55 if (first == NULL)
56 return NULL;
57 next = first->next;
58 } while (atomic_cas_ptr(&head->first, first, next) != first);
59
60 return first;
61 }
62
63 static inline bool
llist_add(struct llist_node * new,struct llist_head * head)64 llist_add(struct llist_node *new, struct llist_head *head)
65 {
66 struct llist_node *first = READ_ONCE(head->first);
67
68 do {
69 new->next = first;
70 } while (cmpxchg(&head->first, first, new) != first);
71
72 return (first == NULL);
73 }
74
75 static inline void
init_llist_head(struct llist_head * head)76 init_llist_head(struct llist_head *head)
77 {
78 head->first = NULL;
79 }
80
81 static inline bool
llist_empty(struct llist_head * head)82 llist_empty(struct llist_head *head)
83 {
84 return (head->first == NULL);
85 }
86
87 #define llist_for_each_entry_safe(pos, n, node, member) \
88 for (pos = llist_entry((node), __typeof(*pos), member); \
89 pos != NULL && \
90 (n = llist_entry(pos->member.next, __typeof(*pos), member), pos); \
91 pos = n)
92
93 #define llist_for_each_entry(pos, node, member) \
94 for ((pos) = llist_entry((node), __typeof(*(pos)), member); \
95 (pos) != NULL; \
96 (pos) = llist_entry((pos)->member.next, __typeof(*(pos)), member))
97
98 #endif /* _LINUX_LLIST_H_ */
99