plist.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include <linux/spinlock.h>
  28. #ifdef CONFIG_DEBUG_PI_LIST
  29. static struct plist_head test_head;
  30. static void plist_check_prev_next(struct list_head *t, struct list_head *p,
  31. struct list_head *n)
  32. {
  33. WARN(n->prev != p || p->next != n,
  34. "top: %p, n: %p, p: %p\n"
  35. "prev: %p, n: %p, p: %p\n"
  36. "next: %p, n: %p, p: %p\n",
  37. t, t->next, t->prev,
  38. p, p->next, p->prev,
  39. n, n->next, n->prev);
  40. }
  41. static void plist_check_list(struct list_head *top)
  42. {
  43. struct list_head *prev = top, *next = top->next;
  44. plist_check_prev_next(top, prev, next);
  45. while (next != top) {
  46. prev = next;
  47. next = prev->next;
  48. plist_check_prev_next(top, prev, next);
  49. }
  50. }
  51. static void plist_check_head(struct plist_head *head)
  52. {
  53. if (!plist_head_empty(head))
  54. plist_check_list(&plist_first(head)->prio_list);
  55. plist_check_list(&head->node_list);
  56. }
  57. #else
  58. # define plist_check_head(h) do { } while (0)
  59. #endif
  60. /**
  61. * plist_add - add @node to @head
  62. *
  63. * @node: &struct plist_node pointer
  64. * @head: &struct plist_head pointer
  65. */
  66. void plist_add(struct plist_node *node, struct plist_head *head)
  67. {
  68. struct plist_node *first, *iter, *prev = NULL;
  69. struct list_head *node_next = &head->node_list;
  70. plist_check_head(head);
  71. WARN_ON(!plist_node_empty(node));
  72. WARN_ON(!list_empty(&node->prio_list));
  73. if (plist_head_empty(head))
  74. goto ins_node;
  75. first = iter = plist_first(head);
  76. do {
  77. if (node->prio < iter->prio) {
  78. node_next = &iter->node_list;
  79. break;
  80. }
  81. prev = iter;
  82. iter = list_entry(iter->prio_list.next,
  83. struct plist_node, prio_list);
  84. } while (iter != first);
  85. if (!prev || prev->prio != node->prio)
  86. list_add_tail(&node->prio_list, &iter->prio_list);
  87. ins_node:
  88. list_add_tail(&node->node_list, node_next);
  89. plist_check_head(head);
  90. }
  91. /**
  92. * plist_del - Remove a @node from plist.
  93. *
  94. * @node: &struct plist_node pointer - entry to be removed
  95. * @head: &struct plist_head pointer - list head
  96. */
  97. void plist_del(struct plist_node *node, struct plist_head *head)
  98. {
  99. plist_check_head(head);
  100. if (!list_empty(&node->prio_list)) {
  101. if (node->node_list.next != &head->node_list) {
  102. struct plist_node *next;
  103. next = list_entry(node->node_list.next,
  104. struct plist_node, node_list);
  105. /* add the next plist_node into prio_list */
  106. if (list_empty(&next->prio_list))
  107. list_add(&next->prio_list, &node->prio_list);
  108. }
  109. list_del_init(&node->prio_list);
  110. }
  111. list_del_init(&node->node_list);
  112. plist_check_head(head);
  113. }
  114. #ifdef CONFIG_DEBUG_PI_LIST
  115. #include <linux/sched.h>
  116. #include <linux/module.h>
  117. #include <linux/init.h>
  118. static struct plist_node __initdata test_node[241];
  119. static void __init plist_test_check(int nr_expect)
  120. {
  121. struct plist_node *first, *prio_pos, *node_pos;
  122. if (plist_head_empty(&test_head)) {
  123. BUG_ON(nr_expect != 0);
  124. return;
  125. }
  126. prio_pos = first = plist_first(&test_head);
  127. plist_for_each(node_pos, &test_head) {
  128. if (nr_expect-- < 0)
  129. break;
  130. if (node_pos == first)
  131. continue;
  132. if (node_pos->prio == prio_pos->prio) {
  133. BUG_ON(!list_empty(&node_pos->prio_list));
  134. continue;
  135. }
  136. BUG_ON(prio_pos->prio > node_pos->prio);
  137. BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
  138. prio_pos = node_pos;
  139. }
  140. BUG_ON(nr_expect != 0);
  141. BUG_ON(prio_pos->prio_list.next != &first->prio_list);
  142. }
  143. static int __init plist_test(void)
  144. {
  145. int nr_expect = 0, i, loop;
  146. unsigned int r = local_clock();
  147. printk(KERN_INFO "start plist test\n");
  148. plist_head_init(&test_head);
  149. for (i = 0; i < ARRAY_SIZE(test_node); i++)
  150. plist_node_init(test_node + i, 0);
  151. for (loop = 0; loop < 1000; loop++) {
  152. r = r * 193939 % 47629;
  153. i = r % ARRAY_SIZE(test_node);
  154. if (plist_node_empty(test_node + i)) {
  155. r = r * 193939 % 47629;
  156. test_node[i].prio = r % 99;
  157. plist_add(test_node + i, &test_head);
  158. nr_expect++;
  159. } else {
  160. plist_del(test_node + i, &test_head);
  161. nr_expect--;
  162. }
  163. plist_test_check(nr_expect);
  164. }
  165. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  166. if (plist_node_empty(test_node + i))
  167. continue;
  168. plist_del(test_node + i, &test_head);
  169. nr_expect--;
  170. plist_test_check(nr_expect);
  171. }
  172. printk(KERN_INFO "end plist test\n");
  173. return 0;
  174. }
  175. module_init(plist_test);
  176. #endif