]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/llist.h
bcache in userspace; userspace fsck
[bcachefs-tools-debian] / include / linux / llist.h
1 #ifndef __TOOLS_LINUX_LLIST_H
2 #define __TOOLS_LINUX_LLIST_H
3
4 /*
5  * Lock-less NULL terminated single linked list
6  *
7  * If there are multiple producers and multiple consumers, llist_add
8  * can be used in producers and llist_del_all can be used in
9  * consumers.  They can work simultaneously without lock.  But
10  * llist_del_first can not be used here.  Because llist_del_first
11  * depends on list->first->next does not changed if list->first is not
12  * changed during its operation, but llist_del_first, llist_add,
13  * llist_add (or llist_del_all, llist_add, llist_add) sequence in
14  * another consumer may violate that.
15  *
16  * If there are multiple producers and one consumer, llist_add can be
17  * used in producers and llist_del_all or llist_del_first can be used
18  * in the consumer.
19  *
20  * This can be summarized as follow:
21  *
22  *           |   add    | del_first |  del_all
23  * add       |    -     |     -     |     -
24  * del_first |          |     L     |     L
25  * del_all   |          |           |     -
26  *
27  * Where "-" stands for no lock is needed, while "L" stands for lock
28  * is needed.
29  *
30  * The list entries deleted via llist_del_all can be traversed with
31  * traversing function such as llist_for_each etc.  But the list
32  * entries can not be traversed safely before deleted from the list.
33  * The order of deleted entries is from the newest to the oldest added
34  * one.  If you want to traverse from the oldest to the newest, you
35  * must reverse the order by yourself before traversing.
36  *
37  * The basic atomic operation of this list is cmpxchg on long.  On
38  * architectures that don't have NMI-safe cmpxchg implementation, the
39  * list can NOT be used in NMI handlers.  So code that uses the list in
40  * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
41  *
42  * Copyright 2010,2011 Intel Corp.
43  *   Author: Huang Ying <ying.huang@intel.com>
44  *
45  * This program is free software; you can redistribute it and/or
46  * modify it under the terms of the GNU General Public License version
47  * 2 as published by the Free Software Foundation;
48  *
49  * This program is distributed in the hope that it will be useful,
50  * but WITHOUT ANY WARRANTY; without even the implied warranty of
51  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52  * GNU General Public License for more details.
53  *
54  * You should have received a copy of the GNU General Public License
55  * along with this program; if not, write to the Free Software
56  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
57  */
58
59 #include <linux/atomic.h>
60 #include <linux/kernel.h>
61
62 struct llist_head {
63         struct llist_node *first;
64 };
65
66 struct llist_node {
67         struct llist_node *next;
68 };
69
70 #define LLIST_HEAD_INIT(name)   { NULL }
71 #define LLIST_HEAD(name)        struct llist_head name = LLIST_HEAD_INIT(name)
72
73 /**
74  * init_llist_head - initialize lock-less list head
75  * @head:       the head for your lock-less list
76  */
77 static inline void init_llist_head(struct llist_head *list)
78 {
79         list->first = NULL;
80 }
81
82 /**
83  * llist_entry - get the struct of this entry
84  * @ptr:        the &struct llist_node pointer.
85  * @type:       the type of the struct this is embedded in.
86  * @member:     the name of the llist_node within the struct.
87  */
88 #define llist_entry(ptr, type, member)          \
89         container_of(ptr, type, member)
90
91 /**
92  * llist_for_each - iterate over some deleted entries of a lock-less list
93  * @pos:        the &struct llist_node to use as a loop cursor
94  * @node:       the first entry of deleted list entries
95  *
96  * In general, some entries of the lock-less list can be traversed
97  * safely only after being deleted from list, so start with an entry
98  * instead of list head.
99  *
100  * If being used on entries deleted from lock-less list directly, the
101  * traverse order is from the newest to the oldest added entry.  If
102  * you want to traverse from the oldest to the newest, you must
103  * reverse the order by yourself before traversing.
104  */
105 #define llist_for_each(pos, node)                       \
106         for ((pos) = (node); pos; (pos) = (pos)->next)
107
108 /**
109  * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
110  * @pos:        the type * to use as a loop cursor.
111  * @node:       the fist entry of deleted list entries.
112  * @member:     the name of the llist_node with the struct.
113  *
114  * In general, some entries of the lock-less list can be traversed
115  * safely only after being removed from list, so start with an entry
116  * instead of list head.
117  *
118  * If being used on entries deleted from lock-less list directly, the
119  * traverse order is from the newest to the oldest added entry.  If
120  * you want to traverse from the oldest to the newest, you must
121  * reverse the order by yourself before traversing.
122  */
123 #define llist_for_each_entry(pos, node, member)                         \
124         for ((pos) = llist_entry((node), typeof(*(pos)), member);       \
125              &(pos)->member != NULL;                                    \
126              (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
127
128 /**
129  * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type
130  *                             safe against removal of list entry
131  * @pos:        the type * to use as a loop cursor.
132  * @n:          another type * to use as temporary storage
133  * @node:       the first entry of deleted list entries.
134  * @member:     the name of the llist_node with the struct.
135  *
136  * In general, some entries of the lock-less list can be traversed
137  * safely only after being removed from list, so start with an entry
138  * instead of list head.
139  *
140  * If being used on entries deleted from lock-less list directly, the
141  * traverse order is from the newest to the oldest added entry.  If
142  * you want to traverse from the oldest to the newest, you must
143  * reverse the order by yourself before traversing.
144  */
145 #define llist_for_each_entry_safe(pos, n, node, member)                        \
146         for (pos = llist_entry((node), typeof(*pos), member);                  \
147              &pos->member != NULL &&                                           \
148                 (n = llist_entry(pos->member.next, typeof(*n), member), true); \
149              pos = n)
150
151 /**
152  * llist_empty - tests whether a lock-less list is empty
153  * @head:       the list to test
154  *
155  * Not guaranteed to be accurate or up to date.  Just a quick way to
156  * test whether the list is empty without deleting something from the
157  * list.
158  */
159 static inline bool llist_empty(const struct llist_head *head)
160 {
161         return ACCESS_ONCE(head->first) == NULL;
162 }
163
164 static inline struct llist_node *llist_next(struct llist_node *node)
165 {
166         return node->next;
167 }
168
169 extern bool llist_add_batch(struct llist_node *new_first,
170                             struct llist_node *new_last,
171                             struct llist_head *head);
172 /**
173  * llist_add - add a new entry
174  * @new:        new entry to be added
175  * @head:       the head for your lock-less list
176  *
177  * Returns true if the list was empty prior to adding this entry.
178  */
179 static inline bool llist_add(struct llist_node *new, struct llist_head *head)
180 {
181         return llist_add_batch(new, new, head);
182 }
183
184 /**
185  * llist_del_all - delete all entries from lock-less list
186  * @head:       the head of lock-less list to delete all entries
187  *
188  * If list is empty, return NULL, otherwise, delete all entries and
189  * return the pointer to the first entry.  The order of entries
190  * deleted is from the newest to the oldest added one.
191  */
192 static inline struct llist_node *llist_del_all(struct llist_head *head)
193 {
194         return xchg(&head->first, NULL);
195 }
196
197 extern struct llist_node *llist_del_first(struct llist_head *head);
198
199 struct llist_node *llist_reverse_order(struct llist_node *head);
200
201 #endif /* __TOOLS_LINUX_LLIST_H */