2 * Copyright (c) 2019 Jonathan Gray <jsg@openbsd.org>
3 * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org>
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:
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
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
25 #ifndef _LINUX_LLIST_H_
26 #define _LINUX_LLIST_H_
28 #include <linux/atomic.h>
29 #include <linux/kernel.h>
32 struct llist_node *next;
36 struct llist_node *first;
39 #define llist_entry(ptr, type, member) \
40 ((ptr) ? container_of(ptr, type, member) : NULL)
42 static inline struct llist_node *
43 llist_del_all(struct llist_head *head)
45 return atomic_swap_ptr((void *)&head->first, NULL);
49 llist_add(struct llist_node *new, struct llist_head *head)
51 struct llist_node *first = READ_ONCE(head->first);
55 } while (cmpxchg(&head->first, first, new) != first);
57 return (first == NULL);
61 init_llist_head(struct llist_head *head)
67 llist_empty(struct llist_head *head)
69 return (head->first == NULL);
72 #define llist_for_each_entry_safe(pos, n, node, member) \
73 for (pos = llist_entry((node), __typeof(*pos), member); \
75 (n = llist_entry(pos->member.next, __typeof(*pos), member), pos); \
78 #endif /* _LINUX_LLIST_H_ */