Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
[linux-2.6-block.git] / include / linux / plist.h
CommitLineData
77ba89c5
IM
1/*
2 * Descending-priority-sorted double-linked list
3 *
4 * (C) 2002-2003 Intel Corp
5 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
6 *
7 * 2001-2005 (c) MontaVista Software, Inc.
8 * Daniel Walker <dwalker@mvista.com>
9 *
10 * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
11 *
12 * Simplifications of the original code by
13 * Oleg Nesterov <oleg@tv-sign.ru>
14 *
15 * Licensed under the FSF's GNU Public License v2 or later.
16 *
17 * Based on simple lists (include/linux/list.h).
18 *
19 * This is a priority-sorted list of nodes; each node has a
20 * priority from INT_MIN (highest) to INT_MAX (lowest).
21 *
22 * Addition is O(K), removal is O(1), change of priority of a node is
23 * O(K) and K is the number of RT priority levels used in the system.
24 * (1 <= K <= 99)
25 *
26 * This list is really a list of lists:
27 *
28 * - The tier 1 list is the prio_list, different priority nodes.
29 *
30 * - The tier 2 list is the node_list, serialized nodes.
31 *
32 * Simple ASCII art explanation:
33 *
34 * |HEAD |
35 * | |
36 * |prio_list.prev|<------------------------------------|
37 * |prio_list.next|<->|pl|<->|pl|<--------------->|pl|<-|
38 * |10 | |10| |21| |21| |21| |40| (prio)
39 * | | | | | | | | | | | |
40 * | | | | | | | | | | | |
41 * |node_list.next|<->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<-|
42 * |node_list.prev|<------------------------------------|
43 *
44 * The nodes on the prio_list list are sorted by priority to simplify
45 * the insertion of new nodes. There are no nodes with duplicate
46 * priorites on the list.
47 *
48 * The nodes on the node_list is ordered by priority and can contain
49 * entries which have the same priority. Those entries are ordered
50 * FIFO
51 *
52 * Addition means: look for the prio_list node in the prio_list
53 * for the priority of the node and insert it before the node_list
54 * entry of the next prio_list node. If it is the first node of
55 * that priority, add it to the prio_list in the right position and
56 * insert it into the serialized node_list list
57 *
58 * Removal means remove it from the node_list and remove it from
59 * the prio_list if the node_list list_head is non empty. In case
60 * of removal from the prio_list it must be checked whether other
61 * entries of the same priority are on the list or not. If there
62 * is another entry of the same priority then this entry has to
63 * replace the removed entry on the prio_list. If the entry which
64 * is removed is the only entry of this priority then a simple
65 * remove from both list is sufficient.
66 *
67 * INT_MIN is the highest priority, 0 is the medium highest, INT_MAX
68 * is lowest priority.
69 *
70 * No locking is done, up to the caller.
71 *
72 */
73#ifndef _LINUX_PLIST_H_
74#define _LINUX_PLIST_H_
75
b4459790 76#include <linux/kernel.h>
77ba89c5
IM
77#include <linux/list.h>
78#include <linux/spinlock_types.h>
79
80struct plist_head {
81 struct list_head prio_list;
82 struct list_head node_list;
83#ifdef CONFIG_DEBUG_PI_LIST
84 spinlock_t *lock;
85#endif
86};
87
88struct plist_node {
89 int prio;
90 struct plist_head plist;
91};
92
93#ifdef CONFIG_DEBUG_PI_LIST
94# define PLIST_HEAD_LOCK_INIT(_lock) .lock = _lock
95#else
96# define PLIST_HEAD_LOCK_INIT(_lock)
97#endif
98
99/**
11265420 100 * PLIST_HEAD_INIT - static struct plist_head initializer
77ba89c5 101 * @head: struct plist_head variable name
11265420 102 * @_lock: lock to initialize for this list
77ba89c5
IM
103 */
104#define PLIST_HEAD_INIT(head, _lock) \
105{ \
106 .prio_list = LIST_HEAD_INIT((head).prio_list), \
107 .node_list = LIST_HEAD_INIT((head).node_list), \
108 PLIST_HEAD_LOCK_INIT(&(_lock)) \
109}
110
111/**
11265420 112 * PLIST_NODE_INIT - static struct plist_node initializer
77ba89c5
IM
113 * @node: struct plist_node variable name
114 * @__prio: initial node priority
115 */
116#define PLIST_NODE_INIT(node, __prio) \
117{ \
118 .prio = (__prio), \
119 .plist = PLIST_HEAD_INIT((node).plist, NULL), \
120}
121
122/**
123 * plist_head_init - dynamic struct plist_head initializer
77ba89c5 124 * @head: &struct plist_head pointer
11265420 125 * @lock: list spinlock, remembered for debugging
77ba89c5
IM
126 */
127static inline void
128plist_head_init(struct plist_head *head, spinlock_t *lock)
129{
130 INIT_LIST_HEAD(&head->prio_list);
131 INIT_LIST_HEAD(&head->node_list);
132#ifdef CONFIG_DEBUG_PI_LIST
133 head->lock = lock;
134#endif
135}
136
137/**
138 * plist_node_init - Dynamic struct plist_node initializer
77ba89c5
IM
139 * @node: &struct plist_node pointer
140 * @prio: initial node priority
141 */
142static inline void plist_node_init(struct plist_node *node, int prio)
143{
144 node->prio = prio;
145 plist_head_init(&node->plist, NULL);
146}
147
148extern void plist_add(struct plist_node *node, struct plist_head *head);
149extern void plist_del(struct plist_node *node, struct plist_head *head);
150
151/**
152 * plist_for_each - iterate over the plist
11265420
RD
153 * @pos: the type * to use as a loop counter
154 * @head: the head for your list
77ba89c5
IM
155 */
156#define plist_for_each(pos, head) \
157 list_for_each_entry(pos, &(head)->node_list, plist.node_list)
158
159/**
11265420
RD
160 * plist_for_each_safe - iterate safely over a plist of given type
161 * @pos: the type * to use as a loop counter
162 * @n: another type * to use as temporary storage
163 * @head: the head for your list
77ba89c5 164 *
11265420 165 * Iterate over a plist of given type, safe against removal of list entry.
77ba89c5
IM
166 */
167#define plist_for_each_safe(pos, n, head) \
168 list_for_each_entry_safe(pos, n, &(head)->node_list, plist.node_list)
169
170/**
171 * plist_for_each_entry - iterate over list of given type
11265420
RD
172 * @pos: the type * to use as a loop counter
173 * @head: the head for your list
174 * @mem: the name of the list_struct within the struct
77ba89c5
IM
175 */
176#define plist_for_each_entry(pos, head, mem) \
177 list_for_each_entry(pos, &(head)->node_list, mem.plist.node_list)
178
179/**
11265420
RD
180 * plist_for_each_entry_safe - iterate safely over list of given type
181 * @pos: the type * to use as a loop counter
77ba89c5 182 * @n: another type * to use as temporary storage
11265420
RD
183 * @head: the head for your list
184 * @m: the name of the list_struct within the struct
185 *
186 * Iterate over list of given type, safe against removal of list entry.
77ba89c5
IM
187 */
188#define plist_for_each_entry_safe(pos, n, head, m) \
189 list_for_each_entry_safe(pos, n, &(head)->node_list, m.plist.node_list)
190
191/**
192 * plist_head_empty - return !0 if a plist_head is empty
77ba89c5
IM
193 * @head: &struct plist_head pointer
194 */
195static inline int plist_head_empty(const struct plist_head *head)
196{
197 return list_empty(&head->node_list);
198}
199
200/**
201 * plist_node_empty - return !0 if plist_node is not on a list
77ba89c5
IM
202 * @node: &struct plist_node pointer
203 */
204static inline int plist_node_empty(const struct plist_node *node)
205{
206 return plist_head_empty(&node->plist);
207}
208
209/* All functions below assume the plist_head is not empty. */
210
211/**
212 * plist_first_entry - get the struct for the first entry
11265420
RD
213 * @head: the &struct plist_head pointer
214 * @type: the type of the struct this is embedded in
215 * @member: the name of the list_struct within the struct
77ba89c5
IM
216 */
217#ifdef CONFIG_DEBUG_PI_LIST
218# define plist_first_entry(head, type, member) \
219({ \
220 WARN_ON(plist_head_empty(head)); \
221 container_of(plist_first(head), type, member); \
222})
223#else
224# define plist_first_entry(head, type, member) \
225 container_of(plist_first(head), type, member)
226#endif
227
228/**
229 * plist_first - return the first node (and thus, highest priority)
77ba89c5
IM
230 * @head: the &struct plist_head pointer
231 *
232 * Assumes the plist is _not_ empty.
233 */
234static inline struct plist_node* plist_first(const struct plist_head *head)
235{
236 return list_entry(head->node_list.next,
237 struct plist_node, plist.node_list);
238}
239
240#endif