zoukankan      html  css  js  c++  java
  • linux内核链表的实现

    .linux-2.6.22.6_vscodeincludelinuxlist.h

    #ifndef _LINUX_LIST_H
    #define _LINUX_LIST_H

    #ifdef __KERNEL__

    #include <linux/stddef.h>
    #include <linux/poison.h>
    #include <linux/prefetch.h>
    #include <asm/system.h>

    /*
    * Simple doubly linked list implementation.
    *
    * Some of the internal functions ("__xxx") are useful when
    * manipulating whole lists rather than single entries, as
    * sometimes we already know the next/prev entries and we can
    * generate better code by using them directly rather than
    * using the generic single-entry routines.

    */

     

    //链表头

    struct list_head {
            struct list_head *next, *prev;
    };

       

    //(静态)初始化链表头

    #define LIST_HEAD_INIT(name) { &(name), &(name) }

    #define LIST_HEAD(name)
    struct list_head name = LIST_HEAD_INIT(name)

       

    //(动态)初始化链表头

    static inline void INIT_LIST_HEAD(struct list_head *list)
    {
    list->next = list;
    list->prev = list;
    }

    /*
    * Insert a new entry between two known consecutive entries.
    *
    * This is only for internal list manipulation where we know
    * the prev/next entries already!
    */
    #ifndef CONFIG_DEBUG_LIST
    static inline void __list_add(struct list_head *new,
                             struct
    list_head *prev,
                             struct list_head *next)
    {
            next->prev = new;
            new->next = next;
            new->prev = prev;
            prev->next = new;
    }
    #else
    extern void __list_add(struct list_head *new,
                             struct
    list_head *prev,
                             struct list_head *next);
    #endif

    /**
    * list_add - add a new entry
    * @new: new entry to be added
    * @head: list head to add it after
    *
    * Insert a new entry after the specified head.
    * This is good for implementing stacks.
    */
    #ifndef CONFIG_DEBUG_LIST
    static inline void list_add(struct list_head *new, struct list_head *head)
    {
            __list_add(new, head, head->next);
    }
    #else
    extern void list_add(struct list_head *new, struct list_head *head);
    #endif


    /**
    * list_add_tail - add a new entry
    * @new: new entry to be added
    * @head: list head to add it before
    *
    * Insert a new entry before the specified head.
    * This is useful for implementing queues.
    */
    static inline void list_add_tail(struct list_head *new, struct list_head *head)
    {
            __list_add(new, head->prev, head);
    }

    /*
    * Insert a new entry between two known consecutive entries.
    *
    * This is only for internal list manipulation where we know
    * the prev/next entries already!
    */
    static inline void __list_add_rcu(struct list_head * new,
                    struct
    list_head * prev, struct list_head * next)
    {
            new->next = next;
            new->prev = prev;
            smp_wmb(); //smp_wmb()防止编译器和CPU优化代码执行的顺序。在这里,smp_wmb保证在它之前的两行代码执行完了之后再执行后两行.

    //如果没有smp_wmb,代码在CPU中执行的顺序可能和源代码里的不一样!

            next->prev = new;
            prev->next = new;
    }

    /**
    * list_add_rcu - add a new entry to rcu-protected list
    * @new: new entry to be added
    * @head: list head to add it after
    *
    * Insert a new entry after the specified head.
    * This is good for implementing stacks.
    *
    * The caller must take whatever precautions are necessary
    * (such as holding appropriate locks) to avoid racing
    * with another list-mutation primitive, such as list_add_rcu()
    * or list_del_rcu(), running on this same list.
    * However, it is perfectly legal to run concurrently with
    * the _rcu list-traversal primitives, such as
    * list_for_each_entry_rcu().
    */
    static inline void list_add_rcu(struct list_head *new, struct list_head *head)
    {
            __list_add_rcu(new, head, head->next);
    }

    /**
    * list_add_tail_rcu - add a new entry to rcu-protected list
    * @new: new entry to be added
    * @head: list head to add it before
    *
    * Insert a new entry before the specified head.
    * This is useful for implementing queues.
    *
    * The caller must take whatever precautions are necessary
    * (such as holding appropriate locks) to avoid racing
    * with another list-mutation primitive, such as list_add_tail_rcu()
    * or list_del_rcu(), running on this same list.
    * However, it is perfectly legal to run concurrently with
    * the _rcu list-traversal primitives, such as
    * list_for_each_entry_rcu().
    */
    static inline void list_add_tail_rcu(struct list_head *new,
                                            struct
    list_head *head)
    {
            __list_add_rcu(new, head->prev, head);
    }

    /*
    * Delete a list entry by making the prev/next entries
    * point to each other.
    *
    * This is only for internal list manipulation where we know
    * the prev/next entries already!
    */
    static inline void __list_del(struct list_head * prev, struct list_head * next)
    {
            next->prev = prev;
            prev->next = next;
    }

    /**
    * list_del - deletes entry from list.
    * @entry: the element to delete from the list.
    * Note: list_empty() on entry does not return true after this, the entry is
    * in an undefined state.
    */
    #ifndef CONFIG_DEBUG_LIST
    static inline void list_del(struct list_head *entry)
    {
            __list_del(entry->prev, entry->next);
            entry->next = LIST_POISON1;
            entry->prev = LIST_POISON2;
    }
    #else
    extern void list_del(struct list_head *entry);
    #endif

    /**
    * list_del_rcu - deletes entry from list without re-initialization
    * @entry: the element to delete from the list.
    *
    * Note: list_empty() on entry does not return true after this,
    * the entry is in an undefined state. It is useful for RCU based
    * lockfree traversal.
    *
    * In particular, it means that we can not poison the forward
    * pointers that may still be used for walking the list.
    *
    * The caller must take whatever precautions are necessary
    * (such as holding appropriate locks) to avoid racing
    * with another list-mutation primitive, such as list_del_rcu()
    * or list_add_rcu(), running on this same list.
    * However, it is perfectly legal to run concurrently with
    * the _rcu list-traversal primitives, such as
    * list_for_each_entry_rcu().
    *
    * Note that the caller is not permitted to immediately free
    * the newly deleted entry. Instead, either synchronize_rcu()
    * or call_rcu() must be used to defer freeing until an RCU
    * grace period has elapsed.
    */
    static inline void list_del_rcu(struct list_head *entry)
    {
            __list_del(entry->prev, entry->next);
            entry->prev = LIST_POISON2;
    }

    /**
    * list_replace - replace old entry by new one
    * @old : the element to be replaced
    * @new : the new element to insert
    *
    * If @old was empty, it will be overwritten.
    */
    static inline void list_replace(struct list_head *old,
                                    struct list_head *new)
    {
            new->next = old->next;
            new->next->prev = new;
            new->prev = old->prev;
            new->prev->next = new;
    }

    static inline void list_replace_init(struct list_head *old,
                                            struct list_head *new)
    {
            list_replace(old, new);
            INIT_LIST_HEAD(old);
    }

    /**
    * list_replace_rcu - replace old entry by new one
    * @old : the element to be replaced
    * @new : the new element to insert
    *
    * The @old entry will be replaced with the @new entry atomically.
    * Note: @old should not be empty.
    */
    static inline void list_replace_rcu(struct list_head *old,
                                    struct list_head *new)
    {
            new->next = old->next;
            new->prev = old->prev;
            smp_wmb();
            new->next->prev = new;
            new->prev->next = new;
            old->prev = LIST_POISON2;
    }

    /**
    * list_del_init - deletes entry from list and reinitialize it.
    * @entry: the element to delete from the list.
    */
    static inline void list_del_init(struct list_head *entry)
    {
            __list_del(entry->prev, entry->next);
            INIT_LIST_HEAD(entry);
    }

    /**
    * list_move - delete from one list and add as another's head
    * @list: the entry to move
    * @head: the head that will precede our entry
    */
    static inline void list_move(struct list_head *list, struct list_head *head)
    {
            __list_del(list->prev, list->next);
            list_add(list, head);
    }

    /**
    * list_move_tail - delete from one list and add as another's tail
    * @list: the entry to move
    * @head: the head that will follow our entry
    */
    static inline void list_move_tail(struct list_head *list,
                                     struct list_head *head)
    {
            __list_del(list->prev, list->next);
            list_add_tail(list, head);
    }

    /**
    * list_is_last - tests whether @list is the last entry in list @head
    * @list: the entry to test
    * @head: the head of the list
    */
    static inline int list_is_last(const struct list_head *list,
                                    const struct list_head *head)
    {
            return list->next == head;
    }

    /**
    * list_empty - tests whether a list is empty
    * @head: the list to test.
    */
    static inline int list_empty(const struct list_head *head)
    {
            return head->next == head;
    }

    /**
    * list_empty_careful - tests whether a list is empty and not being modified
    * @head: the list to test
    *
    * Description:
    * tests whether a list is empty _and_ checks that no other CPU might be
    * in the process of modifying either member (next or prev)
    *
    * NOTE: using list_empty_careful() without synchronization
    * can only be safe if the only activity that can happen
    * to the list entry is list_del_init(). Eg. it cannot be used
    * if another CPU could re-list_add() it.
    */
    static inline int list_empty_careful(const struct list_head *head)
    {
            struct list_head *next = head->next;
            return (next == head) && (next == head->prev);
    }

    static inline void __list_splice(struct list_head *list,
                                     struct list_head *head)
    {
            struct list_head *first = list->next;
            struct list_head *last = list->prev;
            struct list_head *at = head->next;

            first->prev = head;
            head->next = first;

            last->next = at;
            at->prev = last;
    }

    /**
    * list_splice - join two lists
    * @list: the new list to add.
    * @head: the place to add it in the first list.
    */
    static inline void list_splice(struct list_head *list, struct list_head *head)
    {
            if (!list_empty(list))
                    __list_splice(list, head);
    }

    /**
    * list_splice_init - join two lists and reinitialise the emptied list.
    * @list: the new list to add.
    * @head: the place to add it in the first list.
    *
    * The list at @list is reinitialised
    */
    static inline void list_splice_init(struct list_head *list,
    357                                  struct list_head *head)
    358 {
    359         if (!list_empty(list)) {
    360                 __list_splice(list, head);
    361                 INIT_LIST_HEAD(list);
    362         }
    363 }
    364
    365 /**
    366 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
    367 * @list:        the RCU-protected list to splice
    368 * @head:        the place in the list to splice the first list into
    369 * @sync:        function to sync: synchronize_rcu(), synchronize_sched(), ...
    370 *
    371 * @head can be RCU-read traversed concurrently with this function.
    372 *
    373 * Note that this function blocks.
    374 *
    375 * Important note: the caller must take whatever action is necessary to
    376 *        prevent any other updates to @head. In principle, it is possible
    377 *        to modify the list as soon as sync() begins execution.
    378 *        If this sort of thing becomes necessary, an alternative version
    379 *        based on call_rcu() could be created. But only if -really-
    380 *        needed -- there is no shortage of RCU API members.
    381 */
    382 static inline void list_splice_init_rcu(struct list_head *list,
    383                                         struct list_head *head,
    384                                         void (*sync)(void))
    385 {
    386         struct list_head *first = list->next;
    387         struct list_head *last = list->prev;
    388         struct list_head *at = head->next;
    389
    390         if (list_empty(head))
    391                 return;
    392
    393         /* "first" and "last" tracking list, so initialize it. */
    394
    395         INIT_LIST_HEAD(list);
    396
    397         /*
    398          * At this point, the list body still points to the source list.
    399          * Wait for any readers to finish using the list before splicing
    400          * the list body into the new list. Any new readers will see
    401          * an empty list.
    402          */
    403
    404         sync();
    405
    406         /*
    407          * Readers are finished with the source list, so perform splice.
    408          * The order is important if the new list is global and accessible
    409          * to concurrent RCU readers. Note that RCU readers are not
    410          * permitted to traverse the prev pointers without excluding
    411          * this function.
    412          */
    413
    414         last->next = at;
    415         smp_wmb();
    416         head->next = first;
    417         first->prev = head;
    418         at->prev = last;
    419 }
    420
    421 /**
    422 * list_entry - get the struct for this entry
    423 * @ptr:        the &struct list_head pointer.
    424 * @type:        the type of the struct this is embedded in.
    425 * @member:        the name of the list_struct within the struct.
    426 */
    427 #define list_entry(ptr, type, member)
    428         container_of(ptr, type, member)
    429
    430 /**
    431 * list_first_entry - get the first element from a list
    432 * @ptr:        the list head to take the element from.
    433 * @type:        the type of the struct this is embedded in.
    434 * @member:        the name of the list_struct within the struct.
    435 *
    436 * Note, that list is expected to be not empty.
    437 */
    438 #define list_first_entry(ptr, type, member)
    439         list_entry((ptr)->next, type, member)
    440
    441 /**
    442 * list_for_each        -        iterate over a list
    443 * @pos:        the &struct list_head to use as a loop cursor.
    444 * @head:        the head for your list.
    445 */
    446 #define list_for_each(pos, head)
    447         for (pos = (head)->next; prefetch(pos->next), pos != (head);
    448         pos = pos->next)
    449
    450 /**
    451 * __list_for_each        -        iterate over a list
    452 * @pos:        the &struct list_head to use as a loop cursor.
    453 * @head:        the head for your list.
    454 *
    455 * This variant differs from list_for_each() in that it's the
    456 * simplest possible list iteration code, no prefetching is done.
    457 * Use this for code that knows the list to be very short (empty
    458 * or 1 entry) most of the time.
    459 */
    460 #define __list_for_each(pos, head)
    461         for (pos = (head)->next; pos != (head); pos = pos->next)
    462
    463 /**
    464 * list_for_each_prev        -        iterate over a list backwards
    465 * @pos:        the &struct list_head to use as a loop cursor.
    466 * @head:        the head for your list.
    467 */
    468 #define list_for_each_prev(pos, head)
    469         for (pos = (head)->prev; prefetch(pos->prev), pos != (head);
    470         pos = pos->prev)
    471
    472 /**
    473 * list_for_each_safe - iterate over a list safe against removal of list entry
    474 * @pos:        the &struct list_head to use as a loop cursor.
    475 * @n:                another &struct list_head to use as temporary storage
    476 * @head:        the head for your list.
    477 */
    478 #define list_for_each_safe(pos, n, head)
    479         for (pos = (head)->next, n = pos->next; pos != (head);
    480                 pos = n, n = pos->next)
    481
    482 /**
    483 * list_for_each_entry        -        iterate over list of given type
    484 * @pos:        the type * to use as a loop cursor.
    485 * @head:        the head for your list.
    486 * @member:        the name of the list_struct within the struct.
    487 */
    488 #define list_for_each_entry(pos, head, member)                                
    489         for (pos = list_entry((head)->next, typeof(*pos), member);        
    490          prefetch(pos->member.next), &pos->member != (head);         
    491          pos = list_entry(pos->member.next, typeof(*pos), member))
    492
    493 /**
    494 * list_for_each_entry_reverse - iterate backwards over list of given type.
    495 * @pos:        the type * to use as a loop cursor.
    496 * @head:        the head for your list.
    497 * @member:        the name of the list_struct within the struct.
    498 */
    499 #define list_for_each_entry_reverse(pos, head, member)                        
    500         for (pos = list_entry((head)->prev, typeof(*pos), member);        
    501          prefetch(pos->member.prev), &pos->member != (head);         
    502          pos = list_entry(pos->member.prev, typeof(*pos), member))
    503
    504 /**
    505 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
    506 * @pos:        the type * to use as a start point
    507 * @head:        the head of the list
    508 * @member:        the name of the list_struct within the struct.
    509 *
    510 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
    511 */
    512 #define list_prepare_entry(pos, head, member)
    513         ((pos) ? : list_entry(head, typeof(*pos), member))
    514
    515 /**
    516 * list_for_each_entry_continue - continue iteration over list of given type
    517 * @pos:        the type * to use as a loop cursor.
    518 * @head:        the head for your list.
    519 * @member:        the name of the list_struct within the struct.
    520 *
    521 * Continue to iterate over list of given type, continuing after
    522 * the current position.
    523 */
    524 #define list_for_each_entry_continue(pos, head, member)                 
    525         for (pos = list_entry(pos->member.next, typeof(*pos), member);        
    526          prefetch(pos->member.next), &pos->member != (head);        
    527          pos = list_entry(pos->member.next, typeof(*pos), member))
    528
    529 /**
    530 * list_for_each_entry_from - iterate over list of given type from the current point
    531 * @pos:        the type * to use as a loop cursor.
    532 * @head:        the head for your list.
    533 * @member:        the name of the list_struct within the struct.
    534 *
    535 * Iterate over list of given type, continuing from current position.
    536 */
    537 #define list_for_each_entry_from(pos, head, member)                         
    538         for (; prefetch(pos->member.next), &pos->member != (head);        
    539          pos = list_entry(pos->member.next, typeof(*pos), member))
    540
    541 /**
    542 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
    543 * @pos:        the type * to use as a loop cursor.
    544 * @n:                another type * to use as temporary storage
    545 * @head:        the head for your list.
    546 * @member:        the name of the list_struct within the struct.
    547 */
    548 #define list_for_each_entry_safe(pos, n, head, member)                        
    549         for (pos = list_entry((head)->next, typeof(*pos), member),        
    550                 n = list_entry(pos->member.next, typeof(*pos), member);        
    551          &pos->member != (head);                                         
    552          pos = n, n = list_entry(n->member.next, typeof(*n), member))
    553
    554 /**
    555 * list_for_each_entry_safe_continue
    556 * @pos:        the type * to use as a loop cursor.
    557 * @n:                another type * to use as temporary storage
    558 * @head:        the head for your list.
    559 * @member:        the name of the list_struct within the struct.
    560 *
    561 * Iterate over list of given type, continuing after current point,
    562 * safe against removal of list entry.
    563 */
    564 #define list_for_each_entry_safe_continue(pos, n, head, member)                 
    565         for (pos = list_entry(pos->member.next, typeof(*pos), member),                 
    566                 n = list_entry(pos->member.next, typeof(*pos), member);                
    567          &pos->member != (head);                                                
    568          pos = n, n = list_entry(n->member.next, typeof(*n), member))
    569
    570 /**
    571 * list_for_each_entry_safe_from
    572 * @pos:        the type * to use as a loop cursor.
    573 * @n:                another type * to use as temporary storage
    574 * @head:        the head for your list.
    575 * @member:        the name of the list_struct within the struct.
    576 *
    577 * Iterate over list of given type from current point, safe against
    578 * removal of list entry.
    579 */
    580 #define list_for_each_entry_safe_from(pos, n, head, member)                         
    581         for (n = list_entry(pos->member.next, typeof(*pos), member);                
    582          &pos->member != (head);                                                
    583          pos = n, n = list_entry(n->member.next, typeof(*n), member))
    584
    585 /**
    586 * list_for_each_entry_safe_reverse
    587 * @pos:        the type * to use as a loop cursor.
    588 * @n:                another type * to use as temporary storage
    589 * @head:        the head for your list.
    590 * @member:        the name of the list_struct within the struct.
    591 *
    592 * Iterate backwards over list of given type, safe against removal
    593 * of list entry.
    594 */
    595 #define list_for_each_entry_safe_reverse(pos, n, head, member)                
    596         for (pos = list_entry((head)->prev, typeof(*pos), member),        
    597                 n = list_entry(pos->member.prev, typeof(*pos), member);        
    598          &pos->member != (head);                                         
    599          pos = n, n = list_entry(n->member.prev, typeof(*n), member))
    600
    601 /**
    602 * list_for_each_rcu        -        iterate over an rcu-protected list
    603 * @pos:        the &struct list_head to use as a loop cursor.
    604 * @head:        the head for your list.
    605 *
    606 * This list-traversal primitive may safely run concurrently with
    607 * the _rcu list-mutation primitives such as list_add_rcu()
    608 * as long as the traversal is guarded by rcu_read_lock().
    609 */
    610 #define list_for_each_rcu(pos, head)
    611         for (pos = (head)->next;
    612                 prefetch(rcu_dereference(pos)->next), pos != (head);
    613         pos = pos->next)
    614
    615 #define __list_for_each_rcu(pos, head)
    616         for (pos = (head)->next;
    617                 rcu_dereference(pos) != (head);
    618         pos = pos->next)
    619
    620 /**
    621 * list_for_each_safe_rcu
    622 * @pos:        the &struct list_head to use as a loop cursor.
    623 * @n:                another &struct list_head to use as temporary storage
    624 * @head:        the head for your list.
    625 *
    626 * Iterate over an rcu-protected list, safe against removal of list entry.
    627 *
    628 * This list-traversal primitive may safely run concurrently with
    629 * the _rcu list-mutation primitives such as list_add_rcu()
    630 * as long as the traversal is guarded by rcu_read_lock().
    631 */
    632 #define list_for_each_safe_rcu(pos, n, head)
    633         for (pos = (head)->next;
    634                 n = rcu_dereference(pos)->next, pos != (head);
    635                 pos = n)
    636
    637 /**
    638 * list_for_each_entry_rcu        -        iterate over rcu list of given type
    639 * @pos:        the type * to use as a loop cursor.
    640 * @head:        the head for your list.
    641 * @member:        the name of the list_struct within the struct.
    642 *
    643 * This list-traversal primitive may safely run concurrently with
    644 * the _rcu list-mutation primitives such as list_add_rcu()
    645 * as long as the traversal is guarded by rcu_read_lock().
    646 */
    647 #define list_for_each_entry_rcu(pos, head, member)
    648         for (pos = list_entry((head)->next, typeof(*pos), member);
    649                 prefetch(rcu_dereference(pos)->member.next),
    650                         &pos->member != (head);
    651                 pos = list_entry(pos->member.next, typeof(*pos), member))
    652
    653
    654 /**
    655 * list_for_each_continue_rcu
    656 * @pos:        the &struct list_head to use as a loop cursor.
    657 * @head:        the head for your list.
    658 *
    659 * Iterate over an rcu-protected list, continuing after current point.
    660 *
    661 * This list-traversal primitive may safely run concurrently with
    662 * the _rcu list-mutation primitives such as list_add_rcu()
    663 * as long as the traversal is guarded by rcu_read_lock().
    664 */
    665 #define list_for_each_continue_rcu(pos, head)
    666         for ((pos) = (pos)->next;
    667                 prefetch(rcu_dereference((pos))->next), (pos) != (head);
    668         (pos) = (pos)->next)
    669
    670 /*
    671 * Double linked lists with a single pointer list head.
    672 * Mostly useful for hash tables where the two pointer list head is
    673 * too wasteful.
    674 * You lose the ability to access the tail in O(1).
    675 */
    676
    677 struct hlist_head {
    678         struct hlist_node *first;
    679 };
    680
    681 struct hlist_node {
    682         struct hlist_node *next, **pprev;
    683 };
    684
    685 #define HLIST_HEAD_INIT { .first = NULL }
    686 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
    687 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
    688 static inline void INIT_HLIST_NODE(struct hlist_node *h)
    689 {
    690         h->next = NULL;
    691         h->pprev = NULL;
    692 }
    693
    694 static inline int hlist_unhashed(const struct hlist_node *h)
    695 {
    696         return !h->pprev;
    697 }
    698
    699 static inline int hlist_empty(const struct hlist_head *h)
    700 {
    701         return !h->first;
    702 }
    703
    704 static inline void __hlist_del(struct hlist_node *n)
    705 {
    706         struct hlist_node *next = n->next;
    707         struct hlist_node **pprev = n->pprev;
    708         *pprev = next;
    709         if (next)
    710                 next->pprev = pprev;
    711 }
    712
    713 static inline void hlist_del(struct hlist_node *n)
    714 {
    715         __hlist_del(n);
    716         n->next = LIST_POISON1;
    717         n->pprev = LIST_POISON2;
    718 }
    719
    720 /**
    721 * hlist_del_rcu - deletes entry from hash list without re-initialization
    722 * @n: the element to delete from the hash list.
    723 *
    724 * Note: list_unhashed() on entry does not return true after this,
    725 * the entry is in an undefined state. It is useful for RCU based
    726 * lockfree traversal.
    727 *
    728 * In particular, it means that we can not poison the forward
    729 * pointers that may still be used for walking the hash list.
    730 *
    731 * The caller must take whatever precautions are necessary
    732 * (such as holding appropriate locks) to avoid racing
    733 * with another list-mutation primitive, such as hlist_add_head_rcu()
    734 * or hlist_del_rcu(), running on this same list.
    735 * However, it is perfectly legal to run concurrently with
    736 * the _rcu list-traversal primitives, such as
    737 * hlist_for_each_entry().
    738 */
    739 static inline void hlist_del_rcu(struct hlist_node *n)
    740 {
    741         __hlist_del(n);
    742         n->pprev = LIST_POISON2;
    743 }
    744
    745 static inline void hlist_del_init(struct hlist_node *n)
    746 {
    747         if (!hlist_unhashed(n)) {
    748                 __hlist_del(n);
    749                 INIT_HLIST_NODE(n);
    750         }
    751 }
    752
    753 /**
    754 * hlist_replace_rcu - replace old entry by new one
    755 * @old : the element to be replaced
    756 * @new : the new element to insert
    757 *
    758 * The @old entry will be replaced with the @new entry atomically.
    759 */
    760 static inline void hlist_replace_rcu(struct hlist_node *old,
    761                                         struct hlist_node *new)
    762 {
    763         struct hlist_node *next = old->next;
    764
    765         new->next = next;
    766         new->pprev = old->pprev;
    767         smp_wmb();
    768         if (next)
    769                 new->next->pprev = &new->next;
    770         *new->pprev = new;
    771         old->
    pprev = LIST_POISON2;

    772 }
    773
    774 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
    775 {
    776         struct hlist_node *first = h->first;
    777         n->next = first;
    778         if (first)
    779                 first->pprev = &n->next;
    780         h->first = n;
    781         n->pprev = &h->first;
    782 }
    783
    784
    785 /**
    786 * hlist_add_head_rcu
    787 * @n: the element to add to the hash list.
    788 * @h: the list to add to.
    789 *
    790 * Description:
    791 * Adds the specified element to the specified hlist,
    792 * while permitting racing traversals.
    793 *
    794 * The caller must take whatever precautions are necessary
    795 * (such as holding appropriate locks) to avoid racing
    796 * with another list-mutation primitive, such as hlist_add_head_rcu()
    797 * or hlist_del_rcu(), running on this same list.
    798 * However, it is perfectly legal to run concurrently with
    799 * the _rcu list-traversal primitives, such as
    800 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
    801 * problems on Alpha CPUs. Regardless of the type of CPU, the
    802 * list-traversal primitive must be guarded by rcu_read_lock().
    803 */
    804 static inline void hlist_add_head_rcu(struct hlist_node *n,
    805                                         struct hlist_head *h)
    806 {
    807         struct hlist_node *first = h->first;
    808         n->next = first;
    809         n->pprev = &h->first;
    810         smp_wmb();
    811         if (first)
    812                 first->pprev = &n->next;
    813         h->first = n;
    814 }
    815
    816 /* next must be != NULL */
    817 static inline void hlist_add_before(struct hlist_node *n,
    818                                         struct hlist_node *next)
    819 {
    820         n->pprev = next->pprev;
    821         n->next = next;
    822         next->pprev = &n->next;
    823         *(n->pprev) = n;
    824 }
    825
    826 static inline void hlist_add_after(struct hlist_node *n,
    827                                         struct hlist_node *next)
    828 {
    829         next->next = n->next;
    830         n->next = next;
    831         next->pprev = &n->next;
    832
    833         if(next->next)
    834                 next->next->pprev = &next->next;
    835 }
    836
    837 /**
    838 * hlist_add_before_rcu
    839 * @n: the new element to add to the hash list.
    840 * @next: the existing element to add the new element before.
    841 *
    842 * Description:
    843 * Adds the specified element to the specified hlist
    844 * before the specified node while permitting racing traversals.
    845 *
    846 * The caller must take whatever precautions are necessary
    847 * (such as holding appropriate locks) to avoid racing
    848 * with another list-mutation primitive, such as hlist_add_head_rcu()
    849 * or hlist_del_rcu(), running on this same list.
    850 * However, it is perfectly legal to run concurrently with
    851 * the _rcu list-traversal primitives, such as
    852 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
    853 * problems on Alpha CPUs.
    854 */
    855 static inline void hlist_add_before_rcu(struct hlist_node *n,
    856                                         struct hlist_node *next)
    857 {
    858         n->pprev = next->pprev;
    859         n->next = next;
    860         smp_wmb();
    861         next->pprev = &n->next;
    862         *(n->pprev) = n;
    863 }
    864
    865 /**
    866 * hlist_add_after_rcu
    867 * @prev: the existing element to add the new element after.
    868 * @n: the new element to add to the hash list.
    869 *
    870 * Description:
    871 * Adds the specified element to the specified hlist
    872 * after the specified node while permitting racing traversals.
    873 *
    874 * The caller must take whatever precautions are necessary
    875 * (such as holding appropriate locks) to avoid racing
    876 * with another list-mutation primitive, such as hlist_add_head_rcu()
    877 * or hlist_del_rcu(), running on this same list.
    878 * However, it is perfectly legal to run concurrently with
    879 * the _rcu list-traversal primitives, such as
    880 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
    881 * problems on Alpha CPUs.
    882 */
    883 static inline void hlist_add_after_rcu(struct hlist_node *prev,
    884                                  struct hlist_node *n)
    885 {
    886         n->next = prev->next;
    887         n->pprev = &prev->next;
    888         smp_wmb();
    889         prev->next = n;
    890         if (n->next)
    891                 n->next->pprev = &n->next;
    892 }
    893
    894 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
    895
    896 #define hlist_for_each(pos, head)
    897         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; });
    898          pos = pos->next)
    899
    900 #define hlist_for_each_safe(pos, n, head)
    901         for (pos = (head)->first; pos && ({ n = pos->next; 1; });
    902          pos = n)
    903
    904 /**
    905 * hlist_for_each_entry        - iterate over list of given type
    906 * @tpos:        the type * to use as a loop cursor.
    907 * @pos:        the &struct hlist_node to use as a loop cursor.
    908 * @head:        the head for your list.
    909 * @member:        the name of the hlist_node within the struct.
    910 */
    911 #define hlist_for_each_entry(tpos, pos, head, member)                        
    912         for (pos = (head)->first;                                        
    913          pos && ({ prefetch(pos->next); 1;}) &&                        
    914                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
    915          pos = pos->next)
    916
    917 /**
    918 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
    919 * @tpos:        the type * to use as a loop cursor.
    920 * @pos:        the &struct hlist_node to use as a loop cursor.
    921 * @member:        the name of the hlist_node within the struct.
    922 */
    923 #define hlist_for_each_entry_continue(tpos, pos, member)                
    924         for (pos = (pos)->next;                                                
    925          pos && ({ prefetch(pos->next); 1;}) &&                        
    926                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
    927          pos = pos->next)
    928
    929 /**
    930 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
    931 * @tpos:        the type * to use as a loop cursor.
    932 * @pos:        the &struct hlist_node to use as a loop cursor.
    933 * @member:        the name of the hlist_node within the struct.
    934 */
    935 #define hlist_for_each_entry_from(tpos, pos, member)                        
    936         for (; pos && ({ prefetch(pos->next); 1;}) &&                        
    937                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
    938          pos = pos->next)
    939
    940 /**
    941 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
    942 * @tpos:        the type * to use as a loop cursor.
    943 * @pos:        the &struct hlist_node to use as a loop cursor.
    944 * @n:                another &struct hlist_node to use as temporary storage
    945 * @head:        the head for your list.
    946 * @member:        the name of the hlist_node within the struct.
    947 */
    948 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)                 
    949         for (pos = (head)->first;                                        
    950          pos && ({ n = pos->next; 1; }) &&                                 
    951                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
    952          pos = n)
    953
    954 /**
    955 * hlist_for_each_entry_rcu - iterate over rcu list of given type
    956 * @tpos:        the type * to use as a loop cursor.
    957 * @pos:        the &struct hlist_node to use as a loop cursor.
    958 * @head:        the head for your list.
    959 * @member:        the name of the hlist_node within the struct.
    960 *
    961 * This list-traversal primitive may safely run concurrently with
    962 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
    963 * as long as the traversal is guarded by rcu_read_lock().
    964 */
    965 #define hlist_for_each_entry_rcu(tpos, pos, head, member)                
    966         for (pos = (head)->first;                                        
    967          rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) &&        
    968                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
    969          pos = pos->next)
    970
    971 #else
    972 #warning "don't include kernel headers in userspace"
    973 #endif /* __KERNEL__ */
    974 #endif

       

       

    `。

  • 相关阅读:
    iOS-mac下 svn的使用
    iOS-Model-View-ViewModel & ReactiveCocoa
    iOS-通过实现一个TableView来理解iOS UI编程(转)
    iOS-细说 iOS 消息推送(转)
    【原创】O2O,你真的知道怎么玩吗?
    谷歌提升虚拟运营商逼格,传统电信运营商的好日子到头?
    【技术贴】大型发布会现场的WiFi网络应该如何搭建?
    千万别把WIFI玩坏了!关于WIFI的新鲜玩法和商业模式探讨
    【深度分享】千团大战:看今天商业WiFi乱局及其破解之道
    (转)迎接 Entity Framework 7
  • 原文地址:https://www.cnblogs.com/lilto/p/11876385.html
Copyright © 2011-2022 走看看