plist.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include <linux/list.h>
  76. #include <linux/spinlock_types.h>
  77. struct plist_head {
  78. struct list_head prio_list;
  79. struct list_head node_list;
  80. #ifdef CONFIG_DEBUG_PI_LIST
  81. spinlock_t *lock;
  82. #endif
  83. };
  84. struct plist_node {
  85. int prio;
  86. struct plist_head plist;
  87. };
  88. #ifdef CONFIG_DEBUG_PI_LIST
  89. # define PLIST_HEAD_LOCK_INIT(_lock) .lock = _lock
  90. #else
  91. # define PLIST_HEAD_LOCK_INIT(_lock)
  92. #endif
  93. /**
  94. * #PLIST_HEAD_INIT - static struct plist_head initializer
  95. *
  96. * @head: struct plist_head variable name
  97. */
  98. #define PLIST_HEAD_INIT(head, _lock) \
  99. { \
  100. .prio_list = LIST_HEAD_INIT((head).prio_list), \
  101. .node_list = LIST_HEAD_INIT((head).node_list), \
  102. PLIST_HEAD_LOCK_INIT(&(_lock)) \
  103. }
  104. /**
  105. * #PLIST_NODE_INIT - static struct plist_node initializer
  106. *
  107. * @node: struct plist_node variable name
  108. * @__prio: initial node priority
  109. */
  110. #define PLIST_NODE_INIT(node, __prio) \
  111. { \
  112. .prio = (__prio), \
  113. .plist = PLIST_HEAD_INIT((node).plist, NULL), \
  114. }
  115. /**
  116. * plist_head_init - dynamic struct plist_head initializer
  117. *
  118. * @head: &struct plist_head pointer
  119. */
  120. static inline void
  121. plist_head_init(struct plist_head *head, spinlock_t *lock)
  122. {
  123. INIT_LIST_HEAD(&head->prio_list);
  124. INIT_LIST_HEAD(&head->node_list);
  125. #ifdef CONFIG_DEBUG_PI_LIST
  126. head->lock = lock;
  127. #endif
  128. }
  129. /**
  130. * plist_node_init - Dynamic struct plist_node initializer
  131. *
  132. * @node: &struct plist_node pointer
  133. * @prio: initial node priority
  134. */
  135. static inline void plist_node_init(struct plist_node *node, int prio)
  136. {
  137. node->prio = prio;
  138. plist_head_init(&node->plist, NULL);
  139. }
  140. extern void plist_add(struct plist_node *node, struct plist_head *head);
  141. extern void plist_del(struct plist_node *node, struct plist_head *head);
  142. /**
  143. * plist_for_each - iterate over the plist
  144. *
  145. * @pos1: the type * to use as a loop counter.
  146. * @head: the head for your list.
  147. */
  148. #define plist_for_each(pos, head) \
  149. list_for_each_entry(pos, &(head)->node_list, plist.node_list)
  150. /**
  151. * plist_for_each_entry_safe - iterate over a plist of given type safe
  152. * against removal of list entry
  153. *
  154. * @pos1: the type * to use as a loop counter.
  155. * @n1: another type * to use as temporary storage
  156. * @head: the head for your list.
  157. */
  158. #define plist_for_each_safe(pos, n, head) \
  159. list_for_each_entry_safe(pos, n, &(head)->node_list, plist.node_list)
  160. /**
  161. * plist_for_each_entry - iterate over list of given type
  162. *
  163. * @pos: the type * to use as a loop counter.
  164. * @head: the head for your list.
  165. * @member: the name of the list_struct within the struct.
  166. */
  167. #define plist_for_each_entry(pos, head, mem) \
  168. list_for_each_entry(pos, &(head)->node_list, mem.plist.node_list)
  169. /**
  170. * plist_for_each_entry_safe - iterate over list of given type safe against
  171. * removal of list entry
  172. *
  173. * @pos: the type * to use as a loop counter.
  174. * @n: another type * to use as temporary storage
  175. * @head: the head for your list.
  176. * @m: the name of the list_struct within the struct.
  177. */
  178. #define plist_for_each_entry_safe(pos, n, head, m) \
  179. list_for_each_entry_safe(pos, n, &(head)->node_list, m.plist.node_list)
  180. /**
  181. * plist_head_empty - return !0 if a plist_head is empty
  182. *
  183. * @head: &struct plist_head pointer
  184. */
  185. static inline int plist_head_empty(const struct plist_head *head)
  186. {
  187. return list_empty(&head->node_list);
  188. }
  189. /**
  190. * plist_node_empty - return !0 if plist_node is not on a list
  191. *
  192. * @node: &struct plist_node pointer
  193. */
  194. static inline int plist_node_empty(const struct plist_node *node)
  195. {
  196. return plist_head_empty(&node->plist);
  197. }
  198. /* All functions below assume the plist_head is not empty. */
  199. /**
  200. * plist_first_entry - get the struct for the first entry
  201. *
  202. * @ptr: the &struct plist_head pointer.
  203. * @type: the type of the struct this is embedded in.
  204. * @member: the name of the list_struct within the struct.
  205. */
  206. #ifdef CONFIG_DEBUG_PI_LIST
  207. # define plist_first_entry(head, type, member) \
  208. ({ \
  209. WARN_ON(plist_head_empty(head)); \
  210. container_of(plist_first(head), type, member); \
  211. })
  212. #else
  213. # define plist_first_entry(head, type, member) \
  214. container_of(plist_first(head), type, member)
  215. #endif
  216. /**
  217. * plist_first - return the first node (and thus, highest priority)
  218. *
  219. * @head: the &struct plist_head pointer
  220. *
  221. * Assumes the plist is _not_ empty.
  222. */
  223. static inline struct plist_node* plist_first(const struct plist_head *head)
  224. {
  225. return list_entry(head->node_list.next,
  226. struct plist_node, plist.node_list);
  227. }
  228. #endif