plist.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <linux/bug.h>
  26. #include <linux/plist.h>
  27. #ifdef CONFIG_DEBUG_PI_LIST
  28. static struct plist_head test_head;
  29. static void plist_check_prev_next(struct list_head *t, struct list_head *p,
  30. struct list_head *n)
  31. {
  32. WARN(n->prev != p || p->next != n,
  33. "top: %p, n: %p, p: %p\n"
  34. "prev: %p, n: %p, p: %p\n"
  35. "next: %p, n: %p, p: %p\n",
  36. t, t->next, t->prev,
  37. p, p->next, p->prev,
  38. n, n->next, n->prev);
  39. }
  40. static void plist_check_list(struct list_head *top)
  41. {
  42. struct list_head *prev = top, *next = top->next;
  43. plist_check_prev_next(top, prev, next);
  44. while (next != top) {
  45. prev = next;
  46. next = prev->next;
  47. plist_check_prev_next(top, prev, next);
  48. }
  49. }
  50. static void plist_check_head(struct plist_head *head)
  51. {
  52. if (!plist_head_empty(head))
  53. plist_check_list(&plist_first(head)->prio_list);
  54. plist_check_list(&head->node_list);
  55. }
  56. #else
  57. # define plist_check_head(h) do { } while (0)
  58. #endif
  59. /**
  60. * plist_add - add @node to @head
  61. *
  62. * @node: &struct plist_node pointer
  63. * @head: &struct plist_head pointer
  64. */
  65. void plist_add(struct plist_node *node, struct plist_head *head)
  66. {
  67. struct plist_node *first, *iter, *prev = NULL;
  68. struct list_head *node_next = &head->node_list;
  69. plist_check_head(head);
  70. WARN_ON(!plist_node_empty(node));
  71. WARN_ON(!list_empty(&node->prio_list));
  72. if (plist_head_empty(head))
  73. goto ins_node;
  74. first = iter = plist_first(head);
  75. do {
  76. if (node->prio < iter->prio) {
  77. node_next = &iter->node_list;
  78. break;
  79. }
  80. prev = iter;
  81. iter = list_entry(iter->prio_list.next,
  82. struct plist_node, prio_list);
  83. } while (iter != first);
  84. if (!prev || prev->prio != node->prio)
  85. list_add_tail(&node->prio_list, &iter->prio_list);
  86. ins_node:
  87. list_add_tail(&node->node_list, node_next);
  88. plist_check_head(head);
  89. }
  90. /**
  91. * plist_del - Remove a @node from plist.
  92. *
  93. * @node: &struct plist_node pointer - entry to be removed
  94. * @head: &struct plist_head pointer - list head
  95. */
  96. void plist_del(struct plist_node *node, struct plist_head *head)
  97. {
  98. plist_check_head(head);
  99. if (!list_empty(&node->prio_list)) {
  100. if (node->node_list.next != &head->node_list) {
  101. struct plist_node *next;
  102. next = list_entry(node->node_list.next,
  103. struct plist_node, node_list);
  104. /* add the next plist_node into prio_list */
  105. if (list_empty(&next->prio_list))
  106. list_add(&next->prio_list, &node->prio_list);
  107. }
  108. list_del_init(&node->prio_list);
  109. }
  110. list_del_init(&node->node_list);
  111. plist_check_head(head);
  112. }
  113. /**
  114. * plist_requeue - Requeue @node at end of same-prio entries.
  115. *
  116. * This is essentially an optimized plist_del() followed by
  117. * plist_add(). It moves an entry already in the plist to
  118. * after any other same-priority entries.
  119. *
  120. * @node: &struct plist_node pointer - entry to be moved
  121. * @head: &struct plist_head pointer - list head
  122. */
  123. void plist_requeue(struct plist_node *node, struct plist_head *head)
  124. {
  125. struct plist_node *iter;
  126. struct list_head *node_next = &head->node_list;
  127. plist_check_head(head);
  128. BUG_ON(plist_head_empty(head));
  129. BUG_ON(plist_node_empty(node));
  130. if (node == plist_last(head))
  131. return;
  132. iter = plist_next(node);
  133. if (node->prio != iter->prio)
  134. return;
  135. plist_del(node, head);
  136. plist_for_each_continue(iter, head) {
  137. if (node->prio != iter->prio) {
  138. node_next = &iter->node_list;
  139. break;
  140. }
  141. }
  142. list_add_tail(&node->node_list, node_next);
  143. plist_check_head(head);
  144. }
  145. #ifdef CONFIG_DEBUG_PI_LIST
  146. #include <linux/sched.h>
  147. #include <linux/module.h>
  148. #include <linux/init.h>
  149. static struct plist_node __initdata test_node[241];
  150. static void __init plist_test_check(int nr_expect)
  151. {
  152. struct plist_node *first, *prio_pos, *node_pos;
  153. if (plist_head_empty(&test_head)) {
  154. BUG_ON(nr_expect != 0);
  155. return;
  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. BUG_ON(prio_pos->prio > node_pos->prio);
  168. BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
  169. prio_pos = node_pos;
  170. }
  171. BUG_ON(nr_expect != 0);
  172. BUG_ON(prio_pos->prio_list.next != &first->prio_list);
  173. }
  174. static void __init plist_test_requeue(struct plist_node *node)
  175. {
  176. plist_requeue(node, &test_head);
  177. if (node != plist_last(&test_head))
  178. BUG_ON(node->prio == plist_next(node)->prio);
  179. }
  180. static int __init plist_test(void)
  181. {
  182. int nr_expect = 0, i, loop;
  183. unsigned int r = local_clock();
  184. printk(KERN_DEBUG "start plist test\n");
  185. plist_head_init(&test_head);
  186. for (i = 0; i < ARRAY_SIZE(test_node); i++)
  187. plist_node_init(test_node + i, 0);
  188. for (loop = 0; loop < 1000; loop++) {
  189. r = r * 193939 % 47629;
  190. i = r % ARRAY_SIZE(test_node);
  191. if (plist_node_empty(test_node + i)) {
  192. r = r * 193939 % 47629;
  193. test_node[i].prio = r % 99;
  194. plist_add(test_node + i, &test_head);
  195. nr_expect++;
  196. } else {
  197. plist_del(test_node + i, &test_head);
  198. nr_expect--;
  199. }
  200. plist_test_check(nr_expect);
  201. if (!plist_node_empty(test_node + i)) {
  202. plist_test_requeue(test_node + i);
  203. plist_test_check(nr_expect);
  204. }
  205. }
  206. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  207. if (plist_node_empty(test_node + i))
  208. continue;
  209. plist_del(test_node + i, &test_head);
  210. nr_expect--;
  211. plist_test_check(nr_expect);
  212. }
  213. printk(KERN_DEBUG "end plist test\n");
  214. return 0;
  215. }
  216. module_init(plist_test);
  217. #endif