| 1 | |
| 2 | |
| 3 | * Lock-less NULL terminated single linked list
|
| 4 | *
|
| 5 | * The basic atomic operation of this list is cmpxchg on long. On
|
| 6 | * architectures that don't have NMI-safe cmpxchg implementation, the
|
| 7 | * list can NOT be used in NMI handlers. So code that uses the list in
|
| 8 | * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
|
| 9 | *
|
| 10 | * Copyright 2010,2011 Intel Corp.
|
| 11 | * Author: Huang Ying <[email protected]>
|
| 12 |
|
| 13 | #include <linux/kernel.h>
|
| 14 | #include <linux/export.h>
|
| 15 | #include <linux/llist.h>
|
| 16 |
|
| 17 | |
| 18 | * llist_del_first - delete the first entry of lock-less list
|
| 19 | * @head: the head for your lock-less list
|
| 20 | *
|
| 21 | * If list is empty, return NULL, otherwise, return the first entry
|
| 22 | * deleted, this is the newest added one.
|
| 23 | *
|
| 24 | * Only one llist_del_first user can be used simultaneously with
|
| 25 | * multiple llist_add users without lock. Because otherwise
|
| 26 | * llist_del_first, llist_add, llist_add (or llist_del_all, llist_add,
|
| 27 | * llist_add) sequence in another user may change @head->first->next,
|
| 28 | * but keep @head->first. If multiple consumers are needed, please
|
| 29 | * use llist_del_all or use lock between consumers.
|
| 30 |
|
| 31 | struct llist_node *llist_del_first(struct llist_head *head)
|
| 32 | {
|
| 33 | struct llist_node *entry, *next;
|
| 34 |
|
| 35 | entry = smp_load_acquire(&head->first);
|
| 36 | do {
|
| 37 | if (entry == NULL)
|
| 38 | return NULL;
|
| 39 | next = READ_ONCE(entry->next);
|
| 40 | } while (!try_cmpxchg(&head->first, &entry, next));
|
| 41 |
|
| 42 | return entry;
|
| 43 | }
|
| 44 | EXPORT_SYMBOL_GPL(llist_del_first);
|
| 45 |
|
| 46 | |
| 47 | * llist_del_first_this - delete given entry of lock-less list if it is first
|
| 48 | * @head: the head for your lock-less list
|
| 49 | * @this: a list entry.
|
| 50 | *
|
| 51 | * If head of the list is given entry, delete and return %true else
|
| 52 | * return %false.
|
| 53 | *
|
| 54 | * Multiple callers can safely call this concurrently with multiple
|
| 55 | * llist_add() callers, providing all the callers offer a different @this.
|
| 56 |
|
| 57 | bool llist_del_first_this(struct llist_head *head,
|
| 58 | struct llist_node *this)
|
| 59 | {
|
| 60 | struct llist_node *entry, *next;
|
| 61 |
|
| 62 |
|
| 63 | entry = smp_load_acquire(&head->first);
|
| 64 | do {
|
| 65 | if (entry != this)
|
| 66 | return false;
|
| 67 | next = READ_ONCE(entry->next);
|
| 68 | } while (!try_cmpxchg(&head->first, &entry, next));
|
| 69 |
|
| 70 | return true;
|
| 71 | }
|
| 72 | EXPORT_SYMBOL_GPL(llist_del_first_this);
|
| 73 |
|
| 74 | |
| 75 | * llist_reverse_order - reverse order of a llist chain
|
| 76 | * @head: first item of the list to be reversed
|
| 77 | *
|
| 78 | * Reverse the order of a chain of llist entries and return the
|
| 79 | * new first entry.
|
| 80 |
|
| 81 | struct llist_node *llist_reverse_order(struct llist_node *head)
|
| 82 | {
|
| 83 | struct llist_node *new_head = NULL;
|
| 84 |
|
| 85 | while (head) {
|
| 86 | struct llist_node *tmp = head;
|
| 87 | head = head->next;
|
| 88 | tmp->next = new_head;
|
| 89 | new_head = tmp;
|
| 90 | }
|
| 91 |
|
| 92 | return new_head;
|
| 93 | }
|
| 94 | EXPORT_SYMBOL_GPL(llist_reverse_order);
|