@@ -138,7 +138,35 @@ static inline void plist_node_init(struct plist_node *node, int prio)
INIT_LIST_HEAD(&node->node_list);
}
-extern void plist_add(struct plist_node *node, struct plist_head *head);
+extern void __plist_add(struct plist_node *node,
+ struct plist_head *head, bool is_head);
+
+/**
+ * plist_add_head - add @node to @head, before all existing same-prio nodes
+ *
+ * @node: &struct plist_node pointer
+ * @head: &struct plist_head pointer
+ */
+static inline
+void plist_add_head(struct plist_node *node, struct plist_head *head)
+{
+ __plist_add(node, head, true);
+}
+
+/**
+ * plist_add_tail - add @node to @head, after all existing same-prio nodes
+ *
+ * @node: &struct plist_node pointer
+ * @head: &struct plist_head pointer
+ */
+static inline
+void plist_add_tail(struct plist_node *node, struct plist_head *head)
+{
+ __plist_add(node, head, false);
+}
+
+#define plist_add plist_add_tail
+
extern void plist_del(struct plist_node *node, struct plist_head *head);
extern void plist_requeue(struct plist_node *node, struct plist_head *head);
@@ -66,12 +66,16 @@ static void plist_check_head(struct plist_head *head)
#endif
/**
- * plist_add - add @node to @head
+ * __plist_add - add @node to @head
*
* @node: &struct plist_node pointer
* @head: &struct plist_head pointer
+ * @is_head: bool
+ *
+ * If there're any nodes with the same prio, add @node
+ * behind or before all of them according to @is_head.
*/
-void plist_add(struct plist_node *node, struct plist_head *head)
+void __plist_add(struct plist_node *node, struct plist_head *head, bool is_head)
{
struct plist_node *first, *iter, *prev = NULL;
struct list_head *node_next = &head->node_list;
@@ -96,8 +100,20 @@ void plist_add(struct plist_node *node, struct plist_head *head)
struct plist_node, prio_list);
} while (iter != first);
- if (!prev || prev->prio != node->prio)
+ if (!prev || prev->prio != node->prio) {
list_add_tail(&node->prio_list, &iter->prio_list);
+ } else if (is_head) {
+ /*
+ * prev has the same priority as the node that is being
+ * added. It is also the first node for this priority,
+ * but the new node needs to be added ahead of it.
+ * To accomplish this, replace prev in the prio_list
+ * with node. Then set node_next to prev->node_list so
+ * that the new node gets added before prev and not iter.
+ */
+ list_replace_init(&prev->prio_list, &node->prio_list);
+ node_next = &prev->node_list;
+ }
ins_node:
list_add_tail(&node->node_list, node_next);