Back to home page

LXR

 
 

    


0001 /*
0002  * lib/plist.c
0003  *
0004  * Descending-priority-sorted double-linked list
0005  *
0006  * (C) 2002-2003 Intel Corp
0007  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
0008  *
0009  * 2001-2005 (c) MontaVista Software, Inc.
0010  * Daniel Walker <dwalker@mvista.com>
0011  *
0012  * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
0013  *
0014  * Simplifications of the original code by
0015  * Oleg Nesterov <oleg@tv-sign.ru>
0016  *
0017  * Licensed under the FSF's GNU Public License v2 or later.
0018  *
0019  * Based on simple lists (include/linux/list.h).
0020  *
0021  * This file contains the add / del functions which are considered to
0022  * be too large to inline. See include/linux/plist.h for further
0023  * information.
0024  */
0025 
0026 #include <linux/bug.h>
0027 #include <linux/plist.h>
0028 
0029 #ifdef CONFIG_DEBUG_PI_LIST
0030 
0031 static struct plist_head test_head;
0032 
0033 static void plist_check_prev_next(struct list_head *t, struct list_head *p,
0034                   struct list_head *n)
0035 {
0036     WARN(n->prev != p || p->next != n,
0037             "top: %p, n: %p, p: %p\n"
0038             "prev: %p, n: %p, p: %p\n"
0039             "next: %p, n: %p, p: %p\n",
0040              t, t->next, t->prev,
0041             p, p->next, p->prev,
0042             n, n->next, n->prev);
0043 }
0044 
0045 static void plist_check_list(struct list_head *top)
0046 {
0047     struct list_head *prev = top, *next = top->next;
0048 
0049     plist_check_prev_next(top, prev, next);
0050     while (next != top) {
0051         prev = next;
0052         next = prev->next;
0053         plist_check_prev_next(top, prev, next);
0054     }
0055 }
0056 
0057 static void plist_check_head(struct plist_head *head)
0058 {
0059     if (!plist_head_empty(head))
0060         plist_check_list(&plist_first(head)->prio_list);
0061     plist_check_list(&head->node_list);
0062 }
0063 
0064 #else
0065 # define plist_check_head(h)    do { } while (0)
0066 #endif
0067 
0068 /**
0069  * plist_add - add @node to @head
0070  *
0071  * @node:   &struct plist_node pointer
0072  * @head:   &struct plist_head pointer
0073  */
0074 void plist_add(struct plist_node *node, struct plist_head *head)
0075 {
0076     struct plist_node *first, *iter, *prev = NULL;
0077     struct list_head *node_next = &head->node_list;
0078 
0079     plist_check_head(head);
0080     WARN_ON(!plist_node_empty(node));
0081     WARN_ON(!list_empty(&node->prio_list));
0082 
0083     if (plist_head_empty(head))
0084         goto ins_node;
0085 
0086     first = iter = plist_first(head);
0087 
0088     do {
0089         if (node->prio < iter->prio) {
0090             node_next = &iter->node_list;
0091             break;
0092         }
0093 
0094         prev = iter;
0095         iter = list_entry(iter->prio_list.next,
0096                 struct plist_node, prio_list);
0097     } while (iter != first);
0098 
0099     if (!prev || prev->prio != node->prio)
0100         list_add_tail(&node->prio_list, &iter->prio_list);
0101 ins_node:
0102     list_add_tail(&node->node_list, node_next);
0103 
0104     plist_check_head(head);
0105 }
0106 
0107 /**
0108  * plist_del - Remove a @node from plist.
0109  *
0110  * @node:   &struct plist_node pointer - entry to be removed
0111  * @head:   &struct plist_head pointer - list head
0112  */
0113 void plist_del(struct plist_node *node, struct plist_head *head)
0114 {
0115     plist_check_head(head);
0116 
0117     if (!list_empty(&node->prio_list)) {
0118         if (node->node_list.next != &head->node_list) {
0119             struct plist_node *next;
0120 
0121             next = list_entry(node->node_list.next,
0122                     struct plist_node, node_list);
0123 
0124             /* add the next plist_node into prio_list */
0125             if (list_empty(&next->prio_list))
0126                 list_add(&next->prio_list, &node->prio_list);
0127         }
0128         list_del_init(&node->prio_list);
0129     }
0130 
0131     list_del_init(&node->node_list);
0132 
0133     plist_check_head(head);
0134 }
0135 
0136 /**
0137  * plist_requeue - Requeue @node at end of same-prio entries.
0138  *
0139  * This is essentially an optimized plist_del() followed by
0140  * plist_add().  It moves an entry already in the plist to
0141  * after any other same-priority entries.
0142  *
0143  * @node:   &struct plist_node pointer - entry to be moved
0144  * @head:   &struct plist_head pointer - list head
0145  */
0146 void plist_requeue(struct plist_node *node, struct plist_head *head)
0147 {
0148     struct plist_node *iter;
0149     struct list_head *node_next = &head->node_list;
0150 
0151     plist_check_head(head);
0152     BUG_ON(plist_head_empty(head));
0153     BUG_ON(plist_node_empty(node));
0154 
0155     if (node == plist_last(head))
0156         return;
0157 
0158     iter = plist_next(node);
0159 
0160     if (node->prio != iter->prio)
0161         return;
0162 
0163     plist_del(node, head);
0164 
0165     plist_for_each_continue(iter, head) {
0166         if (node->prio != iter->prio) {
0167             node_next = &iter->node_list;
0168             break;
0169         }
0170     }
0171     list_add_tail(&node->node_list, node_next);
0172 
0173     plist_check_head(head);
0174 }
0175 
0176 #ifdef CONFIG_DEBUG_PI_LIST
0177 #include <linux/sched.h>
0178 #include <linux/module.h>
0179 #include <linux/init.h>
0180 
0181 static struct plist_node __initdata test_node[241];
0182 
0183 static void __init plist_test_check(int nr_expect)
0184 {
0185     struct plist_node *first, *prio_pos, *node_pos;
0186 
0187     if (plist_head_empty(&test_head)) {
0188         BUG_ON(nr_expect != 0);
0189         return;
0190     }
0191 
0192     prio_pos = first = plist_first(&test_head);
0193     plist_for_each(node_pos, &test_head) {
0194         if (nr_expect-- < 0)
0195             break;
0196         if (node_pos == first)
0197             continue;
0198         if (node_pos->prio == prio_pos->prio) {
0199             BUG_ON(!list_empty(&node_pos->prio_list));
0200             continue;
0201         }
0202 
0203         BUG_ON(prio_pos->prio > node_pos->prio);
0204         BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
0205         prio_pos = node_pos;
0206     }
0207 
0208     BUG_ON(nr_expect != 0);
0209     BUG_ON(prio_pos->prio_list.next != &first->prio_list);
0210 }
0211 
0212 static void __init plist_test_requeue(struct plist_node *node)
0213 {
0214     plist_requeue(node, &test_head);
0215 
0216     if (node != plist_last(&test_head))
0217         BUG_ON(node->prio == plist_next(node)->prio);
0218 }
0219 
0220 static int  __init plist_test(void)
0221 {
0222     int nr_expect = 0, i, loop;
0223     unsigned int r = local_clock();
0224 
0225     printk(KERN_DEBUG "start plist test\n");
0226     plist_head_init(&test_head);
0227     for (i = 0; i < ARRAY_SIZE(test_node); i++)
0228         plist_node_init(test_node + i, 0);
0229 
0230     for (loop = 0; loop < 1000; loop++) {
0231         r = r * 193939 % 47629;
0232         i = r % ARRAY_SIZE(test_node);
0233         if (plist_node_empty(test_node + i)) {
0234             r = r * 193939 % 47629;
0235             test_node[i].prio = r % 99;
0236             plist_add(test_node + i, &test_head);
0237             nr_expect++;
0238         } else {
0239             plist_del(test_node + i, &test_head);
0240             nr_expect--;
0241         }
0242         plist_test_check(nr_expect);
0243         if (!plist_node_empty(test_node + i)) {
0244             plist_test_requeue(test_node + i);
0245             plist_test_check(nr_expect);
0246         }
0247     }
0248 
0249     for (i = 0; i < ARRAY_SIZE(test_node); i++) {
0250         if (plist_node_empty(test_node + i))
0251             continue;
0252         plist_del(test_node + i, &test_head);
0253         nr_expect--;
0254         plist_test_check(nr_expect);
0255     }
0256 
0257     printk(KERN_DEBUG "end plist test\n");
0258     return 0;
0259 }
0260 
0261 module_init(plist_test);
0262 
0263 #endif