Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Descending-priority-sorted double-linked list
0004  *
0005  * (C) 2002-2003 Intel Corp
0006  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
0007  *
0008  * 2001-2005 (c) MontaVista Software, Inc.
0009  * Daniel Walker <dwalker@mvista.com>
0010  *
0011  * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
0012  *
0013  * Simplifications of the original code by
0014  * Oleg Nesterov <oleg@tv-sign.ru>
0015  *
0016  * Based on simple lists (include/linux/list.h).
0017  *
0018  * This is a priority-sorted list of nodes; each node has a
0019  * priority from INT_MIN (highest) to INT_MAX (lowest).
0020  *
0021  * Addition is O(K), removal is O(1), change of priority of a node is
0022  * O(K) and K is the number of RT priority levels used in the system.
0023  * (1 <= K <= 99)
0024  *
0025  * This list is really a list of lists:
0026  *
0027  *  - The tier 1 list is the prio_list, different priority nodes.
0028  *
0029  *  - The tier 2 list is the node_list, serialized nodes.
0030  *
0031  * Simple ASCII art explanation:
0032  *
0033  * pl:prio_list (only for plist_node)
0034  * nl:node_list
0035  *   HEAD|             NODE(S)
0036  *       |
0037  *       ||------------------------------------|
0038  *       ||->|pl|<->|pl|<--------------->|pl|<-|
0039  *       |   |10|   |21|   |21|   |21|   |40|   (prio)
0040  *       |   |  |   |  |   |  |   |  |   |  |
0041  *       |   |  |   |  |   |  |   |  |   |  |
0042  * |->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<-|
0043  * |-------------------------------------------|
0044  *
0045  * The nodes on the prio_list list are sorted by priority to simplify
0046  * the insertion of new nodes. There are no nodes with duplicate
0047  * priorites on the list.
0048  *
0049  * The nodes on the node_list are ordered by priority and can contain
0050  * entries which have the same priority. Those entries are ordered
0051  * FIFO
0052  *
0053  * Addition means: look for the prio_list node in the prio_list
0054  * for the priority of the node and insert it before the node_list
0055  * entry of the next prio_list node. If it is the first node of
0056  * that priority, add it to the prio_list in the right position and
0057  * insert it into the serialized node_list list
0058  *
0059  * Removal means remove it from the node_list and remove it from
0060  * the prio_list if the node_list list_head is non empty. In case
0061  * of removal from the prio_list it must be checked whether other
0062  * entries of the same priority are on the list or not. If there
0063  * is another entry of the same priority then this entry has to
0064  * replace the removed entry on the prio_list. If the entry which
0065  * is removed is the only entry of this priority then a simple
0066  * remove from both list is sufficient.
0067  *
0068  * INT_MIN is the highest priority, 0 is the medium highest, INT_MAX
0069  * is lowest priority.
0070  *
0071  * No locking is done, up to the caller.
0072  */
0073 #ifndef _LINUX_PLIST_H_
0074 #define _LINUX_PLIST_H_
0075 
0076 #include <linux/container_of.h>
0077 #include <linux/list.h>
0078 #include <linux/types.h>
0079 
0080 #include <asm/bug.h>
0081 
0082 struct plist_head {
0083     struct list_head node_list;
0084 };
0085 
0086 struct plist_node {
0087     int         prio;
0088     struct list_head    prio_list;
0089     struct list_head    node_list;
0090 };
0091 
0092 /**
0093  * PLIST_HEAD_INIT - static struct plist_head initializer
0094  * @head:   struct plist_head variable name
0095  */
0096 #define PLIST_HEAD_INIT(head)               \
0097 {                           \
0098     .node_list = LIST_HEAD_INIT((head).node_list)   \
0099 }
0100 
0101 /**
0102  * PLIST_HEAD - declare and init plist_head
0103  * @head:   name for struct plist_head variable
0104  */
0105 #define PLIST_HEAD(head) \
0106     struct plist_head head = PLIST_HEAD_INIT(head)
0107 
0108 /**
0109  * PLIST_NODE_INIT - static struct plist_node initializer
0110  * @node:   struct plist_node variable name
0111  * @__prio: initial node priority
0112  */
0113 #define PLIST_NODE_INIT(node, __prio)           \
0114 {                           \
0115     .prio  = (__prio),              \
0116     .prio_list = LIST_HEAD_INIT((node).prio_list),  \
0117     .node_list = LIST_HEAD_INIT((node).node_list),  \
0118 }
0119 
0120 /**
0121  * plist_head_init - dynamic struct plist_head initializer
0122  * @head:   &struct plist_head pointer
0123  */
0124 static inline void
0125 plist_head_init(struct plist_head *head)
0126 {
0127     INIT_LIST_HEAD(&head->node_list);
0128 }
0129 
0130 /**
0131  * plist_node_init - Dynamic struct plist_node initializer
0132  * @node:   &struct plist_node pointer
0133  * @prio:   initial node priority
0134  */
0135 static inline void plist_node_init(struct plist_node *node, int prio)
0136 {
0137     node->prio = prio;
0138     INIT_LIST_HEAD(&node->prio_list);
0139     INIT_LIST_HEAD(&node->node_list);
0140 }
0141 
0142 extern void plist_add(struct plist_node *node, struct plist_head *head);
0143 extern void plist_del(struct plist_node *node, struct plist_head *head);
0144 
0145 extern void plist_requeue(struct plist_node *node, struct plist_head *head);
0146 
0147 /**
0148  * plist_for_each - iterate over the plist
0149  * @pos:    the type * to use as a loop counter
0150  * @head:   the head for your list
0151  */
0152 #define plist_for_each(pos, head)   \
0153      list_for_each_entry(pos, &(head)->node_list, node_list)
0154 
0155 /**
0156  * plist_for_each_continue - continue iteration over the plist
0157  * @pos:    the type * to use as a loop cursor
0158  * @head:   the head for your list
0159  *
0160  * Continue to iterate over plist, continuing after the current position.
0161  */
0162 #define plist_for_each_continue(pos, head)  \
0163      list_for_each_entry_continue(pos, &(head)->node_list, node_list)
0164 
0165 /**
0166  * plist_for_each_safe - iterate safely over a plist of given type
0167  * @pos:    the type * to use as a loop counter
0168  * @n:  another type * to use as temporary storage
0169  * @head:   the head for your list
0170  *
0171  * Iterate over a plist of given type, safe against removal of list entry.
0172  */
0173 #define plist_for_each_safe(pos, n, head)   \
0174      list_for_each_entry_safe(pos, n, &(head)->node_list, node_list)
0175 
0176 /**
0177  * plist_for_each_entry - iterate over list of given type
0178  * @pos:    the type * to use as a loop counter
0179  * @head:   the head for your list
0180  * @mem:    the name of the list_head within the struct
0181  */
0182 #define plist_for_each_entry(pos, head, mem)    \
0183      list_for_each_entry(pos, &(head)->node_list, mem.node_list)
0184 
0185 /**
0186  * plist_for_each_entry_continue - continue iteration over list of given type
0187  * @pos:    the type * to use as a loop cursor
0188  * @head:   the head for your list
0189  * @m:      the name of the list_head within the struct
0190  *
0191  * Continue to iterate over list of given type, continuing after
0192  * the current position.
0193  */
0194 #define plist_for_each_entry_continue(pos, head, m) \
0195     list_for_each_entry_continue(pos, &(head)->node_list, m.node_list)
0196 
0197 /**
0198  * plist_for_each_entry_safe - iterate safely over list of given type
0199  * @pos:    the type * to use as a loop counter
0200  * @n:      another type * to use as temporary storage
0201  * @head:   the head for your list
0202  * @m:      the name of the list_head within the struct
0203  *
0204  * Iterate over list of given type, safe against removal of list entry.
0205  */
0206 #define plist_for_each_entry_safe(pos, n, head, m)  \
0207     list_for_each_entry_safe(pos, n, &(head)->node_list, m.node_list)
0208 
0209 /**
0210  * plist_head_empty - return !0 if a plist_head is empty
0211  * @head:   &struct plist_head pointer
0212  */
0213 static inline int plist_head_empty(const struct plist_head *head)
0214 {
0215     return list_empty(&head->node_list);
0216 }
0217 
0218 /**
0219  * plist_node_empty - return !0 if plist_node is not on a list
0220  * @node:   &struct plist_node pointer
0221  */
0222 static inline int plist_node_empty(const struct plist_node *node)
0223 {
0224     return list_empty(&node->node_list);
0225 }
0226 
0227 /* All functions below assume the plist_head is not empty. */
0228 
0229 /**
0230  * plist_first_entry - get the struct for the first entry
0231  * @head:   the &struct plist_head pointer
0232  * @type:   the type of the struct this is embedded in
0233  * @member: the name of the list_head within the struct
0234  */
0235 #ifdef CONFIG_DEBUG_PLIST
0236 # define plist_first_entry(head, type, member)  \
0237 ({ \
0238     WARN_ON(plist_head_empty(head)); \
0239     container_of(plist_first(head), type, member); \
0240 })
0241 #else
0242 # define plist_first_entry(head, type, member)  \
0243     container_of(plist_first(head), type, member)
0244 #endif
0245 
0246 /**
0247  * plist_last_entry - get the struct for the last entry
0248  * @head:   the &struct plist_head pointer
0249  * @type:   the type of the struct this is embedded in
0250  * @member: the name of the list_head within the struct
0251  */
0252 #ifdef CONFIG_DEBUG_PLIST
0253 # define plist_last_entry(head, type, member)   \
0254 ({ \
0255     WARN_ON(plist_head_empty(head)); \
0256     container_of(plist_last(head), type, member); \
0257 })
0258 #else
0259 # define plist_last_entry(head, type, member)   \
0260     container_of(plist_last(head), type, member)
0261 #endif
0262 
0263 /**
0264  * plist_next - get the next entry in list
0265  * @pos:    the type * to cursor
0266  */
0267 #define plist_next(pos) \
0268     list_next_entry(pos, node_list)
0269 
0270 /**
0271  * plist_prev - get the prev entry in list
0272  * @pos:    the type * to cursor
0273  */
0274 #define plist_prev(pos) \
0275     list_prev_entry(pos, node_list)
0276 
0277 /**
0278  * plist_first - return the first node (and thus, highest priority)
0279  * @head:   the &struct plist_head pointer
0280  *
0281  * Assumes the plist is _not_ empty.
0282  */
0283 static inline struct plist_node *plist_first(const struct plist_head *head)
0284 {
0285     return list_entry(head->node_list.next,
0286               struct plist_node, node_list);
0287 }
0288 
0289 /**
0290  * plist_last - return the last node (and thus, lowest priority)
0291  * @head:   the &struct plist_head pointer
0292  *
0293  * Assumes the plist is _not_ empty.
0294  */
0295 static inline struct plist_node *plist_last(const struct plist_head *head)
0296 {
0297     return list_entry(head->node_list.prev,
0298               struct plist_node, node_list);
0299 }
0300 
0301 #endif