xref: /dragonfly/sys/dev/drm/include/linux/llist.h (revision 9317c2d0)
1 /*
2  * Copyright (c) 2019 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 *
43 llist_del_all(struct llist_head *head)
44 {
45 	return atomic_swap_ptr((void *)&head->first, NULL);
46 }
47 
48 static inline bool
49 llist_add(struct llist_node *new, struct llist_head *head)
50 {
51 	struct llist_node *first = READ_ONCE(head->first);
52 
53 	do {
54 		new->next = first;
55 	} while (cmpxchg(&head->first, first, new) != first);
56 
57 	return (first == NULL);
58 }
59 
60 static inline void
61 init_llist_head(struct llist_head *head)
62 {
63 	head->first = NULL;
64 }
65 
66 static inline bool
67 llist_empty(struct llist_head *head)
68 {
69 	return (head->first == NULL);
70 }
71 
72 #define llist_for_each_entry_safe(pos, n, node, member) 		\
73 	for (pos = llist_entry((node), __typeof(*pos), member); 	\
74 	    pos != NULL &&						\
75 	    (n = llist_entry(pos->member.next, __typeof(*pos), member), pos); \
76 	    pos = n)
77 
78 #endif	/* _LINUX_LLIST_H_ */
79