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 | c32a0e5 | 2019-10-04 18:01:39 +0200 | [diff] [blame] | 107 | /* adds the contents of a list whose first element is <old> and last one is |
| 108 | * <old->prev> at the end of another list <new>. The old list DOES NOT have |
| 109 | * any head here. |
| 110 | */ |
| 111 | #define LIST_SPLICE_END_DETACHED(new, old) do { \ |
| 112 | typeof(new) __t; \ |
| 113 | (new)->p->n = (old); \ |
| 114 | (old)->p->n = (new); \ |
| 115 | __t = (old)->p; \ |
| 116 | (old)->p = (new)->p; \ |
| 117 | (new)->p = __t; \ |
| 118 | } while (0) |
| 119 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 120 | /* removes an element from a list and returns it */ |
| 121 | #define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); }) |
| 122 | |
Willy Tarreau | c5bd311 | 2019-03-06 19:32:11 +0100 | [diff] [blame] | 123 | /* removes an element from a list, initializes it and returns it. |
| 124 | * This is faster than LIST_DEL+LIST_INIT as we avoid reloading the pointers. |
| 125 | */ |
| 126 | #define LIST_DEL_INIT(el) ({ \ |
| 127 | typeof(el) __ret = (el); \ |
| 128 | typeof(__ret->n) __n = __ret->n; \ |
| 129 | typeof(__ret->p) __p = __ret->p; \ |
| 130 | __n->p = __p; __p->n = __n; \ |
| 131 | __ret->n = __ret->p = __ret; \ |
| 132 | __ret; \ |
| 133 | }) |
| 134 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 135 | /* returns a pointer of type <pt> to a structure containing a list head called |
| 136 | * <el> at address <lh>. Note that <lh> can be the result of a function or macro |
| 137 | * since it's used only once. |
| 138 | * Example: LIST_ELEM(cur_node->args.next, struct node *, args) |
| 139 | */ |
| 140 | #define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el))) |
| 141 | |
| 142 | /* checks if the list head <lh> is empty or not */ |
| 143 | #define LIST_ISEMPTY(lh) ((lh)->n == (lh)) |
| 144 | |
Willy Tarreau | 42ccb5a | 2019-05-13 17:48:46 +0200 | [diff] [blame] | 145 | /* checks if the list element <el> was added to a list or not. This only |
| 146 | * works when detached elements are reinitialized (using LIST_DEL_INIT) |
| 147 | */ |
| 148 | #define LIST_ADDED(el) ((el)->n != (el)) |
| 149 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 150 | /* returns a pointer of type <pt> to a structure following the element |
| 151 | * which contains list head <lh>, which is known as element <el> in |
| 152 | * struct pt. |
| 153 | * Example: LIST_NEXT(args, struct node *, list) |
| 154 | */ |
| 155 | #define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el)) |
| 156 | |
| 157 | |
Joseph Herlant | 41abef7 | 2018-11-25 10:57:13 -0800 | [diff] [blame] | 158 | /* returns a pointer of type <pt> to a structure preceding the element |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 159 | * which contains list head <lh>, which is known as element <el> in |
| 160 | * struct pt. |
| 161 | */ |
Thierry FOURNIER | 1db9667 | 2015-11-03 19:17:37 +0100 | [diff] [blame] | 162 | #undef LIST_PREV |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 163 | #define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el)) |
| 164 | |
| 165 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 166 | * Simpler FOREACH_ITEM macro inspired from Linux sources. |
| 167 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 168 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 169 | * the list is passed in <list_head>. No temporary variable is needed. Note |
| 170 | * that <item> must not be modified during the loop. |
| 171 | * Example: list_for_each_entry(cur_acl, known_acl, list) { ... }; |
| 172 | */ |
| 173 | #define list_for_each_entry(item, list_head, member) \ |
| 174 | for (item = LIST_ELEM((list_head)->n, typeof(item), member); \ |
| 175 | &item->member != (list_head); \ |
| 176 | item = LIST_ELEM(item->member.n, typeof(item), member)) |
| 177 | |
| 178 | /* |
William Lallemand | 83215a4 | 2017-09-24 11:26:02 +0200 | [diff] [blame] | 179 | * Same as list_for_each_entry but starting from current point |
| 180 | * Iterates <item> through the list starting from <item> |
| 181 | * It's basically the same macro but without initializing item to the head of |
| 182 | * the list. |
| 183 | */ |
| 184 | #define list_for_each_entry_from(item, list_head, member) \ |
| 185 | for ( ; &item->member != (list_head); \ |
| 186 | item = LIST_ELEM(item->member.n, typeof(item), member)) |
| 187 | |
| 188 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 189 | * Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources. |
| 190 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 191 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 192 | * the list is passed in <list_head>. A temporary variable <back> of same type |
| 193 | * as <item> is needed so that <item> may safely be deleted if needed. |
| 194 | * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list) { ... }; |
| 195 | */ |
| 196 | #define list_for_each_entry_safe(item, back, list_head, member) \ |
| 197 | for (item = LIST_ELEM((list_head)->n, typeof(item), member), \ |
| 198 | back = LIST_ELEM(item->member.n, typeof(item), member); \ |
| 199 | &item->member != (list_head); \ |
| 200 | item = back, back = LIST_ELEM(back->member.n, typeof(back), member)) |
| 201 | |
| 202 | |
William Lallemand | 83215a4 | 2017-09-24 11:26:02 +0200 | [diff] [blame] | 203 | /* |
| 204 | * Same as list_for_each_entry_safe but starting from current point |
| 205 | * Iterates <item> through the list starting from <item> |
| 206 | * It's basically the same macro but without initializing item to the head of |
| 207 | * the list. |
| 208 | */ |
| 209 | #define list_for_each_entry_safe_from(item, back, list_head, member) \ |
| 210 | for (back = LIST_ELEM(item->member.n, typeof(item), member); \ |
| 211 | &item->member != (list_head); \ |
| 212 | item = back, back = LIST_ELEM(back->member.n, typeof(back), member)) |
| 213 | |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 214 | #include <common/hathreads.h> |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 215 | #define MT_LIST_BUSY ((struct mt_list *)1) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Locked version of list manipulation macros. |
| 219 | * 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] | 220 | * list is only used with the locked variants. |
| 221 | */ |
| 222 | |
| 223 | /* |
| 224 | * Add an item at the beginning of a list. |
| 225 | * Returns 1 if we added the item, 0 otherwise (because it was already in a |
| 226 | * list). |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 227 | */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 228 | #define MT_LIST_ADD(_lh, _el) \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 229 | ({ \ |
| 230 | int _ret = 0; \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 231 | struct mt_list *lh = (_lh), *el = (_el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 232 | do { \ |
| 233 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 234 | struct mt_list *n; \ |
| 235 | struct mt_list *p; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 236 | n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \ |
| 237 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 238 | continue; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 239 | p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \ |
| 240 | if (p == MT_LIST_BUSY) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 241 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 242 | __ha_barrier_store(); \ |
| 243 | continue; \ |
| 244 | } \ |
Olivier Houchard | cb22ad4 | 2019-09-20 14:44:22 +0200 | [diff] [blame] | 245 | if ((el)->next != (el) || (el)->prev != (el)) { \ |
| 246 | (n)->prev = p; \ |
| 247 | (lh)->next = n; \ |
| 248 | break; \ |
| 249 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 250 | (el)->next = n; \ |
| 251 | (el)->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 252 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 253 | n->prev = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 254 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 255 | p->next = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 256 | __ha_barrier_store(); \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 257 | _ret = 1; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 258 | break; \ |
| 259 | } \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 260 | } while (0); \ |
| 261 | (_ret); \ |
| 262 | }) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 263 | |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 264 | /* |
| 265 | * Add an item at the end of a list. |
| 266 | * Returns 1 if we added the item, 0 otherwise (because it was already in a |
| 267 | * list). |
| 268 | */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 269 | #define MT_LIST_ADDQ(_lh, _el) \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 270 | ({ \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 271 | int _ret = 0; \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 272 | struct mt_list *lh = (_lh), *el = (_el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 273 | do { \ |
| 274 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 275 | struct mt_list *n; \ |
| 276 | struct mt_list *p; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 277 | p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \ |
| 278 | if (p == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 279 | continue; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 280 | n = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY); \ |
| 281 | if (n == MT_LIST_BUSY) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 282 | (lh)->prev = p; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 283 | __ha_barrier_store(); \ |
| 284 | continue; \ |
| 285 | } \ |
Olivier Houchard | cb22ad4 | 2019-09-20 14:44:22 +0200 | [diff] [blame] | 286 | if ((el)->next != (el) || (el)->prev != (el)) { \ |
| 287 | p->next = n; \ |
| 288 | (lh)->prev = p; \ |
| 289 | break; \ |
| 290 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 291 | (el)->next = n; \ |
| 292 | (el)->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 293 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 294 | p->next = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 295 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 296 | n->prev = (el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 297 | __ha_barrier_store(); \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 298 | _ret = 1; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 299 | break; \ |
| 300 | } \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 301 | } while (0); \ |
| 302 | (_ret); \ |
| 303 | }) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 304 | |
Willy Tarreau | d7f2bbc | 2019-10-04 18:02:40 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Detach a list from its head. A pointer to the first element is returned |
| 307 | * and the list is closed. If the list was empty, NULL is returned. This may |
| 308 | * exclusively be used with lists modified by MT_LIST_ADD/MT_LIST_ADDQ. This |
| 309 | * is incompatible with MT_LIST_DEL run concurrently. |
Olivier Houchard | 2068ec4 | 2019-10-17 17:46:01 +0200 | [diff] [blame] | 310 | * If there's at least one element, the next of the last element will always |
| 311 | * be NULL. |
Willy Tarreau | d7f2bbc | 2019-10-04 18:02:40 +0200 | [diff] [blame] | 312 | */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 313 | #define MT_LIST_BEHEAD(_lh) ({ \ |
| 314 | struct mt_list *lh = (_lh); \ |
Willy Tarreau | d7f2bbc | 2019-10-04 18:02:40 +0200 | [diff] [blame] | 315 | struct mt_list *_n; \ |
| 316 | struct mt_list *_p; \ |
| 317 | while (1) { \ |
| 318 | _p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \ |
| 319 | if (_p == MT_LIST_BUSY) \ |
| 320 | continue; \ |
| 321 | if (_p == (lh)) { \ |
| 322 | (lh)->prev = _p; \ |
| 323 | _n = NULL; \ |
| 324 | break; \ |
| 325 | } \ |
| 326 | _n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \ |
| 327 | if (_n == MT_LIST_BUSY) { \ |
| 328 | (lh)->prev = _p; \ |
| 329 | __ha_barrier_store(); \ |
| 330 | continue; \ |
| 331 | } \ |
| 332 | if (_n == (lh)) { \ |
| 333 | (lh)->next = _n; \ |
| 334 | (lh)->prev = _p; \ |
| 335 | _n = NULL; \ |
| 336 | break; \ |
| 337 | } \ |
| 338 | (lh)->next = (lh); \ |
| 339 | (lh)->prev = (lh); \ |
| 340 | _n->prev = _p; \ |
Olivier Houchard | 2068ec4 | 2019-10-17 17:46:01 +0200 | [diff] [blame] | 341 | _p->next = NULL; \ |
Willy Tarreau | d7f2bbc | 2019-10-04 18:02:40 +0200 | [diff] [blame] | 342 | __ha_barrier_store(); \ |
| 343 | break; \ |
| 344 | } \ |
| 345 | (_n); \ |
| 346 | }) |
| 347 | |
| 348 | |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 349 | /* Remove an item from a list. |
| 350 | * Returns 1 if we removed the item, 0 otherwise (because it was in no list). |
| 351 | */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 352 | #define MT_LIST_DEL(_el) \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 353 | ({ \ |
| 354 | int _ret = 0; \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 355 | struct mt_list *el = (_el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 356 | do { \ |
| 357 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 358 | struct mt_list *n, *n2; \ |
| 359 | struct mt_list *p, *p2 = NULL; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 360 | n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \ |
| 361 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 362 | continue; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 363 | p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \ |
| 364 | if (p == MT_LIST_BUSY) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 365 | (el)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 366 | __ha_barrier_store(); \ |
| 367 | continue; \ |
| 368 | } \ |
| 369 | if (p != (el)) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 370 | p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 371 | if (p2 == MT_LIST_BUSY) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 372 | (el)->prev = p; \ |
| 373 | (el)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 374 | __ha_barrier_store(); \ |
| 375 | continue; \ |
| 376 | } \ |
| 377 | } \ |
| 378 | if (n != (el)) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 379 | n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 380 | if (n2 == MT_LIST_BUSY) { \ |
Olivier Houchard | db64489 | 2019-02-26 18:46:07 +0100 | [diff] [blame] | 381 | if (p2 != NULL) \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 382 | p->next = p2; \ |
| 383 | (el)->prev = p; \ |
| 384 | (el)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 385 | __ha_barrier_store(); \ |
| 386 | continue; \ |
| 387 | } \ |
| 388 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 389 | n->prev = p; \ |
| 390 | p->next = n; \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 391 | if (p != (el) && n != (el)) \ |
| 392 | _ret = 1; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 393 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 394 | (el)->prev = (el); \ |
| 395 | (el)->next = (el); \ |
Willy Tarreau | 4c747e8 | 2019-02-28 15:05:53 +0100 | [diff] [blame] | 396 | __ha_barrier_store(); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 397 | break; \ |
| 398 | } \ |
Olivier Houchard | 0cd6a97 | 2019-09-20 17:32:47 +0200 | [diff] [blame] | 399 | } while (0); \ |
| 400 | (_ret); \ |
| 401 | }) |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 402 | |
| 403 | |
| 404 | /* Remove the first element from the list, and return it */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 405 | #define MT_LIST_POP(_lh, pt, el) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 406 | ({ \ |
| 407 | void *_ret; \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 408 | struct mt_list *lh = (_lh); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 409 | while (1) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 410 | struct mt_list *n, *n2; \ |
| 411 | struct mt_list *p, *p2; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 412 | n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \ |
| 413 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 414 | continue; \ |
| 415 | if (n == (lh)) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 416 | (lh)->next = lh; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 417 | __ha_barrier_store(); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 418 | _ret = NULL; \ |
| 419 | break; \ |
| 420 | } \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 421 | p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \ |
| 422 | if (p == MT_LIST_BUSY) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 423 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 424 | __ha_barrier_store(); \ |
| 425 | continue; \ |
| 426 | } \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 427 | n2 = _HA_ATOMIC_XCHG(&n->next, MT_LIST_BUSY); \ |
| 428 | if (n2 == MT_LIST_BUSY) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 429 | n->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 430 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 431 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 432 | __ha_barrier_store(); \ |
| 433 | continue; \ |
| 434 | } \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 435 | p2 = _HA_ATOMIC_XCHG(&n2->prev, MT_LIST_BUSY); \ |
| 436 | if (p2 == MT_LIST_BUSY) { \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 437 | n->next = n2; \ |
| 438 | n->prev = p; \ |
Willy Tarreau | 690d2ad | 2019-02-28 11:14:22 +0100 | [diff] [blame] | 439 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 440 | (lh)->next = n; \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 441 | __ha_barrier_store(); \ |
| 442 | continue; \ |
| 443 | } \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 444 | (lh)->next = n2; \ |
| 445 | (n2)->prev = (lh); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 446 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 447 | (n)->prev = (n); \ |
| 448 | (n)->next = (n); \ |
Willy Tarreau | 4c747e8 | 2019-02-28 15:05:53 +0100 | [diff] [blame] | 449 | __ha_barrier_store(); \ |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 450 | _ret = MT_LIST_ELEM(n, pt, el); \ |
Olivier Houchard | a8434ec | 2019-01-18 17:26:26 +0100 | [diff] [blame] | 451 | break; \ |
| 452 | } \ |
| 453 | (_ret); \ |
| 454 | }) |
William Lallemand | 83215a4 | 2017-09-24 11:26:02 +0200 | [diff] [blame] | 455 | |
Olivier Houchard | 859dc80 | 2019-08-08 15:47:21 +0200 | [diff] [blame] | 456 | #define MT_LIST_HEAD(a) ((void *)(&(a))) |
| 457 | |
| 458 | #define MT_LIST_INIT(l) ((l)->next = (l)->prev = (l)) |
| 459 | |
| 460 | #define MT_LIST_HEAD_INIT(l) { &l, &l } |
| 461 | /* returns a pointer of type <pt> to a structure containing a list head called |
| 462 | * <el> at address <lh>. Note that <lh> can be the result of a function or macro |
| 463 | * since it's used only once. |
| 464 | * Example: MT_LIST_ELEM(cur_node->args.next, struct node *, args) |
| 465 | */ |
| 466 | #define MT_LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el))) |
| 467 | |
| 468 | /* checks if the list head <lh> is empty or not */ |
| 469 | #define MT_LIST_ISEMPTY(lh) ((lh)->next == (lh)) |
| 470 | |
| 471 | /* returns a pointer of type <pt> to a structure following the element |
| 472 | * which contains list head <lh>, which is known as element <el> in |
| 473 | * struct pt. |
| 474 | * Example: MT_LIST_NEXT(args, struct node *, list) |
| 475 | */ |
| 476 | #define MT_LIST_NEXT(lh, pt, el) (MT_LIST_ELEM((lh)->next, pt, el)) |
| 477 | |
| 478 | |
| 479 | /* returns a pointer of type <pt> to a structure preceding the element |
| 480 | * which contains list head <lh>, which is known as element <el> in |
| 481 | * struct pt. |
| 482 | */ |
| 483 | #undef MT_LIST_PREV |
| 484 | #define MT_LIST_PREV(lh, pt, el) (MT_LIST_ELEM((lh)->prev, pt, el)) |
| 485 | |
| 486 | /* checks if the list element <el> was added to a list or not. This only |
| 487 | * works when detached elements are reinitialized (using LIST_DEL_INIT) |
| 488 | */ |
| 489 | #define MT_LIST_ADDED(el) ((el)->next != (el)) |
| 490 | |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 491 | /* Lock an element in the list, to be sure it won't be removed. |
| 492 | * It needs to be synchronized somehow to be sure it's not removed |
| 493 | * from the list in the meanwhile. |
| 494 | * This returns a struct mt_list, that will be needed at unlock time. |
| 495 | */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 496 | #define MT_LIST_LOCK_ELT(_el) \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 497 | ({ \ |
| 498 | struct mt_list ret; \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 499 | struct mt_liet *el = (_el); \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 500 | while (1) { \ |
| 501 | struct mt_list *n, *n2; \ |
| 502 | struct mt_list *p, *p2 = NULL; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 503 | n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \ |
| 504 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 505 | continue; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 506 | p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \ |
| 507 | if (p == MT_LIST_BUSY) { \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 508 | (el)->next = n; \ |
| 509 | __ha_barrier_store(); \ |
| 510 | continue; \ |
| 511 | } \ |
| 512 | if (p != (el)) { \ |
| 513 | p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 514 | if (p2 == MT_LIST_BUSY) { \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 515 | (el)->prev = p; \ |
| 516 | (el)->next = n; \ |
| 517 | __ha_barrier_store(); \ |
| 518 | continue; \ |
| 519 | } \ |
| 520 | } \ |
| 521 | if (n != (el)) { \ |
| 522 | n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 523 | if (n2 == MT_LIST_BUSY) { \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 524 | if (p2 != NULL) \ |
| 525 | p->next = p2; \ |
| 526 | (el)->prev = p; \ |
| 527 | (el)->next = n; \ |
| 528 | __ha_barrier_store(); \ |
| 529 | continue; \ |
| 530 | } \ |
| 531 | } \ |
| 532 | ret.next = n; \ |
| 533 | ret.prev = p; \ |
| 534 | break; \ |
| 535 | } \ |
| 536 | ret; \ |
| 537 | }) |
| 538 | |
| 539 | /* Unlock an element previously locked by MT_LIST_LOCK_ELT. "np" is the |
| 540 | * struct mt_list returned by MT_LIST_LOCK_ELT(). |
| 541 | */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 542 | #define MT_LIST_UNLOCK_ELT(_el, np) \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 543 | do { \ |
| 544 | struct mt_list *n = (np).next, *p = (np).prev; \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 545 | struct mt_list *el = (_el); \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 546 | (el)->next = n; \ |
| 547 | (el)->prev = p; \ |
| 548 | if (n != (el)) \ |
| 549 | n->prev = (el); \ |
| 550 | if (p != (el)) \ |
| 551 | p->next = (el); \ |
| 552 | } while (0) |
| 553 | |
| 554 | /* Internal macroes for the foreach macroes */ |
| 555 | #define _MT_LIST_UNLOCK_NEXT(el, np) \ |
| 556 | do { \ |
| 557 | struct mt_list *n = (np); \ |
| 558 | (el)->next = n; \ |
| 559 | if (n != (el)) \ |
| 560 | n->prev = (el); \ |
| 561 | } while (0) |
| 562 | |
| 563 | /* Internal macroes for the foreach macroes */ |
| 564 | #define _MT_LIST_UNLOCK_PREV(el, np) \ |
| 565 | do { \ |
| 566 | struct mt_list *p = (np); \ |
| 567 | (el)->prev = p; \ |
| 568 | if (p != (el)) \ |
| 569 | p->next = (el); \ |
| 570 | } while (0) |
| 571 | |
| 572 | #define _MT_LIST_LOCK_NEXT(el) \ |
| 573 | ({ \ |
| 574 | struct mt_list *n = NULL; \ |
| 575 | while (1) { \ |
| 576 | struct mt_list *n2; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 577 | n = _HA_ATOMIC_XCHG(&((el)->next), MT_LIST_BUSY); \ |
| 578 | if (n == MT_LIST_BUSY) \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 579 | continue; \ |
| 580 | if (n != (el)) { \ |
| 581 | n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 582 | if (n2 == MT_LIST_BUSY) { \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 583 | (el)->next = n; \ |
| 584 | __ha_barrier_store(); \ |
| 585 | continue; \ |
| 586 | } \ |
| 587 | } \ |
| 588 | break; \ |
| 589 | } \ |
| 590 | n; \ |
| 591 | }) |
| 592 | |
| 593 | #define _MT_LIST_LOCK_PREV(el) \ |
| 594 | ({ \ |
| 595 | struct mt_list *p = NULL; \ |
| 596 | while (1) { \ |
| 597 | struct mt_list *p2; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 598 | p = _HA_ATOMIC_XCHG(&((el)->prev), MT_LIST_BUSY); \ |
| 599 | if (p == MT_LIST_BUSY) \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 600 | continue; \ |
| 601 | if (p != (el)) { \ |
| 602 | p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 603 | if (p2 == MT_LIST_BUSY) { \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 604 | (el)->prev = p; \ |
| 605 | __ha_barrier_store(); \ |
| 606 | continue; \ |
| 607 | } \ |
| 608 | } \ |
| 609 | break; \ |
| 610 | } \ |
| 611 | p; \ |
| 612 | }) |
| 613 | |
| 614 | #define _MT_LIST_RELINK_DELETED(elt2) \ |
| 615 | do { \ |
| 616 | struct mt_list *n = elt2.next, *p = elt2.prev; \ |
| 617 | n->prev = p; \ |
| 618 | p->next = n; \ |
| 619 | } while (0); |
| 620 | |
| 621 | /* Equivalent of MT_LIST_DEL(), to be used when parsing the list with mt_list_entry_for_each_safe(). |
| 622 | * It should be the element currently parsed (tmpelt1) |
| 623 | */ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 624 | #define MT_LIST_DEL_SAFE(_el) \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 625 | do { \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 626 | struct mt_list *el = (_el); \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 627 | (el)->prev = (el); \ |
| 628 | (el)->next = (el); \ |
| 629 | (el) = NULL; \ |
| 630 | } while (0) |
| 631 | |
| 632 | /* Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources. |
| 633 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 634 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 635 | * the list is passed in <list_head>. A temporary variable <back> of same type |
| 636 | * as <item> is needed so that <item> may safely be deleted if needed. |
| 637 | * tmpelt1 is a temporary struct mt_list *, and tmpelt2 is a temporary |
| 638 | * struct mt_list, used internally, both are needed for MT_LIST_DEL_SAFE. |
| 639 | * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list, elt1, elt2) |
| 640 | * { ... }; |
| 641 | * If you want to remove the current element, please use MT_LIST_DEL_SAFE. |
| 642 | */ |
| 643 | #define mt_list_for_each_entry_safe(item, list_head, member, tmpelt, tmpelt2) \ |
| 644 | for ((tmpelt) = NULL; (tmpelt) != MT_LIST_BUSY; ({ \ |
| 645 | if (tmpelt) { \ |
| 646 | if (tmpelt2.prev) \ |
| 647 | MT_LIST_UNLOCK_ELT(tmpelt, tmpelt2); \ |
| 648 | else \ |
| 649 | _MT_LIST_UNLOCK_NEXT(tmpelt, tmpelt2.next); \ |
| 650 | } else \ |
| 651 | _MT_LIST_RELINK_DELETED(tmpelt2); \ |
| 652 | (tmpelt) = MT_LIST_BUSY; \ |
Olivier Houchard | 804ef24 | 2019-10-11 16:57:43 +0200 | [diff] [blame] | 653 | })) \ |
Olivier Houchard | 74715da | 2019-10-11 16:55:11 +0200 | [diff] [blame] | 654 | for ((tmpelt) = (list_head), (tmpelt2).prev = NULL, (tmpelt2).next = _MT_LIST_LOCK_NEXT(tmpelt); ({ \ |
Olivier Houchard | 5e9b92c | 2019-08-12 14:10:12 +0200 | [diff] [blame] | 655 | (item) = MT_LIST_ELEM((tmpelt2.next), typeof(item), member); \ |
| 656 | if (&item->member != (list_head)) { \ |
| 657 | if (tmpelt2.prev != &item->member) \ |
| 658 | tmpelt2.next = _MT_LIST_LOCK_NEXT(&item->member); \ |
| 659 | else \ |
| 660 | tmpelt2.next = tmpelt; \ |
| 661 | if (tmpelt != NULL) { \ |
| 662 | if (tmpelt2.prev) \ |
| 663 | _MT_LIST_UNLOCK_PREV(tmpelt, tmpelt2.prev); \ |
| 664 | tmpelt2.prev = tmpelt; \ |
| 665 | } \ |
| 666 | (tmpelt) = &item->member; \ |
| 667 | } \ |
| 668 | }), \ |
| 669 | &item->member != (list_head);) |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 670 | #endif /* _COMMON_MINI_CLIST_H */ |