plist.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/plist.h>
  26. #include <linux/spinlock.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. #ifdef CONFIG_DEBUG_PI_LIST
  114. #include <linux/sched.h>
  115. #include <linux/module.h>
  116. #include <linux/init.h>
  117. static struct plist_node __initdata test_node[241];
  118. static void __init plist_test_check(int nr_expect)
  119. {
  120. struct plist_node *first, *prio_pos, *node_pos;
  121. if (plist_head_empty(&test_head)) {
  122. BUG_ON(nr_expect != 0);
  123. return;
  124. }
  125. prio_pos = first = plist_first(&test_head);
  126. plist_for_each(node_pos, &test_head) {
  127. if (nr_expect-- < 0)
  128. break;
  129. if (node_pos == first)
  130. continue;
  131. if (node_pos->prio == prio_pos->prio) {
  132. BUG_ON(!list_empty(&node_pos->prio_list));
  133. continue;
  134. }
  135. BUG_ON(prio_pos->prio > node_pos->prio);
  136. BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
  137. prio_pos = node_pos;
  138. }
  139. BUG_ON(nr_expect != 0);
  140. BUG_ON(prio_pos->prio_list.next != &first->prio_list);
  141. }
  142. static int __init plist_test(void)
  143. {
  144. int nr_expect = 0, i, loop;
  145. unsigned int r = local_clock();
  146. printk(KERN_INFO "start plist test\n");
  147. plist_head_init(&test_head);
  148. for (i = 0; i < ARRAY_SIZE(test_node); i++)
  149. plist_node_init(test_node + i, 0);
  150. for (loop = 0; loop < 1000; loop++) {
  151. r = r * 193939 % 47629;
  152. i = r % ARRAY_SIZE(test_node);
  153. if (plist_node_empty(test_node + i)) {
  154. r = r * 193939 % 47629;
  155. test_node[i].prio = r % 99;
  156. plist_add(test_node + i, &test_head);
  157. nr_expect++;
  158. } else {
  159. plist_del(test_node + i, &test_head);
  160. nr_expect--;
  161. }
  162. plist_test_check(nr_expect);
  163. }
  164. for (i = 0; i < ARRAY_SIZE(test_node); i++) {
  165. if (plist_node_empty(test_node + i))
  166. continue;
  167. plist_del(test_node + i, &test_head);
  168. nr_expect--;
  169. plist_test_check(nr_expect);
  170. }
  171. printk(KERN_INFO "end plist test\n");
  172. return 0;
  173. }
  174. module_init(plist_test);
  175. #endif