llist.h revision 1.2 1 /* $NetBSD: llist.h,v 1.2 2021/12/19 00:54:54 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 *llh_first;
40 };
41
42 struct llist_node {
43 struct llist_node *llh_next;
44 };
45
46 static inline void
47 init_llist_head(struct llist_head *head)
48 {
49
50 head->llh_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 = (head->llh_first == NULL);
61 membar_enter();
62
63 return empty;
64 }
65
66 static inline bool
67 llist_add(struct llist_node *node, struct llist_head *head)
68 {
69 struct llist_node *first;
70
71 do {
72 first = head->llh_first;
73 node->llh_next = first;
74 membar_exit();
75 } while (atomic_cas_ptr(&head->llh_first, first, node) != first);
76
77 return first == NULL;
78 }
79
80 static inline struct llist_node *
81 llist_del_all(struct llist_head *head)
82 {
83 struct llist_node *first;
84
85 first = atomic_swap_ptr(&head->llh_first, NULL);
86 membar_enter();
87
88 return first;
89 }
90
91 static inline struct llist_node *
92 llist_del_first(struct llist_head *head)
93 {
94 struct llist_node *first;
95
96 do {
97 first = head->llh_first;
98 membar_datadep_consumer();
99 } while (atomic_cas_ptr(&head->llh_first, first, first->llh_next)
100 != first);
101 membar_enter();
102
103 return first;
104 }
105
106 #define llist_for_each_entry_safe(ENTRY, TMP, NODE, FIELD) \
107 for ((ENTRY) = ((NODE) == NULL ? NULL : \
108 llist_entry(NODE, typeof(*(ENTRY)), FIELD)); \
109 (ENTRY) == NULL ? 0 : \
110 (membar_datadep_consumer(), \
111 (TMP) = list_entry((ENTRY)->FIELD.llh_next, \
112 typeof(*(ENTRY)), FIELD), \
113 1); \
114 (ENTRY) = (TMP))
115
116 #endif /* _LINUX_LLIST_H_ */
117