blob: 3d519197e44e4e161e8a8cd2ced72d187f57d649 [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 tarreau80862a32006-04-12 19:15:57 +0200107/* 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 Tarreauc5bd3112019-03-06 19:32:11 +0100110/* 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 tarreau80862a32006-04-12 19:15:57 +0200122/* 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 Tarreau42ccb5a2019-05-13 17:48:46 +0200132/* 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 tarreau80862a32006-04-12 19:15:57 +0200137/* 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 Herlant41abef72018-11-25 10:57:13 -0800145/* returns a pointer of type <pt> to a structure preceding the element
willy tarreau80862a32006-04-12 19:15:57 +0200146 * which contains list head <lh>, which is known as element <el> in
147 * struct pt.
148 */
Thierry FOURNIER1db96672015-11-03 19:17:37 +0100149#undef LIST_PREV
willy tarreau80862a32006-04-12 19:15:57 +0200150#define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el))
151
152/*
Willy Tarreaub9c62b92007-05-02 20:46:49 +0200153 * 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 Lallemand83215a42017-09-24 11:26:02 +0200166 * 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 Tarreaub9c62b92007-05-02 20:46:49 +0200176 * 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 Lallemand83215a42017-09-24 11:26:02 +0200190/*
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 Houcharda8434ec2019-01-18 17:26:26 +0100201#include <common/hathreads.h>
Olivier Houchard859dc802019-08-08 15:47:21 +0200202#define MT_LIST_BUSY ((struct mt_list *)1)
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100203
204/*
205 * Locked version of list manipulation macros.
206 * It is OK to use those concurrently from multiple threads, as long as the
207 * list is only used with the locked variants. The only "unlocked" macro you
208 * can use with a locked list is LIST_INIT.
209 */
Olivier Houchard859dc802019-08-08 15:47:21 +0200210#define MT_LIST_ADD(lh, el) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100211 do { \
212 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200213 struct mt_list *n; \
214 struct mt_list *p; \
215 n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
216 if (n == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100217 continue; \
Olivier Houchard859dc802019-08-08 15:47:21 +0200218 p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \
219 if (p == MT_LIST_BUSY) { \
220 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100221 __ha_barrier_store(); \
222 continue; \
223 } \
Olivier Houchardcb22ad42019-09-20 14:44:22 +0200224 if ((el)->next != (el) || (el)->prev != (el)) { \
225 (n)->prev = p; \
226 (lh)->next = n; \
227 break; \
228 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200229 (el)->next = n; \
230 (el)->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100231 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200232 n->prev = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100233 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200234 p->next = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100235 __ha_barrier_store(); \
236 break; \
237 } \
238 } while (0)
239
Olivier Houchard859dc802019-08-08 15:47:21 +0200240#define MT_LIST_ADDQ(lh, el) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100241 do { \
242 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200243 struct mt_list *n; \
244 struct mt_list *p; \
245 p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \
246 if (p == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100247 continue; \
Olivier Houchard859dc802019-08-08 15:47:21 +0200248 n = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY); \
249 if (n == MT_LIST_BUSY) { \
250 (lh)->prev = p; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100251 __ha_barrier_store(); \
252 continue; \
253 } \
Olivier Houchardcb22ad42019-09-20 14:44:22 +0200254 if ((el)->next != (el) || (el)->prev != (el)) { \
255 p->next = n; \
256 (lh)->prev = p; \
257 break; \
258 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200259 (el)->next = n; \
260 (el)->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100261 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200262 p->next = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100263 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200264 n->prev = (el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100265 __ha_barrier_store(); \
266 break; \
267 } \
268 } while (0)
269
Olivier Houchard859dc802019-08-08 15:47:21 +0200270#define MT_LIST_DEL(el) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100271 do { \
272 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200273 struct mt_list *n, *n2; \
274 struct mt_list *p, *p2 = NULL; \
275 n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \
276 if (n == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100277 continue; \
Olivier Houchard859dc802019-08-08 15:47:21 +0200278 p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \
279 if (p == MT_LIST_BUSY) { \
280 (el)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100281 __ha_barrier_store(); \
282 continue; \
283 } \
284 if (p != (el)) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200285 p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
286 if (p2 == MT_LIST_BUSY) { \
287 (el)->prev = p; \
288 (el)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100289 __ha_barrier_store(); \
290 continue; \
291 } \
292 } \
293 if (n != (el)) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200294 n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
295 if (n2 == MT_LIST_BUSY) { \
Olivier Houcharddb644892019-02-26 18:46:07 +0100296 if (p2 != NULL) \
Olivier Houchard859dc802019-08-08 15:47:21 +0200297 p->next = p2; \
298 (el)->prev = p; \
299 (el)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100300 __ha_barrier_store(); \
301 continue; \
302 } \
303 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200304 n->prev = p; \
305 p->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100306 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200307 (el)->prev = (el); \
308 (el)->next = (el); \
Willy Tarreau4c747e82019-02-28 15:05:53 +0100309 __ha_barrier_store(); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100310 break; \
311 } \
312 } while (0)
313
314
315/* Remove the first element from the list, and return it */
Olivier Houchard859dc802019-08-08 15:47:21 +0200316#define MT_LIST_POP(lh, pt, el) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100317 ({ \
318 void *_ret; \
319 while (1) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200320 struct mt_list *n, *n2; \
321 struct mt_list *p, *p2; \
322 n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
323 if (n == MT_LIST_BUSY) \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100324 continue; \
325 if (n == (lh)) { \
Olivier Houchard859dc802019-08-08 15:47:21 +0200326 (lh)->next = lh; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100327 __ha_barrier_store(); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100328 _ret = NULL; \
329 break; \
330 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200331 p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \
332 if (p == MT_LIST_BUSY) { \
333 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100334 __ha_barrier_store(); \
335 continue; \
336 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200337 n2 = _HA_ATOMIC_XCHG(&n->next, MT_LIST_BUSY); \
338 if (n2 == MT_LIST_BUSY) { \
339 n->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100340 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200341 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100342 __ha_barrier_store(); \
343 continue; \
344 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200345 p2 = _HA_ATOMIC_XCHG(&n2->prev, MT_LIST_BUSY); \
346 if (p2 == MT_LIST_BUSY) { \
347 n->next = n2; \
348 n->prev = p; \
Willy Tarreau690d2ad2019-02-28 11:14:22 +0100349 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200350 (lh)->next = n; \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100351 __ha_barrier_store(); \
352 continue; \
353 } \
Olivier Houchard859dc802019-08-08 15:47:21 +0200354 (lh)->next = n2; \
355 (n2)->prev = (lh); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100356 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200357 (n)->prev = (n); \
358 (n)->next = (n); \
Willy Tarreau4c747e82019-02-28 15:05:53 +0100359 __ha_barrier_store(); \
Olivier Houchard859dc802019-08-08 15:47:21 +0200360 _ret = MT_LIST_ELEM(n, pt, el); \
Olivier Houcharda8434ec2019-01-18 17:26:26 +0100361 break; \
362 } \
363 (_ret); \
364 })
William Lallemand83215a42017-09-24 11:26:02 +0200365
Olivier Houchard859dc802019-08-08 15:47:21 +0200366#define MT_LIST_HEAD(a) ((void *)(&(a)))
367
368#define MT_LIST_INIT(l) ((l)->next = (l)->prev = (l))
369
370#define MT_LIST_HEAD_INIT(l) { &l, &l }
371/* returns a pointer of type <pt> to a structure containing a list head called
372 * <el> at address <lh>. Note that <lh> can be the result of a function or macro
373 * since it's used only once.
374 * Example: MT_LIST_ELEM(cur_node->args.next, struct node *, args)
375 */
376#define MT_LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el)))
377
378/* checks if the list head <lh> is empty or not */
379#define MT_LIST_ISEMPTY(lh) ((lh)->next == (lh))
380
381/* returns a pointer of type <pt> to a structure following the element
382 * which contains list head <lh>, which is known as element <el> in
383 * struct pt.
384 * Example: MT_LIST_NEXT(args, struct node *, list)
385 */
386#define MT_LIST_NEXT(lh, pt, el) (MT_LIST_ELEM((lh)->next, pt, el))
387
388
389/* returns a pointer of type <pt> to a structure preceding the element
390 * which contains list head <lh>, which is known as element <el> in
391 * struct pt.
392 */
393#undef MT_LIST_PREV
394#define MT_LIST_PREV(lh, pt, el) (MT_LIST_ELEM((lh)->prev, pt, el))
395
396/* checks if the list element <el> was added to a list or not. This only
397 * works when detached elements are reinitialized (using LIST_DEL_INIT)
398 */
399#define MT_LIST_ADDED(el) ((el)->next != (el))
400
Olivier Houchard5e9b92c2019-08-12 14:10:12 +0200401/* Lock an element in the list, to be sure it won't be removed.
402 * It needs to be synchronized somehow to be sure it's not removed
403 * from the list in the meanwhile.
404 * This returns a struct mt_list, that will be needed at unlock time.
405 */
406#define MT_LIST_LOCK_ELT(el) \
407 ({ \
408 struct mt_list ret; \
409 while (1) { \
410 struct mt_list *n, *n2; \
411 struct mt_list *p, *p2 = NULL; \
412 n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \
413 if (n == MT_LIST_BUSY) \
414 continue; \
415 p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \
416 if (p == MT_LIST_BUSY) { \
417 (el)->next = n; \
418 __ha_barrier_store(); \
419 continue; \
420 } \
421 if (p != (el)) { \
422 p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
423 if (p2 == MT_LIST_BUSY) { \
424 (el)->prev = p; \
425 (el)->next = n; \
426 __ha_barrier_store(); \
427 continue; \
428 } \
429 } \
430 if (n != (el)) { \
431 n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
432 if (n2 == MT_LIST_BUSY) { \
433 if (p2 != NULL) \
434 p->next = p2; \
435 (el)->prev = p; \
436 (el)->next = n; \
437 __ha_barrier_store(); \
438 continue; \
439 } \
440 } \
441 ret.next = n; \
442 ret.prev = p; \
443 break; \
444 } \
445 ret; \
446 })
447
448/* Unlock an element previously locked by MT_LIST_LOCK_ELT. "np" is the
449 * struct mt_list returned by MT_LIST_LOCK_ELT().
450 */
451#define MT_LIST_UNLOCK_ELT(el, np) \
452 do { \
453 struct mt_list *n = (np).next, *p = (np).prev; \
454 (el)->next = n; \
455 (el)->prev = p; \
456 if (n != (el)) \
457 n->prev = (el); \
458 if (p != (el)) \
459 p->next = (el); \
460 } while (0)
461
462/* Internal macroes for the foreach macroes */
463#define _MT_LIST_UNLOCK_NEXT(el, np) \
464 do { \
465 struct mt_list *n = (np); \
466 (el)->next = n; \
467 if (n != (el)) \
468 n->prev = (el); \
469 } while (0)
470
471/* Internal macroes for the foreach macroes */
472#define _MT_LIST_UNLOCK_PREV(el, np) \
473 do { \
474 struct mt_list *p = (np); \
475 (el)->prev = p; \
476 if (p != (el)) \
477 p->next = (el); \
478 } while (0)
479
480#define _MT_LIST_LOCK_NEXT(el) \
481 ({ \
482 struct mt_list *n = NULL; \
483 while (1) { \
484 struct mt_list *n2; \
485 n = _HA_ATOMIC_XCHG(&((el)->next), MT_LIST_BUSY); \
486 if (n == MT_LIST_BUSY) \
487 continue; \
488 if (n != (el)) { \
489 n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
490 if (n2 == MT_LIST_BUSY) { \
491 (el)->next = n; \
492 __ha_barrier_store(); \
493 continue; \
494 } \
495 } \
496 break; \
497 } \
498 n; \
499 })
500
501#define _MT_LIST_LOCK_PREV(el) \
502 ({ \
503 struct mt_list *p = NULL; \
504 while (1) { \
505 struct mt_list *p2; \
506 p = _HA_ATOMIC_XCHG(&((el)->prev), MT_LIST_BUSY); \
507 if (p == MT_LIST_BUSY) \
508 continue; \
509 if (p != (el)) { \
510 p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
511 if (p2 == MT_LIST_BUSY) { \
512 (el)->prev = p; \
513 __ha_barrier_store(); \
514 continue; \
515 } \
516 } \
517 break; \
518 } \
519 p; \
520 })
521
522#define _MT_LIST_RELINK_DELETED(elt2) \
523 do { \
524 struct mt_list *n = elt2.next, *p = elt2.prev; \
525 n->prev = p; \
526 p->next = n; \
527 } while (0);
528
529/* Equivalent of MT_LIST_DEL(), to be used when parsing the list with mt_list_entry_for_each_safe().
530 * It should be the element currently parsed (tmpelt1)
531 */
532#define MT_LIST_DEL_SAFE(el) \
533 do { \
534 (el)->prev = (el); \
535 (el)->next = (el); \
536 (el) = NULL; \
537 } while (0)
538
539/* Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources.
540 * Iterates <item> through a list of items of type "typeof(*item)" which are
541 * linked via a "struct list" member named <member>. A pointer to the head of
542 * the list is passed in <list_head>. A temporary variable <back> of same type
543 * as <item> is needed so that <item> may safely be deleted if needed.
544 * tmpelt1 is a temporary struct mt_list *, and tmpelt2 is a temporary
545 * struct mt_list, used internally, both are needed for MT_LIST_DEL_SAFE.
546 * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list, elt1, elt2)
547 * { ... };
548 * If you want to remove the current element, please use MT_LIST_DEL_SAFE.
549 */
550#define mt_list_for_each_entry_safe(item, list_head, member, tmpelt, tmpelt2) \
551 for ((tmpelt) = NULL; (tmpelt) != MT_LIST_BUSY; ({ \
552 if (tmpelt) { \
553 if (tmpelt2.prev) \
554 MT_LIST_UNLOCK_ELT(tmpelt, tmpelt2); \
555 else \
556 _MT_LIST_UNLOCK_NEXT(tmpelt, tmpelt2.next); \
557 } else \
558 _MT_LIST_RELINK_DELETED(tmpelt2); \
559 (tmpelt) = MT_LIST_BUSY; \
560 })) \
561 for ((tmpelt) = (list_head), (tmpelt2).prev = NULL, (tmpelt2).next = _MT_LIST_LOCK_NEXT(list_head); ({ \
562 (item) = MT_LIST_ELEM((tmpelt2.next), typeof(item), member); \
563 if (&item->member != (list_head)) { \
564 if (tmpelt2.prev != &item->member) \
565 tmpelt2.next = _MT_LIST_LOCK_NEXT(&item->member); \
566 else \
567 tmpelt2.next = tmpelt; \
568 if (tmpelt != NULL) { \
569 if (tmpelt2.prev) \
570 _MT_LIST_UNLOCK_PREV(tmpelt, tmpelt2.prev); \
571 tmpelt2.prev = tmpelt; \
572 } \
573 (tmpelt) = &item->member; \
574 } \
575 }), \
576 &item->member != (list_head);)
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200577#endif /* _COMMON_MINI_CLIST_H */