willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 3dd717c | 2014-12-23 13:58:43 +0100 | [diff] [blame] | 2 | * include/common/mini-clist.h |
| 3 | * Circular list manipulation macros and structures. |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 4 | * |
Willy Tarreau | 3dd717c | 2014-12-23 13:58:43 +0100 | [diff] [blame] | 5 | * Copyright (C) 2002-2014 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_MINI_CLIST_H |
| 23 | #define _COMMON_MINI_CLIST_H |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
| 26 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 27 | /* these are circular or bidirectionnal lists only. Each list pointer points to |
| 28 | * another list pointer in a structure, and not the structure itself. The |
| 29 | * pointer to the next element MUST be the first one so that the list is easily |
| 30 | * cast as a single linked list or pointer. |
| 31 | */ |
| 32 | struct list { |
| 33 | struct list *n; /* next */ |
| 34 | struct list *p; /* prev */ |
| 35 | }; |
| 36 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 37 | /* This is similar to struct list, but we want to be sure the compiler will |
| 38 | * yell at you if you use macroes for one when you're using the other. You have |
| 39 | * to expicitely cast if that's really what you want to do. |
| 40 | */ |
| 41 | struct mt_list { |
| 42 | struct mt_list *next; |
| 43 | struct mt_list *prev; |
| 44 | }; |
| 45 | |
| 46 | |
Willy Tarreau | bc04ce7 | 2008-12-07 20:00:15 +0100 | [diff] [blame] | 47 | /* a back-ref is a pointer to a target list entry. It is used to detect when an |
| 48 | * element being deleted is currently being tracked by another user. The best |
| 49 | * example is a user dumping the session table. The table does not fit in the |
| 50 | * output buffer so we have to set a mark on a session and go on later. But if |
| 51 | * that marked session gets deleted, we don't want the user's pointer to go in |
| 52 | * the wild. So we can simply link this user's request to the list of this |
| 53 | * session's users, and put a pointer to the list element in ref, that will be |
| 54 | * used as the mark for next iteration. |
| 55 | */ |
| 56 | struct bref { |
| 57 | struct list users; |
| 58 | struct list *ref; /* pointer to the target's list entry */ |
| 59 | }; |
| 60 | |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 61 | /* a word list is a generic list with a pointer to a string in each element. */ |
| 62 | struct wordlist { |
| 63 | struct list list; |
| 64 | char *s; |
| 65 | }; |
| 66 | |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 67 | /* this is the same as above with an additional pointer to a condition. */ |
| 68 | struct cond_wordlist { |
| 69 | struct list list; |
| 70 | void *cond; |
| 71 | char *s; |
| 72 | }; |
| 73 | |
Willy Tarreau | bd578bb | 2007-10-28 11:41:06 +0100 | [diff] [blame] | 74 | /* First undefine some macros which happen to also be defined on OpenBSD, |
| 75 | * in sys/queue.h, used by sys/event.h |
| 76 | */ |
| 77 | #undef LIST_HEAD |
| 78 | #undef LIST_INIT |
| 79 | #undef LIST_NEXT |
| 80 | |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 81 | /* ILH = Initialized List Head : used to prevent gcc from moving an empty |
| 82 | * list to BSS. Some older version tend to trim all the array and cause |
| 83 | * corruption. |
| 84 | */ |
| 85 | #define ILH { .n = (struct list *)1, .p = (struct list *)2 } |
| 86 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 88 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 89 | #define LIST_INIT(l) ((l)->n = (l)->p = (l)) |
| 90 | |
Willy Tarreau | 2b1dccd | 2007-05-07 00:18:32 +0200 | [diff] [blame] | 91 | #define LIST_HEAD_INIT(l) { &l, &l } |
| 92 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 93 | /* adds an element at the beginning of a list ; returns the element */ |
| 94 | #define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); }) |
| 95 | |
| 96 | /* adds an element at the end of a list ; returns the element */ |
| 97 | #define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); }) |
| 98 | |
Willy Tarreau | 9bead8c | 2019-08-16 11:27:50 +0200 | [diff] [blame] | 99 | /* adds the contents of a list <old> at the beginning of another list <new>. The old list head remains untouched. */ |
| 100 | #define LIST_SPLICE(new, old) do { \ |
| 101 | if (!LIST_ISEMPTY(old)) { \ |
| 102 | (old)->p->n = (new)->n; (old)->n->p = (new); \ |
| 103 | (new)->n->p = (old)->p; (new)->n = (old)->n; \ |
| 104 | } \ |
| 105 | } while (0) |
| 106 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 107 | /* removes an element from a list and returns it */ |
| 108 | #define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); }) |
| 109 | |
Willy Tarreau | c5bd311 | 2019-03-06 19:32:11 +0100 | [diff] [blame] | 110 | /* removes an element from a list, initializes it and returns it. |
| 111 | * This is faster than LIST_DEL+LIST_INIT as we avoid reloading the pointers. |
| 112 | */ |
| 113 | #define LIST_DEL_INIT(el) ({ \ |
| 114 | typeof(el) __ret = (el); \ |
| 115 | typeof(__ret->n) __n = __ret->n; \ |
| 116 | typeof(__ret->p) __p = __ret->p; \ |
| 117 | __n->p = __p; __p->n = __n; \ |
| 118 | __ret->n = __ret->p = __ret; \ |
| 119 | __ret; \ |
| 120 | }) |
| 121 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 122 | /* returns a pointer of type <pt> to a structure containing a list head called |
| 123 | * <el> at address <lh>. Note that <lh> can be the result of a function or macro |
| 124 | * since it's used only once. |
| 125 | * Example: LIST_ELEM(cur_node->args.next, struct node *, args) |
| 126 | */ |
| 127 | #define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el))) |
| 128 | |
| 129 | /* checks if the list head <lh> is empty or not */ |
| 130 | #define LIST_ISEMPTY(lh) ((lh)->n == (lh)) |
| 131 | |
Willy Tarreau | 42ccb5a | 2019-05-13 17:48:46 +0200 | [diff] [blame] | 132 | /* checks if the list element <el> was added to a list or not. This only |
| 133 | * works when detached elements are reinitialized (using LIST_DEL_INIT) |
| 134 | */ |
| 135 | #define LIST_ADDED(el) ((el)->n != (el)) |
| 136 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 137 | /* returns a pointer of type <pt> to a structure following the element |
| 138 | * which contains list head <lh>, which is known as element <el> in |
| 139 | * struct pt. |
| 140 | * Example: LIST_NEXT(args, struct node *, list) |
| 141 | */ |
| 142 | #define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el)) |
| 143 | |
| 144 | |
Joseph Herlant | 41abef7 | 2018-11-25 10:57:13 -0800 | [diff] [blame] | 145 | /* returns a pointer of type <pt> to a structure preceding the element |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 146 | * which contains list head <lh>, which is known as element <el> in |
| 147 | * struct pt. |
| 148 | */ |
Thierry FOURNIER | 1db9667 | 2015-11-03 19:17:37 +0100 | [diff] [blame] | 149 | #undef LIST_PREV |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 150 | #define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el)) |
| 151 | |
| 152 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 153 | * Simpler FOREACH_ITEM macro inspired from Linux sources. |
| 154 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 155 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 156 | * the list is passed in <list_head>. No temporary variable is needed. Note |
| 157 | * that <item> must not be modified during the loop. |
| 158 | * Example: list_for_each_entry(cur_acl, known_acl, list) { ... }; |
| 159 | */ |
| 160 | #define list_for_each_entry(item, list_head, member) \ |
| 161 | for (item = LIST_ELEM((list_head)->n, typeof(item), member); \ |
| 162 | &item->member != (list_head); \ |
| 163 | item = LIST_ELEM(item->member.n, typeof(item), member)) |
| 164 | |
| 165 | /* |
William Lallemand | 83215a4 | 2017-09-24 11:26:02 +0200 | [diff] [blame] | 166 | * Same as list_for_each_entry but starting from current point |
| 167 | * Iterates <item> through the list starting from <item> |
| 168 | * It's basically the same macro but without initializing item to the head of |
| 169 | * the list. |
| 170 | */ |
| 171 | #define list_for_each_entry_from(item, list_head, member) \ |
| 172 | for ( ; &item->member != (list_head); \ |
| 173 | item = LIST_ELEM(item->member.n, typeof(item), member)) |
| 174 | |
| 175 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 176 | * Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources. |
| 177 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 178 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 179 | * the list is passed in <list_head>. A temporary variable <back> of same type |
| 180 | * as <item> is needed so that <item> may safely be deleted if needed. |
| 181 | * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list) { ... }; |
| 182 | */ |
| 183 | #define list_for_each_entry_safe(item, back, list_head, member) \ |
| 184 | for (item = LIST_ELEM((list_head)->n, typeof(item), member), \ |
| 185 | back = LIST_ELEM(item->member.n, typeof(item), member); \ |
| 186 | &item->member != (list_head); \ |
| 187 | item = back, back = LIST_ELEM(back->member.n, typeof(back), member)) |
| 188 | |
| 189 | |
William Lallemand | 83215a4 | 2017-09-24 11:26:02 +0200 | [diff] [blame] | 190 | /* |
| 191 | * Same as list_for_each_entry_safe but starting from current point |
| 192 | * Iterates <item> through the list starting from <item> |
| 193 | * It's basically the same macro but without initializing item to the head of |
| 194 | * the list. |
| 195 | */ |
| 196 | #define list_for_each_entry_safe_from(item, back, list_head, member) \ |
| 197 | for (back = LIST_ELEM(item->member.n, typeof(item), member); \ |
| 198 | &item->member != (list_head); \ |
| 199 | item = back, back = LIST_ELEM(back->member.n, typeof(back), member)) |
| 200 | |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 201 | #include <common/hathreads.h> |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 202 | #define MT_LIST_BUSY ((struct mt_list *)1) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Locked version of list manipulation macros. |
| 206 | * It is OK to use those concurrently from multiple threads, as long as the |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 207 | * list is only used with the locked variants. |
| 208 | */ |
| 209 | |
| 210 | /* |
| 211 | * Add an item at the beginning of a list. |
| 212 | * Returns 1 if we added the item, 0 otherwise (because it was already in a |
| 213 | * list). |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 214 | */ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 215 | #define MT_LIST_ADD(lh, el) \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 216 | ({ \ |
| 217 | int _ret = 0; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 218 | do { \ |
| 219 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 220 | struct mt_list *n; \ |
| 221 | struct mt_list *p; \ |
| 222 | n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \ |
| 223 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 224 | continue; \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 225 | p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \ |
| 226 | if (p == MT_LIST_BUSY) { \ |
| 227 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 228 | __ha_barrier_store(); \ |
| 229 | continue; \ |
| 230 | } \ |
Olivier Houchard | cb22ad4 | 2019-09-20 14:44:22 +0200 | [diff] [blame] | 231 | if ((el)->next != (el) || (el)->prev != (el)) { \ |
| 232 | (n)->prev = p; \ |
| 233 | (lh)->next = n; \ |
| 234 | break; \ |
| 235 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 236 | (el)->next = n; \ |
| 237 | (el)->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 238 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 239 | n->prev = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 240 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 241 | p->next = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 242 | __ha_barrier_store(); \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 243 | _ret = 1; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 244 | break; \ |
| 245 | } \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 246 | } while (0); \ |
| 247 | (_ret); \ |
| 248 | }) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 249 | |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 250 | /* |
| 251 | * Add an item at the end of a list. |
| 252 | * Returns 1 if we added the item, 0 otherwise (because it was already in a |
| 253 | * list). |
| 254 | */ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 255 | #define MT_LIST_ADDQ(lh, el) \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 256 | ({ \ |
| 257 | int _ret = 0; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 258 | do { \ |
| 259 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 260 | struct mt_list *n; \ |
| 261 | struct mt_list *p; \ |
| 262 | p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \ |
| 263 | if (p == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 264 | continue; \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 265 | n = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY); \ |
| 266 | if (n == MT_LIST_BUSY) { \ |
| 267 | (lh)->prev = p; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 268 | __ha_barrier_store(); \ |
| 269 | continue; \ |
| 270 | } \ |
Olivier Houchard | cb22ad4 | 2019-09-20 14:44:22 +0200 | [diff] [blame] | 271 | if ((el)->next != (el) || (el)->prev != (el)) { \ |
| 272 | p->next = n; \ |
| 273 | (lh)->prev = p; \ |
| 274 | break; \ |
| 275 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 276 | (el)->next = n; \ |
| 277 | (el)->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 278 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 279 | p->next = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 280 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 281 | n->prev = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 282 | __ha_barrier_store(); \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 283 | _ret = 1; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 284 | break; \ |
| 285 | } \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 286 | } while (0); \ |
| 287 | (_ret); \ |
| 288 | }) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 289 | |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 290 | /* Remove an item from a list. |
| 291 | * Returns 1 if we removed the item, 0 otherwise (because it was in no list). |
| 292 | */ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 293 | #define MT_LIST_DEL(el) \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 294 | ({ \ |
| 295 | int _ret = 0; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 296 | do { \ |
| 297 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 298 | struct mt_list *n, *n2; \ |
| 299 | struct mt_list *p, *p2 = NULL; \ |
| 300 | n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \ |
| 301 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 302 | continue; \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 303 | p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \ |
| 304 | if (p == MT_LIST_BUSY) { \ |
| 305 | (el)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 306 | __ha_barrier_store(); \ |
| 307 | continue; \ |
| 308 | } \ |
| 309 | if (p != (el)) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 310 | p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\ |
| 311 | if (p2 == MT_LIST_BUSY) { \ |
| 312 | (el)->prev = p; \ |
| 313 | (el)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 314 | __ha_barrier_store(); \ |
| 315 | continue; \ |
| 316 | } \ |
| 317 | } \ |
| 318 | if (n != (el)) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 319 | n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\ |
| 320 | if (n2 == MT_LIST_BUSY) { \ |
Olivier Houchard | db64489 | 2019-02-26 18:46:07 +0100 | [diff] [blame] | 321 | if (p2 != NULL) \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 322 | p->next = p2; \ |
| 323 | (el)->prev = p; \ |
| 324 | (el)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 325 | __ha_barrier_store(); \ |
| 326 | continue; \ |
| 327 | } \ |
| 328 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 329 | n->prev = p; \ |
| 330 | p->next = n; \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 331 | if (p != (el) && n != (el)) \ |
| 332 | _ret = 1; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 333 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 334 | (el)->prev = (el); \ |
| 335 | (el)->next = (el); \ |
Willy Tarreau | 4c747e8 | 2019-02-28 15:05:53 +0100 | [diff] [blame] | 336 | __ha_barrier_store(); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 337 | break; \ |
| 338 | } \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 339 | } while (0); \ |
| 340 | (_ret); \ |
| 341 | }) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 342 | |
| 343 | |
| 344 | /* Remove the first element from the list, and return it */ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 345 | #define MT_LIST_POP(lh, pt, el) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 346 | ({ \ |
| 347 | void *_ret; \ |
| 348 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 349 | struct mt_list *n, *n2; \ |
| 350 | struct mt_list *p, *p2; \ |
| 351 | n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \ |
| 352 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 353 | continue; \ |
| 354 | if (n == (lh)) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 355 | (lh)->next = lh; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 356 | __ha_barrier_store(); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 357 | _ret = NULL; \ |
| 358 | break; \ |
| 359 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 360 | p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \ |
| 361 | if (p == MT_LIST_BUSY) { \ |
| 362 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 363 | __ha_barrier_store(); \ |
| 364 | continue; \ |
| 365 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 366 | n2 = _HA_ATOMIC_XCHG(&n->next, MT_LIST_BUSY); \ |
| 367 | if (n2 == MT_LIST_BUSY) { \ |
| 368 | n->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 369 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 370 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 371 | __ha_barrier_store(); \ |
| 372 | continue; \ |
| 373 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 374 | p2 = _HA_ATOMIC_XCHG(&n2->prev, MT_LIST_BUSY); \ |
| 375 | if (p2 == MT_LIST_BUSY) { \ |
| 376 | n->next = n2; \ |
| 377 | n->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 378 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 379 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 380 | __ha_barrier_store(); \ |
| 381 | continue; \ |
| 382 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 383 | (lh)->next = n2; \ |
| 384 | (n2)->prev = (lh); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 385 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 386 | (n)->prev = (n); \ |
| 387 | (n)->next = (n); \ |
Willy Tarreau | 4c747e8 | 2019-02-28 15:05:53 +0100 | [diff] [blame] | 388 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 389 | _ret = MT_LIST_ELEM(n, pt, el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 390 | break; \ |
| 391 | } \ |
| 392 | (_ret); \ |
| 393 | }) |
William Lallemand | 83215a4 | 2017-09-24 11:26:02 +0200 | [diff] [blame] | 394 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 395 | #define MT_LIST_HEAD(a) ((void *)(&(a))) |
| 396 | |
| 397 | #define MT_LIST_INIT(l) ((l)->next = (l)->prev = (l)) |
| 398 | |
| 399 | #define MT_LIST_HEAD_INIT(l) { &l, &l } |
| 400 | /* returns a pointer of type <pt> to a structure containing a list head called |
| 401 | * <el> at address <lh>. Note that <lh> can be the result of a function or macro |
| 402 | * since it's used only once. |
| 403 | * Example: MT_LIST_ELEM(cur_node->args.next, struct node *, args) |
| 404 | */ |
| 405 | #define MT_LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el))) |
| 406 | |
| 407 | /* checks if the list head <lh> is empty or not */ |
| 408 | #define MT_LIST_ISEMPTY(lh) ((lh)->next == (lh)) |
| 409 | |
| 410 | /* returns a pointer of type <pt> to a structure following the element |
| 411 | * which contains list head <lh>, which is known as element <el> in |
| 412 | * struct pt. |
| 413 | * Example: MT_LIST_NEXT(args, struct node *, list) |
| 414 | */ |
| 415 | #define MT_LIST_NEXT(lh, pt, el) (MT_LIST_ELEM((lh)->next, pt, el)) |
| 416 | |
| 417 | |
| 418 | /* returns a pointer of type <pt> to a structure preceding the element |
| 419 | * which contains list head <lh>, which is known as element <el> in |
| 420 | * struct pt. |
| 421 | */ |
| 422 | #undef MT_LIST_PREV |
| 423 | #define MT_LIST_PREV(lh, pt, el) (MT_LIST_ELEM((lh)->prev, pt, el)) |
| 424 | |
| 425 | /* checks if the list element <el> was added to a list or not. This only |
| 426 | * works when detached elements are reinitialized (using LIST_DEL_INIT) |
| 427 | */ |
| 428 | #define MT_LIST_ADDED(el) ((el)->next != (el)) |
| 429 | |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 430 | /* Lock an element in the list, to be sure it won't be removed. |
| 431 | * It needs to be synchronized somehow to be sure it's not removed |
| 432 | * from the list in the meanwhile. |
| 433 | * This returns a struct mt_list, that will be needed at unlock time. |
| 434 | */ |
| 435 | #define MT_LIST_LOCK_ELT(el) \ |
| 436 | ({ \ |
| 437 | struct mt_list ret; \ |
| 438 | while (1) { \ |
| 439 | struct mt_list *n, *n2; \ |
| 440 | struct mt_list *p, *p2 = NULL; \ |
| 441 | n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \ |
| 442 | if (n == MT_LIST_BUSY) \ |
| 443 | continue; \ |
| 444 | p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \ |
| 445 | if (p == MT_LIST_BUSY) { \ |
| 446 | (el)->next = n; \ |
| 447 | __ha_barrier_store(); \ |
| 448 | continue; \ |
| 449 | } \ |
| 450 | if (p != (el)) { \ |
| 451 | p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\ |
| 452 | if (p2 == MT_LIST_BUSY) { \ |
| 453 | (el)->prev = p; \ |
| 454 | (el)->next = n; \ |
| 455 | __ha_barrier_store(); \ |
| 456 | continue; \ |
| 457 | } \ |
| 458 | } \ |
| 459 | if (n != (el)) { \ |
| 460 | n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\ |
| 461 | if (n2 == MT_LIST_BUSY) { \ |
| 462 | if (p2 != NULL) \ |
| 463 | p->next = p2; \ |
| 464 | (el)->prev = p; \ |
| 465 | (el)->next = n; \ |
| 466 | __ha_barrier_store(); \ |
| 467 | continue; \ |
| 468 | } \ |
| 469 | } \ |
| 470 | ret.next = n; \ |
| 471 | ret.prev = p; \ |
| 472 | break; \ |
| 473 | } \ |
| 474 | ret; \ |
| 475 | }) |
| 476 | |
| 477 | /* Unlock an element previously locked by MT_LIST_LOCK_ELT. "np" is the |
| 478 | * struct mt_list returned by MT_LIST_LOCK_ELT(). |
| 479 | */ |
| 480 | #define MT_LIST_UNLOCK_ELT(el, np) \ |
| 481 | do { \ |
| 482 | struct mt_list *n = (np).next, *p = (np).prev; \ |
| 483 | (el)->next = n; \ |
| 484 | (el)->prev = p; \ |
| 485 | if (n != (el)) \ |
| 486 | n->prev = (el); \ |
| 487 | if (p != (el)) \ |
| 488 | p->next = (el); \ |
| 489 | } while (0) |
| 490 | |
| 491 | /* Internal macroes for the foreach macroes */ |
| 492 | #define _MT_LIST_UNLOCK_NEXT(el, np) \ |
| 493 | do { \ |
| 494 | struct mt_list *n = (np); \ |
| 495 | (el)->next = n; \ |
| 496 | if (n != (el)) \ |
| 497 | n->prev = (el); \ |
| 498 | } while (0) |
| 499 | |
| 500 | /* Internal macroes for the foreach macroes */ |
| 501 | #define _MT_LIST_UNLOCK_PREV(el, np) \ |
| 502 | do { \ |
| 503 | struct mt_list *p = (np); \ |
| 504 | (el)->prev = p; \ |
| 505 | if (p != (el)) \ |
| 506 | p->next = (el); \ |
| 507 | } while (0) |
| 508 | |
| 509 | #define _MT_LIST_LOCK_NEXT(el) \ |
| 510 | ({ \ |
| 511 | struct mt_list *n = NULL; \ |
| 512 | while (1) { \ |
| 513 | struct mt_list *n2; \ |
| 514 | n = _HA_ATOMIC_XCHG(&((el)->next), MT_LIST_BUSY); \ |
| 515 | if (n == MT_LIST_BUSY) \ |
| 516 | continue; \ |
| 517 | if (n != (el)) { \ |
| 518 | n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\ |
| 519 | if (n2 == MT_LIST_BUSY) { \ |
| 520 | (el)->next = n; \ |
| 521 | __ha_barrier_store(); \ |
| 522 | continue; \ |
| 523 | } \ |
| 524 | } \ |
| 525 | break; \ |
| 526 | } \ |
| 527 | n; \ |
| 528 | }) |
| 529 | |
| 530 | #define _MT_LIST_LOCK_PREV(el) \ |
| 531 | ({ \ |
| 532 | struct mt_list *p = NULL; \ |
| 533 | while (1) { \ |
| 534 | struct mt_list *p2; \ |
| 535 | p = _HA_ATOMIC_XCHG(&((el)->prev), MT_LIST_BUSY); \ |
| 536 | if (p == MT_LIST_BUSY) \ |
| 537 | continue; \ |
| 538 | if (p != (el)) { \ |
| 539 | p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\ |
| 540 | if (p2 == MT_LIST_BUSY) { \ |
| 541 | (el)->prev = p; \ |
| 542 | __ha_barrier_store(); \ |
| 543 | continue; \ |
| 544 | } \ |
| 545 | } \ |
| 546 | break; \ |
| 547 | } \ |
| 548 | p; \ |
| 549 | }) |
| 550 | |
| 551 | #define _MT_LIST_RELINK_DELETED(elt2) \ |
| 552 | do { \ |
| 553 | struct mt_list *n = elt2.next, *p = elt2.prev; \ |
| 554 | n->prev = p; \ |
| 555 | p->next = n; \ |
| 556 | } while (0); |
| 557 | |
| 558 | /* Equivalent of MT_LIST_DEL(), to be used when parsing the list with mt_list_entry_for_each_safe(). |
| 559 | * It should be the element currently parsed (tmpelt1) |
| 560 | */ |
| 561 | #define MT_LIST_DEL_SAFE(el) \ |
| 562 | do { \ |
| 563 | (el)->prev = (el); \ |
| 564 | (el)->next = (el); \ |
| 565 | (el) = NULL; \ |
| 566 | } while (0) |
| 567 | |
| 568 | /* Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources. |
| 569 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 570 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 571 | * the list is passed in <list_head>. A temporary variable <back> of same type |
| 572 | * as <item> is needed so that <item> may safely be deleted if needed. |
| 573 | * tmpelt1 is a temporary struct mt_list *, and tmpelt2 is a temporary |
| 574 | * struct mt_list, used internally, both are needed for MT_LIST_DEL_SAFE. |
| 575 | * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list, elt1, elt2) |
| 576 | * { ... }; |
| 577 | * If you want to remove the current element, please use MT_LIST_DEL_SAFE. |
| 578 | */ |
| 579 | #define mt_list_for_each_entry_safe(item, list_head, member, tmpelt, tmpelt2) \ |
| 580 | for ((tmpelt) = NULL; (tmpelt) != MT_LIST_BUSY; ({ \ |
| 581 | if (tmpelt) { \ |
| 582 | if (tmpelt2.prev) \ |
| 583 | MT_LIST_UNLOCK_ELT(tmpelt, tmpelt2); \ |
| 584 | else \ |
| 585 | _MT_LIST_UNLOCK_NEXT(tmpelt, tmpelt2.next); \ |
| 586 | } else \ |
| 587 | _MT_LIST_RELINK_DELETED(tmpelt2); \ |
| 588 | (tmpelt) = MT_LIST_BUSY; \ |
| 589 | })) \ |
| 590 | for ((tmpelt) = (list_head), (tmpelt2).prev = NULL, (tmpelt2).next = _MT_LIST_LOCK_NEXT(list_head); ({ \ |
| 591 | (item) = MT_LIST_ELEM((tmpelt2.next), typeof(item), member); \ |
| 592 | if (&item->member != (list_head)) { \ |
| 593 | if (tmpelt2.prev != &item->member) \ |
| 594 | tmpelt2.next = _MT_LIST_LOCK_NEXT(&item->member); \ |
| 595 | else \ |
| 596 | tmpelt2.next = tmpelt; \ |
| 597 | if (tmpelt != NULL) { \ |
| 598 | if (tmpelt2.prev) \ |
| 599 | _MT_LIST_UNLOCK_PREV(tmpelt, tmpelt2.prev); \ |
| 600 | tmpelt2.prev = tmpelt; \ |
| 601 | } \ |
| 602 | (tmpelt) = &item->member; \ |
| 603 | } \ |
| 604 | }), \ |
| 605 | &item->member != (list_head);) |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 606 | #endif /* _COMMON_MINI_CLIST_H */ |