blob: bb794e3d6b8b2bdd6f3334ec51819ca5d38366b9 [file] [log] [blame]
willy tarreau80862a32006-04-12 19:15:57 +02001/*
Willy Tarreau3dd717c2014-12-23 13:58:43 +01002 * include/common/mini-clist.h
3 * Circular list manipulation macros and structures.
willy tarreau80862a32006-04-12 19:15:57 +02004 *
Willy Tarreau3dd717c2014-12-23 13:58:43 +01005 * 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 tarreau80862a32006-04-12 19:15:57 +020020 */
21
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_MINI_CLIST_H
23#define _COMMON_MINI_CLIST_H
willy tarreau80862a32006-04-12 19:15:57 +020024
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
26
willy tarreau80862a32006-04-12 19:15:57 +020027/* 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 */
32struct list {
33 struct list *n; /* next */
34 struct list *p; /* prev */
35};
36
Olivier Houchard859dc802019-08-08 15:47:21 +020037/* 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 */
41struct mt_list {
42 struct mt_list *next;
43 struct mt_list *prev;
44};
45
46
Willy Tarreaubc04ce72008-12-07 20:00:15 +010047/* 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 */
56struct bref {
57 struct list users;
58 struct list *ref; /* pointer to the target's list entry */
59};
60
Willy Tarreaudeb9ed82010-01-03 21:03:22 +010061/* a word list is a generic list with a pointer to a string in each element. */
62struct wordlist {
63 struct list list;
64 char *s;
65};
66
Willy Tarreauf4f04122010-01-28 18:10:50 +010067/* this is the same as above with an additional pointer to a condition. */
68struct cond_wordlist {
69 struct list list;
70 void *cond;
71 char *s;
72};
73
Willy Tarreaubd578bb2007-10-28 11:41:06 +010074/* 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 Tarreaudc13c112013-06-21 23:16:39 +020081/* 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 Tarreaubaaee002006-06-26 02:48:02 +020087#define LIST_HEAD(a) ((void *)(&(a)))
88
willy tarreau80862a32006-04-12 19:15:57 +020089#define LIST_INIT(l) ((l)->n = (l)->p = (l))
90
Willy Tarreau2b1dccd2007-05-07 00:18:32 +020091#define LIST_HEAD_INIT(l) { &l, &l }
92
willy tarreau80862a32006-04-12 19:15:57 +020093/* 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 Tarreau9bead8c2019-08-16 11:27:50 +020099/* 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 Tarreauc32a0e52019-10-04 18:01:39 +0200107/* 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 tarreau80862a32006-04-12 19:15:57 +0200120/* 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 Tarreauc5bd3112019-03-06 19:32:11 +0100123/* 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 tarreau80862a32006-04-12 19:15:57 +0200135/* 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 Tarreau42ccb5a2019-05-13 17:48:46 +0200145/* 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 tarreau80862a32006-04-12 19:15:57 +0200150/* 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 Herlant41abef72018-11-25 10:57:13 -0800158/* returns a pointer of type <pt> to a structure preceding the element
willy tarreau80862a32006-04-12 19:15:57 +0200159 * which contains list head <lh>, which is known as element <el> in
160 * struct pt.
161 */
Thierry FOURNIER1db96672015-11-03 19:17:37 +0100162#undef LIST_PREV
willy tarreau80862a32006-04-12 19:15:57 +0200163#define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el))
164
165/*
Willy Tarreaub9c62b92007-05-02 20:46:49 +0200166 * 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 Lallemand83215a42017-09-24 11:26:02 +0200179 * 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 Tarreaub9c62b92007-05-02 20:46:49 +0200189 * 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 Lallemand83215a42017-09-24 11:26:02 +0200203/*
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 Houcharda8434ec2019-01-18 17:26:26 +0100214#include <common/hathreads.h>
Olivier Houchard859dc802019-08-08 15:47:21 +0200215#define MT_LIST_BUSY ((struct mt_list *)1)
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100216
217/*
218 * Locked version of list manipulation macros.
219 * It is OK to use those concurrently from multiple threads, as long as the
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200220 * 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 Houcharda8434ec2019-01-18 17:26:26 +0100227 */
Olivier Houchard74715da2019-10-11 16:55:11 +0200228#define MT_LIST_ADD(_lh, _el) \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200229 ({ \
230 int _ret = 0; \
Olivier Houchard74715da2019-10-11 16:55:11 +0200231 struct mt_list *lh = (_lh), *el = (_el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100232 do { \
233 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200234 struct mt_list *n; \
235 struct mt_list *p; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200236 n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
237 if (n == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100238 continue; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200239 p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \
240 if (p == MT_LIST_BUSY) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200241 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100242 __ha_barrier_store(); \
243 continue; \
244 } \
Olivier Houchardcb22ad42019-09-20 14:44:22 +0200245 if ((el)->next != (el) || (el)->prev != (el)) { \
246 (n)->prev = p; \
247 (lh)->next = n; \
248 break; \
249 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200250 (el)->next = n; \
251 (el)->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100252 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200253 n->prev = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100254 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200255 p->next = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100256 __ha_barrier_store(); \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200257 _ret = 1; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100258 break; \
259 } \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200260 } while (0); \
261 (_ret); \
262 })
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100263
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200264/*
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 Houchard74715da2019-10-11 16:55:11 +0200269#define MT_LIST_ADDQ(_lh, _el) \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200270 ({ \
Olivier Houchard804ef242019-10-11 16:57:43 +0200271 int _ret = 0; \
Olivier Houchard74715da2019-10-11 16:55:11 +0200272 struct mt_list *lh = (_lh), *el = (_el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100273 do { \
274 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200275 struct mt_list *n; \
276 struct mt_list *p; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200277 p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \
278 if (p == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100279 continue; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200280 n = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY); \
281 if (n == MT_LIST_BUSY) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200282 (lh)->prev = p; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100283 __ha_barrier_store(); \
284 continue; \
285 } \
Olivier Houchardcb22ad42019-09-20 14:44:22 +0200286 if ((el)->next != (el) || (el)->prev != (el)) { \
287 p->next = n; \
288 (lh)->prev = p; \
289 break; \
290 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200291 (el)->next = n; \
292 (el)->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100293 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200294 p->next = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100295 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200296 n->prev = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100297 __ha_barrier_store(); \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200298 _ret = 1; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100299 break; \
300 } \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200301 } while (0); \
302 (_ret); \
303 })
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100304
Willy Tarreaud7f2bbc2019-10-04 18:02:40 +0200305/*
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.
310 */
Olivier Houchard74715da2019-10-11 16:55:11 +0200311#define MT_LIST_BEHEAD(_lh) ({ \
312 struct mt_list *lh = (_lh); \
Willy Tarreaud7f2bbc2019-10-04 18:02:40 +0200313 struct mt_list *_n; \
314 struct mt_list *_p; \
315 while (1) { \
316 _p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \
317 if (_p == MT_LIST_BUSY) \
318 continue; \
319 if (_p == (lh)) { \
320 (lh)->prev = _p; \
321 _n = NULL; \
322 break; \
323 } \
324 _n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
325 if (_n == MT_LIST_BUSY) { \
326 (lh)->prev = _p; \
327 __ha_barrier_store(); \
328 continue; \
329 } \
330 if (_n == (lh)) { \
331 (lh)->next = _n; \
332 (lh)->prev = _p; \
333 _n = NULL; \
334 break; \
335 } \
336 (lh)->next = (lh); \
337 (lh)->prev = (lh); \
338 _n->prev = _p; \
339 _p->next = _n; \
340 __ha_barrier_store(); \
341 break; \
342 } \
343 (_n); \
344})
345
346
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200347/* Remove an item from a list.
348 * Returns 1 if we removed the item, 0 otherwise (because it was in no list).
349 */
Olivier Houchard74715da2019-10-11 16:55:11 +0200350#define MT_LIST_DEL(_el) \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200351 ({ \
352 int _ret = 0; \
Olivier Houchard74715da2019-10-11 16:55:11 +0200353 struct mt_list *el = (_el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100354 do { \
355 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200356 struct mt_list *n, *n2; \
357 struct mt_list *p, *p2 = NULL; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200358 n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \
359 if (n == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100360 continue; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200361 p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \
362 if (p == MT_LIST_BUSY) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200363 (el)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100364 __ha_barrier_store(); \
365 continue; \
366 } \
367 if (p != (el)) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200368 p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
Olivier Houchard804ef242019-10-11 16:57:43 +0200369 if (p2 == MT_LIST_BUSY) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200370 (el)->prev = p; \
371 (el)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100372 __ha_barrier_store(); \
373 continue; \
374 } \
375 } \
376 if (n != (el)) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200377 n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
Olivier Houchard804ef242019-10-11 16:57:43 +0200378 if (n2 == MT_LIST_BUSY) { \
Olivier Houcharddb644892019-02-26 18:46:07 +0100379 if (p2 != NULL) \
Olivier Houchard859dc802019-08-08 15:47:21 +0200380 p->next = p2; \
381 (el)->prev = p; \
382 (el)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100383 __ha_barrier_store(); \
384 continue; \
385 } \
386 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200387 n->prev = p; \
388 p->next = n; \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200389 if (p != (el) && n != (el)) \
390 _ret = 1; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100391 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200392 (el)->prev = (el); \
393 (el)->next = (el); \
Willy Tarreau4c747e82019-02-28 15:05:53 +0100394 __ha_barrier_store(); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100395 break; \
396 } \
Olivier Houchard0cd6a972019-09-20 17:32:47 +0200397 } while (0); \
398 (_ret); \
399 })
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100400
401
402/* Remove the first element from the list, and return it */
Olivier Houchard74715da2019-10-11 16:55:11 +0200403#define MT_LIST_POP(_lh, pt, el) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100404 ({ \
405 void *_ret; \
Olivier Houchard74715da2019-10-11 16:55:11 +0200406 struct mt_list *lh = (_lh); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100407 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200408 struct mt_list *n, *n2; \
409 struct mt_list *p, *p2; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200410 n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
411 if (n == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100412 continue; \
413 if (n == (lh)) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200414 (lh)->next = lh; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100415 __ha_barrier_store(); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100416 _ret = NULL; \
417 break; \
418 } \
Olivier Houchard804ef242019-10-11 16:57:43 +0200419 p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \
420 if (p == MT_LIST_BUSY) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200421 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100422 __ha_barrier_store(); \
423 continue; \
424 } \
Olivier Houchard804ef242019-10-11 16:57:43 +0200425 n2 = _HA_ATOMIC_XCHG(&n->next, MT_LIST_BUSY); \
426 if (n2 == MT_LIST_BUSY) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200427 n->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100428 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200429 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100430 __ha_barrier_store(); \
431 continue; \
432 } \
Olivier Houchard804ef242019-10-11 16:57:43 +0200433 p2 = _HA_ATOMIC_XCHG(&n2->prev, MT_LIST_BUSY); \
434 if (p2 == MT_LIST_BUSY) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200435 n->next = n2; \
436 n->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100437 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200438 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100439 __ha_barrier_store(); \
440 continue; \
441 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200442 (lh)->next = n2; \
443 (n2)->prev = (lh); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100444 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200445 (n)->prev = (n); \
446 (n)->next = (n); \
Willy Tarreau4c747e82019-02-28 15:05:53 +0100447 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200448 _ret = MT_LIST_ELEM(n, pt, el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100449 break; \
450 } \
451 (_ret); \
452 })
William Lallemand83215a42017-09-24 11:26:02 +0200453
Olivier Houchard859dc802019-08-08 15:47:21 +0200454#define MT_LIST_HEAD(a) ((void *)(&(a)))
455
456#define MT_LIST_INIT(l) ((l)->next = (l)->prev = (l))
457
458#define MT_LIST_HEAD_INIT(l) { &l, &l }
459/* returns a pointer of type <pt> to a structure containing a list head called
460 * <el> at address <lh>. Note that <lh> can be the result of a function or macro
461 * since it's used only once.
462 * Example: MT_LIST_ELEM(cur_node->args.next, struct node *, args)
463 */
464#define MT_LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el)))
465
466/* checks if the list head <lh> is empty or not */
467#define MT_LIST_ISEMPTY(lh) ((lh)->next == (lh))
468
469/* returns a pointer of type <pt> to a structure following the element
470 * which contains list head <lh>, which is known as element <el> in
471 * struct pt.
472 * Example: MT_LIST_NEXT(args, struct node *, list)
473 */
474#define MT_LIST_NEXT(lh, pt, el) (MT_LIST_ELEM((lh)->next, pt, el))
475
476
477/* returns a pointer of type <pt> to a structure preceding the element
478 * which contains list head <lh>, which is known as element <el> in
479 * struct pt.
480 */
481#undef MT_LIST_PREV
482#define MT_LIST_PREV(lh, pt, el) (MT_LIST_ELEM((lh)->prev, pt, el))
483
484/* checks if the list element <el> was added to a list or not. This only
485 * works when detached elements are reinitialized (using LIST_DEL_INIT)
486 */
487#define MT_LIST_ADDED(el) ((el)->next != (el))
488
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200489/* Lock an element in the list, to be sure it won't be removed.
490 * It needs to be synchronized somehow to be sure it's not removed
491 * from the list in the meanwhile.
492 * This returns a struct mt_list, that will be needed at unlock time.
493 */
Olivier Houchard74715da2019-10-11 16:55:11 +0200494#define MT_LIST_LOCK_ELT(_el) \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200495 ({ \
496 struct mt_list ret; \
Olivier Houchard74715da2019-10-11 16:55:11 +0200497 struct mt_liet *el = (_el); \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200498 while (1) { \
499 struct mt_list *n, *n2; \
500 struct mt_list *p, *p2 = NULL; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200501 n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \
502 if (n == MT_LIST_BUSY) \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200503 continue; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200504 p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \
505 if (p == MT_LIST_BUSY) { \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200506 (el)->next = n; \
507 __ha_barrier_store(); \
508 continue; \
509 } \
510 if (p != (el)) { \
511 p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
Olivier Houchard804ef242019-10-11 16:57:43 +0200512 if (p2 == MT_LIST_BUSY) { \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200513 (el)->prev = p; \
514 (el)->next = n; \
515 __ha_barrier_store(); \
516 continue; \
517 } \
518 } \
519 if (n != (el)) { \
520 n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
Olivier Houchard804ef242019-10-11 16:57:43 +0200521 if (n2 == MT_LIST_BUSY) { \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200522 if (p2 != NULL) \
523 p->next = p2; \
524 (el)->prev = p; \
525 (el)->next = n; \
526 __ha_barrier_store(); \
527 continue; \
528 } \
529 } \
530 ret.next = n; \
531 ret.prev = p; \
532 break; \
533 } \
534 ret; \
535 })
536
537/* Unlock an element previously locked by MT_LIST_LOCK_ELT. "np" is the
538 * struct mt_list returned by MT_LIST_LOCK_ELT().
539 */
Olivier Houchard74715da2019-10-11 16:55:11 +0200540#define MT_LIST_UNLOCK_ELT(_el, np) \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200541 do { \
542 struct mt_list *n = (np).next, *p = (np).prev; \
Olivier Houchard74715da2019-10-11 16:55:11 +0200543 struct mt_list *el = (_el); \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200544 (el)->next = n; \
545 (el)->prev = p; \
546 if (n != (el)) \
547 n->prev = (el); \
548 if (p != (el)) \
549 p->next = (el); \
550 } while (0)
551
552/* Internal macroes for the foreach macroes */
553#define _MT_LIST_UNLOCK_NEXT(el, np) \
554 do { \
555 struct mt_list *n = (np); \
556 (el)->next = n; \
557 if (n != (el)) \
558 n->prev = (el); \
559 } while (0)
560
561/* Internal macroes for the foreach macroes */
562#define _MT_LIST_UNLOCK_PREV(el, np) \
563 do { \
564 struct mt_list *p = (np); \
565 (el)->prev = p; \
566 if (p != (el)) \
567 p->next = (el); \
568 } while (0)
569
570#define _MT_LIST_LOCK_NEXT(el) \
571 ({ \
572 struct mt_list *n = NULL; \
573 while (1) { \
574 struct mt_list *n2; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200575 n = _HA_ATOMIC_XCHG(&((el)->next), MT_LIST_BUSY); \
576 if (n == MT_LIST_BUSY) \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200577 continue; \
578 if (n != (el)) { \
579 n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
Olivier Houchard804ef242019-10-11 16:57:43 +0200580 if (n2 == MT_LIST_BUSY) { \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200581 (el)->next = n; \
582 __ha_barrier_store(); \
583 continue; \
584 } \
585 } \
586 break; \
587 } \
588 n; \
589 })
590
591#define _MT_LIST_LOCK_PREV(el) \
592 ({ \
593 struct mt_list *p = NULL; \
594 while (1) { \
595 struct mt_list *p2; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200596 p = _HA_ATOMIC_XCHG(&((el)->prev), MT_LIST_BUSY); \
597 if (p == MT_LIST_BUSY) \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200598 continue; \
599 if (p != (el)) { \
600 p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
Olivier Houchard804ef242019-10-11 16:57:43 +0200601 if (p2 == MT_LIST_BUSY) { \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200602 (el)->prev = p; \
603 __ha_barrier_store(); \
604 continue; \
605 } \
606 } \
607 break; \
608 } \
609 p; \
610 })
611
612#define _MT_LIST_RELINK_DELETED(elt2) \
613 do { \
614 struct mt_list *n = elt2.next, *p = elt2.prev; \
615 n->prev = p; \
616 p->next = n; \
617 } while (0);
618
619/* Equivalent of MT_LIST_DEL(), to be used when parsing the list with mt_list_entry_for_each_safe().
620 * It should be the element currently parsed (tmpelt1)
621 */
Olivier Houchard74715da2019-10-11 16:55:11 +0200622#define MT_LIST_DEL_SAFE(_el) \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200623 do { \
Olivier Houchard74715da2019-10-11 16:55:11 +0200624 struct mt_list *el = (_el); \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200625 (el)->prev = (el); \
626 (el)->next = (el); \
627 (el) = NULL; \
628 } while (0)
629
630/* Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources.
631 * Iterates <item> through a list of items of type "typeof(*item)" which are
632 * linked via a "struct list" member named <member>. A pointer to the head of
633 * the list is passed in <list_head>. A temporary variable <back> of same type
634 * as <item> is needed so that <item> may safely be deleted if needed.
635 * tmpelt1 is a temporary struct mt_list *, and tmpelt2 is a temporary
636 * struct mt_list, used internally, both are needed for MT_LIST_DEL_SAFE.
637 * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list, elt1, elt2)
638 * { ... };
639 * If you want to remove the current element, please use MT_LIST_DEL_SAFE.
640 */
641#define mt_list_for_each_entry_safe(item, list_head, member, tmpelt, tmpelt2) \
642 for ((tmpelt) = NULL; (tmpelt) != MT_LIST_BUSY; ({ \
643 if (tmpelt) { \
644 if (tmpelt2.prev) \
645 MT_LIST_UNLOCK_ELT(tmpelt, tmpelt2); \
646 else \
647 _MT_LIST_UNLOCK_NEXT(tmpelt, tmpelt2.next); \
648 } else \
649 _MT_LIST_RELINK_DELETED(tmpelt2); \
650 (tmpelt) = MT_LIST_BUSY; \
Olivier Houchard804ef242019-10-11 16:57:43 +0200651 })) \
Olivier Houchard74715da2019-10-11 16:55:11 +0200652 for ((tmpelt) = (list_head), (tmpelt2).prev = NULL, (tmpelt2).next = _MT_LIST_LOCK_NEXT(tmpelt); ({ \
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200653 (item) = MT_LIST_ELEM((tmpelt2.next), typeof(item), member); \
654 if (&item->member != (list_head)) { \
655 if (tmpelt2.prev != &item->member) \
656 tmpelt2.next = _MT_LIST_LOCK_NEXT(&item->member); \
657 else \
658 tmpelt2.next = tmpelt; \
659 if (tmpelt != NULL) { \
660 if (tmpelt2.prev) \
661 _MT_LIST_UNLOCK_PREV(tmpelt, tmpelt2.prev); \
662 tmpelt2.prev = tmpelt; \
663 } \
664 (tmpelt) = &item->member; \
665 } \
666 }), \
667 &item->member != (list_head);)
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200668#endif /* _COMMON_MINI_CLIST_H */