Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lib/plist.c |
| 3 | * |
| 4 | * Descending-priority-sorted double-linked list |
| 5 | * |
| 6 | * (C) 2002-2003 Intel Corp |
| 7 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>. |
| 8 | * |
| 9 | * 2001-2005 (c) MontaVista Software, Inc. |
| 10 | * Daniel Walker <dwalker@mvista.com> |
| 11 | * |
| 12 | * (C) 2005 Thomas Gleixner <tglx@linutronix.de> |
| 13 | * |
| 14 | * Simplifications of the original code by |
| 15 | * Oleg Nesterov <oleg@tv-sign.ru> |
| 16 | * |
| 17 | * Licensed under the FSF's GNU Public License v2 or later. |
| 18 | * |
| 19 | * Based on simple lists (include/linux/list.h). |
| 20 | * |
| 21 | * This file contains the add / del functions which are considered to |
| 22 | * be too large to inline. See include/linux/plist.h for further |
| 23 | * information. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/plist.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | |
| 29 | #ifdef CONFIG_DEBUG_PI_LIST |
| 30 | |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 31 | static struct plist_head test_head; |
| 32 | |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 33 | static void plist_check_prev_next(struct list_head *t, struct list_head *p, |
| 34 | struct list_head *n) |
| 35 | { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 36 | WARN(n->prev != p || p->next != n, |
| 37 | "top: %p, n: %p, p: %p\n" |
| 38 | "prev: %p, n: %p, p: %p\n" |
| 39 | "next: %p, n: %p, p: %p\n", |
| 40 | t, t->next, t->prev, |
| 41 | p, p->next, p->prev, |
| 42 | n, n->next, n->prev); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void plist_check_list(struct list_head *top) |
| 46 | { |
| 47 | struct list_head *prev = top, *next = top->next; |
| 48 | |
| 49 | plist_check_prev_next(top, prev, next); |
| 50 | while (next != top) { |
| 51 | prev = next; |
| 52 | next = prev->next; |
| 53 | plist_check_prev_next(top, prev, next); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static void plist_check_head(struct plist_head *head) |
| 58 | { |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 59 | WARN_ON(head != &test_head && !head->rawlock && !head->spinlock); |
Thomas Gleixner | a267245 | 2009-11-17 14:46:14 +0100 | [diff] [blame] | 60 | if (head->rawlock) |
| 61 | WARN_ON_SMP(!raw_spin_is_locked(head->rawlock)); |
| 62 | if (head->spinlock) |
| 63 | WARN_ON_SMP(!spin_is_locked(head->spinlock)); |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 64 | if (!plist_head_empty(head)) |
| 65 | plist_check_list(&plist_first(head)->prio_list); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 66 | plist_check_list(&head->node_list); |
| 67 | } |
| 68 | |
| 69 | #else |
| 70 | # define plist_check_head(h) do { } while (0) |
| 71 | #endif |
| 72 | |
| 73 | /** |
| 74 | * plist_add - add @node to @head |
| 75 | * |
| 76 | * @node: &struct plist_node pointer |
| 77 | * @head: &struct plist_head pointer |
| 78 | */ |
| 79 | void plist_add(struct plist_node *node, struct plist_head *head) |
| 80 | { |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 81 | struct plist_node *first, *iter, *prev = NULL; |
| 82 | struct list_head *node_next = &head->node_list; |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 83 | |
| 84 | plist_check_head(head); |
| 85 | WARN_ON(!plist_node_empty(node)); |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 86 | WARN_ON(!list_empty(&node->prio_list)); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 87 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 88 | if (plist_head_empty(head)) |
| 89 | goto ins_node; |
| 90 | |
| 91 | first = iter = plist_first(head); |
| 92 | |
| 93 | do { |
| 94 | if (node->prio < iter->prio) { |
| 95 | node_next = &iter->node_list; |
| 96 | break; |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 97 | } |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 98 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 99 | prev = iter; |
| 100 | iter = list_entry(iter->prio_list.next, |
| 101 | struct plist_node, prio_list); |
| 102 | } while (iter != first); |
| 103 | |
| 104 | if (!prev || prev->prio != node->prio) |
| 105 | list_add_tail(&node->prio_list, &iter->prio_list); |
| 106 | ins_node: |
| 107 | list_add_tail(&node->node_list, node_next); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 108 | |
| 109 | plist_check_head(head); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * plist_del - Remove a @node from plist. |
| 114 | * |
| 115 | * @node: &struct plist_node pointer - entry to be removed |
| 116 | * @head: &struct plist_head pointer - list head |
| 117 | */ |
| 118 | void plist_del(struct plist_node *node, struct plist_head *head) |
| 119 | { |
| 120 | plist_check_head(head); |
| 121 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 122 | if (!list_empty(&node->prio_list)) { |
| 123 | if (node->node_list.next != &head->node_list) { |
| 124 | struct plist_node *next; |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 125 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 126 | next = list_entry(node->node_list.next, |
| 127 | struct plist_node, node_list); |
| 128 | |
| 129 | /* add the next plist_node into prio_list */ |
| 130 | if (list_empty(&next->prio_list)) |
| 131 | list_add(&next->prio_list, &node->prio_list); |
| 132 | } |
| 133 | list_del_init(&node->prio_list); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Lai Jiangshan | bf6a9b8 | 2010-12-21 17:55:14 +0800 | [diff] [blame] | 136 | list_del_init(&node->node_list); |
Ingo Molnar | 77ba89c | 2006-06-27 02:54:51 -0700 | [diff] [blame] | 137 | |
| 138 | plist_check_head(head); |
| 139 | } |
Lai Jiangshan | 6d55da5 | 2010-12-21 17:55:18 +0800 | [diff] [blame] | 140 | |
| 141 | #ifdef CONFIG_DEBUG_PI_LIST |
| 142 | #include <linux/sched.h> |
| 143 | #include <linux/module.h> |
| 144 | #include <linux/init.h> |
| 145 | |
| 146 | static struct plist_node __initdata test_node[241]; |
| 147 | |
| 148 | static void __init plist_test_check(int nr_expect) |
| 149 | { |
| 150 | struct plist_node *first, *prio_pos, *node_pos; |
| 151 | |
| 152 | if (plist_head_empty(&test_head)) { |
| 153 | BUG_ON(nr_expect != 0); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | prio_pos = first = plist_first(&test_head); |
| 158 | plist_for_each(node_pos, &test_head) { |
| 159 | if (nr_expect-- < 0) |
| 160 | break; |
| 161 | if (node_pos == first) |
| 162 | continue; |
| 163 | if (node_pos->prio == prio_pos->prio) { |
| 164 | BUG_ON(!list_empty(&node_pos->prio_list)); |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | BUG_ON(prio_pos->prio > node_pos->prio); |
| 169 | BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list); |
| 170 | prio_pos = node_pos; |
| 171 | } |
| 172 | |
| 173 | BUG_ON(nr_expect != 0); |
| 174 | BUG_ON(prio_pos->prio_list.next != &first->prio_list); |
| 175 | } |
| 176 | |
| 177 | static int __init plist_test(void) |
| 178 | { |
| 179 | int nr_expect = 0, i, loop; |
| 180 | unsigned int r = local_clock(); |
| 181 | |
| 182 | printk(KERN_INFO "start plist test\n"); |
| 183 | plist_head_init(&test_head, NULL); |
| 184 | for (i = 0; i < ARRAY_SIZE(test_node); i++) |
| 185 | plist_node_init(test_node + i, 0); |
| 186 | |
| 187 | for (loop = 0; loop < 1000; loop++) { |
| 188 | r = r * 193939 % 47629; |
| 189 | i = r % ARRAY_SIZE(test_node); |
| 190 | if (plist_node_empty(test_node + i)) { |
| 191 | r = r * 193939 % 47629; |
| 192 | test_node[i].prio = r % 99; |
| 193 | plist_add(test_node + i, &test_head); |
| 194 | nr_expect++; |
| 195 | } else { |
| 196 | plist_del(test_node + i, &test_head); |
| 197 | nr_expect--; |
| 198 | } |
| 199 | plist_test_check(nr_expect); |
| 200 | } |
| 201 | |
| 202 | for (i = 0; i < ARRAY_SIZE(test_node); i++) { |
| 203 | if (plist_node_empty(test_node + i)) |
| 204 | continue; |
| 205 | plist_del(test_node + i, &test_head); |
| 206 | nr_expect--; |
| 207 | plist_test_check(nr_expect); |
| 208 | } |
| 209 | |
| 210 | printk(KERN_INFO "end plist test\n"); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | module_init(plist_test); |
| 215 | |
| 216 | #endif |