1 #include <linux/version.h>
2 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0))
3 #include_next <linux/llist.h>
5 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0))
6 #include_next <linux/llist.h>
7 extern bool llist_add_batch(struct llist_node *new_first,
8 struct llist_node *new_last,
9 struct llist_head *head);
10 extern struct llist_node *llist_del_first(struct llist_head *head);
13 #if (defined(CONFIG_COMPAT_SLES_11_2) || defined(CONFIG_COMPAT_SLES_11_3))
14 #include_next <linux/llist.h>
20 * Lock-less NULL terminated single linked list
22 * If there are multiple producers and multiple consumers, llist_add
23 * can be used in producers and llist_del_all can be used in
24 * consumers. They can work simultaneously without lock. But
25 * llist_del_first can not be used here. Because llist_del_first
26 * depends on list->first->next does not changed if list->first is not
27 * changed during its operation, but llist_del_first, llist_add,
28 * llist_add (or llist_del_all, llist_add, llist_add) sequence in
29 * another consumer may violate that.
31 * If there are multiple producers and one consumer, llist_add can be
32 * used in producers and llist_del_all or llist_del_first can be used
35 * This can be summarized as follow:
37 * | add | del_first | del_all
42 * Where "-" stands for no lock is needed, while "L" stands for lock
45 * The list entries deleted via llist_del_all can be traversed with
46 * traversing function such as llist_for_each etc. But the list
47 * entries can not be traversed safely before deleted from the list.
48 * The order of deleted entries is from the newest to the oldest added
49 * one. If you want to traverse from the oldest to the newest, you
50 * must reverse the order by yourself before traversing.
52 * The basic atomic operation of this list is cmpxchg on long. On
53 * architectures that don't have NMI-safe cmpxchg implementation, the
54 * list can NOT be used in NMI handlers. So code that uses the list in
55 * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
57 * Copyright 2010,2011 Intel Corp.
58 * Author: Huang Ying <ying.huang@intel.com>
60 * This program is free software; you can redistribute it and/or
61 * modify it under the terms of the GNU General Public License version
62 * 2 as published by the Free Software Foundation;
64 * This program is distributed in the hope that it will be useful,
65 * but WITHOUT ANY WARRANTY; without even the implied warranty of
66 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67 * GNU General Public License for more details.
69 * You should have received a copy of the GNU General Public License
70 * along with this program; if not, write to the Free Software
71 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
74 #include <linux/kernel.h>
75 #include <asm/system.h>
76 #include <asm/processor.h>
79 struct llist_node *first;
83 struct llist_node *next;
86 #define LLIST_HEAD_INIT(name) { NULL }
87 #define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name)
90 * init_llist_head - initialize lock-less list head
91 * @head: the head for your lock-less list
93 static inline void init_llist_head(struct llist_head *list)
99 * llist_entry - get the struct of this entry
100 * @ptr: the &struct llist_node pointer.
101 * @type: the type of the struct this is embedded in.
102 * @member: the name of the llist_node within the struct.
104 #define llist_entry(ptr, type, member) \
105 container_of(ptr, type, member)
108 * llist_for_each - iterate over some deleted entries of a lock-less list
109 * @pos: the &struct llist_node to use as a loop cursor
110 * @node: the first entry of deleted list entries
112 * In general, some entries of the lock-less list can be traversed
113 * safely only after being deleted from list, so start with an entry
114 * instead of list head.
116 * If being used on entries deleted from lock-less list directly, the
117 * traverse order is from the newest to the oldest added entry. If
118 * you want to traverse from the oldest to the newest, you must
119 * reverse the order by yourself before traversing.
121 #define llist_for_each(pos, node) \
122 for ((pos) = (node); pos; (pos) = (pos)->next)
125 * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
126 * @pos: the type * to use as a loop cursor.
127 * @node: the fist entry of deleted list entries.
128 * @member: the name of the llist_node with the struct.
130 * In general, some entries of the lock-less list can be traversed
131 * safely only after being removed from list, so start with an entry
132 * instead of list head.
134 * If being used on entries deleted from lock-less list directly, the
135 * traverse order is from the newest to the oldest added entry. If
136 * you want to traverse from the oldest to the newest, you must
137 * reverse the order by yourself before traversing.
139 #define llist_for_each_entry(pos, node, member) \
140 for ((pos) = llist_entry((node), typeof(*(pos)), member); \
141 &(pos)->member != NULL; \
142 (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
145 * llist_empty - tests whether a lock-less list is empty
146 * @head: the list to test
148 * Not guaranteed to be accurate or up to date. Just a quick way to
149 * test whether the list is empty without deleting something from the
152 static inline bool llist_empty(const struct llist_head *head)
154 return ACCESS_ONCE(head->first) == NULL;
157 static inline struct llist_node *llist_next(struct llist_node *node)
163 * llist_add - add a new entry
164 * @new: new entry to be added
165 * @head: the head for your lock-less list
167 * Returns true if the list was empty prior to adding this entry.
169 static inline bool llist_add(struct llist_node *new, struct llist_head *head)
171 struct llist_node *entry, *old_entry;
177 entry = cmpxchg(&head->first, old_entry, new);
178 if (entry == old_entry)
182 return old_entry == NULL;
186 * llist_del_all - delete all entries from lock-less list
187 * @head: the head of lock-less list to delete all entries
189 * If list is empty, return NULL, otherwise, delete all entries and
190 * return the pointer to the first entry. The order of entries
191 * deleted is from the newest to the oldest added one.
193 static inline struct llist_node *llist_del_all(struct llist_head *head)
195 return xchg(&head->first, NULL);
198 extern bool llist_add_batch(struct llist_node *new_first,
199 struct llist_node *new_last,
200 struct llist_head *head);
202 extern struct llist_node *llist_del_first(struct llist_head *head);
204 #endif /* (defined(CONFIG_COMPAT_SLES_11_2) || defined(CONFIG_COMPAT_SLES_11_3)) */
206 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0) */