1 1.7 riastrad /* $NetBSD: llist.h,v 1.7 2022/04/09 23:43:55 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.1 riastrad #ifndef _LINUX_LLIST_H_ 33 1.1 riastrad #define _LINUX_LLIST_H_ 34 1.1 riastrad 35 1.1 riastrad #include <sys/atomic.h> 36 1.1 riastrad #include <sys/null.h> 37 1.1 riastrad 38 1.1 riastrad struct llist_head { 39 1.4 riastrad struct llist_node *first; 40 1.1 riastrad }; 41 1.1 riastrad 42 1.1 riastrad struct llist_node { 43 1.3 riastrad struct llist_node *next; 44 1.1 riastrad }; 45 1.1 riastrad 46 1.1 riastrad static inline void 47 1.1 riastrad init_llist_head(struct llist_head *head) 48 1.1 riastrad { 49 1.1 riastrad 50 1.4 riastrad head->first = NULL; 51 1.1 riastrad } 52 1.1 riastrad 53 1.1 riastrad #define llist_entry(NODE, TYPE, FIELD) container_of(NODE, TYPE, FIELD) 54 1.1 riastrad 55 1.1 riastrad static inline bool 56 1.1 riastrad llist_empty(struct llist_head *head) 57 1.1 riastrad { 58 1.1 riastrad bool empty; 59 1.1 riastrad 60 1.4 riastrad empty = (atomic_load_acquire(&head->first) == NULL); 61 1.1 riastrad 62 1.1 riastrad return empty; 63 1.1 riastrad } 64 1.1 riastrad 65 1.1 riastrad static inline bool 66 1.1 riastrad llist_add(struct llist_node *node, struct llist_head *head) 67 1.1 riastrad { 68 1.1 riastrad struct llist_node *first; 69 1.1 riastrad 70 1.1 riastrad do { 71 1.4 riastrad first = head->first; 72 1.3 riastrad node->next = first; 73 1.7 riastrad membar_release(); 74 1.4 riastrad } while (atomic_cas_ptr(&head->first, first, node) != first); 75 1.1 riastrad 76 1.1 riastrad return first == NULL; 77 1.1 riastrad } 78 1.1 riastrad 79 1.6 riastrad static inline bool 80 1.6 riastrad llist_add_batch(struct llist_node *first, struct llist_node *last, 81 1.6 riastrad struct llist_head *head) 82 1.6 riastrad { 83 1.6 riastrad struct llist_node *next; 84 1.6 riastrad 85 1.6 riastrad do { 86 1.6 riastrad next = atomic_load_consume(&head->first); 87 1.6 riastrad last->next = next; 88 1.6 riastrad } while (atomic_cas_ptr(&head->first, next, first) != next); 89 1.6 riastrad 90 1.6 riastrad return next == NULL; 91 1.6 riastrad } 92 1.6 riastrad 93 1.1 riastrad static inline struct llist_node * 94 1.1 riastrad llist_del_all(struct llist_head *head) 95 1.1 riastrad { 96 1.1 riastrad struct llist_node *first; 97 1.1 riastrad 98 1.4 riastrad first = atomic_swap_ptr(&head->first, NULL); 99 1.7 riastrad membar_datadep_consumer(); 100 1.1 riastrad 101 1.1 riastrad return first; 102 1.1 riastrad } 103 1.1 riastrad 104 1.1 riastrad static inline struct llist_node * 105 1.1 riastrad llist_del_first(struct llist_head *head) 106 1.1 riastrad { 107 1.1 riastrad struct llist_node *first; 108 1.1 riastrad 109 1.1 riastrad do { 110 1.4 riastrad first = atomic_load_consume(&head->first); 111 1.4 riastrad if (first == NULL) 112 1.4 riastrad return NULL; 113 1.4 riastrad } while (atomic_cas_ptr(&head->first, first, first->next) 114 1.1 riastrad != first); 115 1.1 riastrad 116 1.1 riastrad return first; 117 1.1 riastrad } 118 1.1 riastrad 119 1.4 riastrad #define _llist_next(ENTRY, FIELD) \ 120 1.4 riastrad ({ \ 121 1.4 riastrad __typeof__((ENTRY)->FIELD.next) _NODE = \ 122 1.4 riastrad atomic_load_consume(&(ENTRY)->FIELD.next); \ 123 1.4 riastrad (_NODE == NULL ? NULL : \ 124 1.4 riastrad llist_entry(_NODE, __typeof__(*(ENTRY)), FIELD)); \ 125 1.4 riastrad }) 126 1.4 riastrad 127 1.5 riastrad #define llist_for_each_safe(NODE, TMP, HEAD) \ 128 1.5 riastrad for ((NODE) = (HEAD); \ 129 1.5 riastrad (NODE) && ((TMP) = (NODE)->next, 1); \ 130 1.5 riastrad (NODE) = (TMP)) 131 1.5 riastrad 132 1.4 riastrad #define llist_for_each_entry(ENTRY, NODE, FIELD) \ 133 1.4 riastrad for ((ENTRY) = ((NODE) == NULL ? NULL : \ 134 1.4 riastrad (membar_datadep_consumer(), \ 135 1.4 riastrad llist_entry(NODE, typeof(*(ENTRY)), FIELD))); \ 136 1.4 riastrad (ENTRY) != NULL; \ 137 1.4 riastrad (ENTRY) = _llist_next(ENTRY, FIELD)) 138 1.4 riastrad 139 1.1 riastrad #define llist_for_each_entry_safe(ENTRY, TMP, NODE, FIELD) \ 140 1.1 riastrad for ((ENTRY) = ((NODE) == NULL ? NULL : \ 141 1.1 riastrad (membar_datadep_consumer(), \ 142 1.4 riastrad llist_entry(NODE, typeof(*(ENTRY)), FIELD))); \ 143 1.4 riastrad ((ENTRY) == NULL ? 0 : \ 144 1.4 riastrad ((TMP) = _llist_next(ENTRY, FIELD), 1)); \ 145 1.4 riastrad (ENTRY) = (TMP)) 146 1.1 riastrad 147 1.1 riastrad #endif /* _LINUX_LLIST_H_ */ 148