blob: 8c614d0e6c356962a4623116c86c28a7b9430b15 [file] [log] [blame]
Ingo Molnar77ba89c2006-06-27 02:54:51 -07001/*
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
26#include <linux/plist.h>
27#include <linux/spinlock.h>
28
29#ifdef CONFIG_DEBUG_PI_LIST
30
31static void plist_check_prev_next(struct list_head *t, struct list_head *p,
32 struct list_head *n)
33{
Arjan van de Ven5cd2b452008-07-25 19:45:39 -070034 WARN(n->prev != p || p->next != n,
35 "top: %p, n: %p, p: %p\n"
36 "prev: %p, n: %p, p: %p\n"
37 "next: %p, n: %p, p: %p\n",
38 t, t->next, t->prev,
39 p, p->next, p->prev,
40 n, n->next, n->prev);
Ingo Molnar77ba89c2006-06-27 02:54:51 -070041}
42
43static void plist_check_list(struct list_head *top)
44{
45 struct list_head *prev = top, *next = top->next;
46
47 plist_check_prev_next(top, prev, next);
48 while (next != top) {
49 prev = next;
50 next = prev->next;
51 plist_check_prev_next(top, prev, next);
52 }
53}
54
55static void plist_check_head(struct plist_head *head)
56{
Thomas Gleixnera2672452009-11-17 14:46:14 +010057 WARN_ON(!head->rawlock && !head->spinlock);
58 if (head->rawlock)
59 WARN_ON_SMP(!raw_spin_is_locked(head->rawlock));
60 if (head->spinlock)
61 WARN_ON_SMP(!spin_is_locked(head->spinlock));
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +080062 if (!plist_head_empty(head))
63 plist_check_list(&plist_first(head)->prio_list);
Ingo Molnar77ba89c2006-06-27 02:54:51 -070064 plist_check_list(&head->node_list);
65}
66
67#else
68# define plist_check_head(h) do { } while (0)
69#endif
70
71/**
72 * plist_add - add @node to @head
73 *
74 * @node: &struct plist_node pointer
75 * @head: &struct plist_head pointer
76 */
77void plist_add(struct plist_node *node, struct plist_head *head)
78{
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +080079 struct plist_node *first, *iter, *prev = NULL;
80 struct list_head *node_next = &head->node_list;
Ingo Molnar77ba89c2006-06-27 02:54:51 -070081
82 plist_check_head(head);
83 WARN_ON(!plist_node_empty(node));
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +080084 WARN_ON(!list_empty(&node->prio_list));
Ingo Molnar77ba89c2006-06-27 02:54:51 -070085
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +080086 if (plist_head_empty(head))
87 goto ins_node;
88
89 first = iter = plist_first(head);
90
91 do {
92 if (node->prio < iter->prio) {
93 node_next = &iter->node_list;
94 break;
Ingo Molnar77ba89c2006-06-27 02:54:51 -070095 }
Ingo Molnar77ba89c2006-06-27 02:54:51 -070096
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +080097 prev = iter;
98 iter = list_entry(iter->prio_list.next,
99 struct plist_node, prio_list);
100 } while (iter != first);
101
102 if (!prev || prev->prio != node->prio)
103 list_add_tail(&node->prio_list, &iter->prio_list);
104ins_node:
105 list_add_tail(&node->node_list, node_next);
Ingo Molnar77ba89c2006-06-27 02:54:51 -0700106
107 plist_check_head(head);
108}
109
110/**
111 * plist_del - Remove a @node from plist.
112 *
113 * @node: &struct plist_node pointer - entry to be removed
114 * @head: &struct plist_head pointer - list head
115 */
116void plist_del(struct plist_node *node, struct plist_head *head)
117{
118 plist_check_head(head);
119
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +0800120 if (!list_empty(&node->prio_list)) {
121 if (node->node_list.next != &head->node_list) {
122 struct plist_node *next;
Ingo Molnar77ba89c2006-06-27 02:54:51 -0700123
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +0800124 next = list_entry(node->node_list.next,
125 struct plist_node, node_list);
126
127 /* add the next plist_node into prio_list */
128 if (list_empty(&next->prio_list))
129 list_add(&next->prio_list, &node->prio_list);
130 }
131 list_del_init(&node->prio_list);
Ingo Molnar77ba89c2006-06-27 02:54:51 -0700132 }
133
Lai Jiangshanbf6a9b82010-12-21 17:55:14 +0800134 list_del_init(&node->node_list);
Ingo Molnar77ba89c2006-06-27 02:54:51 -0700135
136 plist_check_head(head);
137}