plist.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. * pl:prio_list (only for plist_node)
  35. * nl:node_list
  36. * HEAD| NODE(S)
  37. * |
  38. * ||------------------------------------|
  39. * ||->|pl|<->|pl|<--------------->|pl|<-|
  40. * | |10| |21| |21| |21| |40| (prio)
  41. * | | | | | | | | | | |
  42. * | | | | | | | | | | |
  43. * |->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<-|
  44. * |-------------------------------------------|
  45. *
  46. * The nodes on the prio_list list are sorted by priority to simplify
  47. * the insertion of new nodes. There are no nodes with duplicate
  48. * priorites on the list.
  49. *
  50. * The nodes on the node_list are ordered by priority and can contain
  51. * entries which have the same priority. Those entries are ordered
  52. * FIFO
  53. *
  54. * Addition means: look for the prio_list node in the prio_list
  55. * for the priority of the node and insert it before the node_list
  56. * entry of the next prio_list node. If it is the first node of
  57. * that priority, add it to the prio_list in the right position and
  58. * insert it into the serialized node_list list
  59. *
  60. * Removal means remove it from the node_list and remove it from
  61. * the prio_list if the node_list list_head is non empty. In case
  62. * of removal from the prio_list it must be checked whether other
  63. * entries of the same priority are on the list or not. If there
  64. * is another entry of the same priority then this entry has to
  65. * replace the removed entry on the prio_list. If the entry which
  66. * is removed is the only entry of this priority then a simple
  67. * remove from both list is sufficient.
  68. *
  69. * INT_MIN is the highest priority, 0 is the medium highest, INT_MAX
  70. * is lowest priority.
  71. *
  72. * No locking is done, up to the caller.
  73. *
  74. */
  75. #ifndef _LINUX_PLIST_H_
  76. #define _LINUX_PLIST_H_
  77. #include <linux/kernel.h>
  78. #include <linux/list.h>
  79. struct plist_head {
  80. struct list_head node_list;
  81. };
  82. struct plist_node {
  83. int prio;
  84. struct list_head prio_list;
  85. struct list_head node_list;
  86. };
  87. /**
  88. * PLIST_HEAD_INIT - static struct plist_head initializer
  89. * @head: struct plist_head variable name
  90. */
  91. #define PLIST_HEAD_INIT(head) \
  92. { \
  93. .node_list = LIST_HEAD_INIT((head).node_list) \
  94. }
  95. /**
  96. * PLIST_HEAD - declare and init plist_head
  97. * @head: name for struct plist_head variable
  98. */
  99. #define PLIST_HEAD(head) \
  100. struct plist_head head = PLIST_HEAD_INIT(head)
  101. /**
  102. * PLIST_NODE_INIT - static struct plist_node initializer
  103. * @node: struct plist_node variable name
  104. * @__prio: initial node priority
  105. */
  106. #define PLIST_NODE_INIT(node, __prio) \
  107. { \
  108. .prio = (__prio), \
  109. .prio_list = LIST_HEAD_INIT((node).prio_list), \
  110. .node_list = LIST_HEAD_INIT((node).node_list), \
  111. }
  112. /**
  113. * plist_head_init - dynamic struct plist_head initializer
  114. * @head: &struct plist_head pointer
  115. */
  116. static inline void
  117. plist_head_init(struct plist_head *head)
  118. {
  119. INIT_LIST_HEAD(&head->node_list);
  120. }
  121. /**
  122. * plist_node_init - Dynamic struct plist_node initializer
  123. * @node: &struct plist_node pointer
  124. * @prio: initial node priority
  125. */
  126. static inline void plist_node_init(struct plist_node *node, int prio)
  127. {
  128. node->prio = prio;
  129. INIT_LIST_HEAD(&node->prio_list);
  130. INIT_LIST_HEAD(&node->node_list);
  131. }
  132. extern void plist_add(struct plist_node *node, struct plist_head *head);
  133. extern void plist_del(struct plist_node *node, struct plist_head *head);
  134. extern void plist_requeue(struct plist_node *node, struct plist_head *head);
  135. /**
  136. * plist_for_each - iterate over the plist
  137. * @pos: the type * to use as a loop counter
  138. * @head: the head for your list
  139. */
  140. #define plist_for_each(pos, head) \
  141. list_for_each_entry(pos, &(head)->node_list, node_list)
  142. /**
  143. * plist_for_each_continue - continue iteration over the plist
  144. * @pos: the type * to use as a loop cursor
  145. * @head: the head for your list
  146. *
  147. * Continue to iterate over plist, continuing after the current position.
  148. */
  149. #define plist_for_each_continue(pos, head) \
  150. list_for_each_entry_continue(pos, &(head)->node_list, node_list)
  151. /**
  152. * plist_for_each_safe - iterate safely over a plist of given type
  153. * @pos: the type * to use as a loop counter
  154. * @n: another type * to use as temporary storage
  155. * @head: the head for your list
  156. *
  157. * Iterate over a plist of given type, safe against removal of list entry.
  158. */
  159. #define plist_for_each_safe(pos, n, head) \
  160. list_for_each_entry_safe(pos, n, &(head)->node_list, node_list)
  161. /**
  162. * plist_for_each_entry - iterate over list of given type
  163. * @pos: the type * to use as a loop counter
  164. * @head: the head for your list
  165. * @mem: the name of the list_head within the struct
  166. */
  167. #define plist_for_each_entry(pos, head, mem) \
  168. list_for_each_entry(pos, &(head)->node_list, mem.node_list)
  169. /**
  170. * plist_for_each_entry_continue - continue iteration over list of given type
  171. * @pos: the type * to use as a loop cursor
  172. * @head: the head for your list
  173. * @m: the name of the list_head within the struct
  174. *
  175. * Continue to iterate over list of given type, continuing after
  176. * the current position.
  177. */
  178. #define plist_for_each_entry_continue(pos, head, m) \
  179. list_for_each_entry_continue(pos, &(head)->node_list, m.node_list)
  180. /**
  181. * plist_for_each_entry_safe - iterate safely over list of given type
  182. * @pos: the type * to use as a loop counter
  183. * @n: another type * to use as temporary storage
  184. * @head: the head for your list
  185. * @m: the name of the list_head within the struct
  186. *
  187. * Iterate over list of given type, safe against removal of list entry.
  188. */
  189. #define plist_for_each_entry_safe(pos, n, head, m) \
  190. list_for_each_entry_safe(pos, n, &(head)->node_list, m.node_list)
  191. /**
  192. * plist_head_empty - return !0 if a plist_head is empty
  193. * @head: &struct plist_head pointer
  194. */
  195. static inline int plist_head_empty(const struct plist_head *head)
  196. {
  197. return list_empty(&head->node_list);
  198. }
  199. /**
  200. * plist_node_empty - return !0 if plist_node is not on a list
  201. * @node: &struct plist_node pointer
  202. */
  203. static inline int plist_node_empty(const struct plist_node *node)
  204. {
  205. return list_empty(&node->node_list);
  206. }
  207. /* All functions below assume the plist_head is not empty. */
  208. /**
  209. * plist_first_entry - get the struct for the first entry
  210. * @head: the &struct plist_head pointer
  211. * @type: the type of the struct this is embedded in
  212. * @member: the name of the list_head within the struct
  213. */
  214. #ifdef CONFIG_DEBUG_PI_LIST
  215. # define plist_first_entry(head, type, member) \
  216. ({ \
  217. WARN_ON(plist_head_empty(head)); \
  218. container_of(plist_first(head), type, member); \
  219. })
  220. #else
  221. # define plist_first_entry(head, type, member) \
  222. container_of(plist_first(head), type, member)
  223. #endif
  224. /**
  225. * plist_last_entry - get the struct for the last entry
  226. * @head: the &struct plist_head pointer
  227. * @type: the type of the struct this is embedded in
  228. * @member: the name of the list_head within the struct
  229. */
  230. #ifdef CONFIG_DEBUG_PI_LIST
  231. # define plist_last_entry(head, type, member) \
  232. ({ \
  233. WARN_ON(plist_head_empty(head)); \
  234. container_of(plist_last(head), type, member); \
  235. })
  236. #else
  237. # define plist_last_entry(head, type, member) \
  238. container_of(plist_last(head), type, member)
  239. #endif
  240. /**
  241. * plist_next - get the next entry in list
  242. * @pos: the type * to cursor
  243. */
  244. #define plist_next(pos) \
  245. list_next_entry(pos, node_list)
  246. /**
  247. * plist_prev - get the prev entry in list
  248. * @pos: the type * to cursor
  249. */
  250. #define plist_prev(pos) \
  251. list_prev_entry(pos, node_list)
  252. /**
  253. * plist_first - return the first node (and thus, highest priority)
  254. * @head: the &struct plist_head pointer
  255. *
  256. * Assumes the plist is _not_ empty.
  257. */
  258. static inline struct plist_node *plist_first(const struct plist_head *head)
  259. {
  260. return list_entry(head->node_list.next,
  261. struct plist_node, node_list);
  262. }
  263. /**
  264. * plist_last - return the last node (and thus, lowest priority)
  265. * @head: the &struct plist_head pointer
  266. *
  267. * Assumes the plist is _not_ empty.
  268. */
  269. static inline struct plist_node *plist_last(const struct plist_head *head)
  270. {
  271. return list_entry(head->node_list.prev,
  272. struct plist_node, node_list);
  273. }
  274. #endif