2006-04-12 17:15:57 +00:00
|
|
|
/*
|
2014-12-23 12:58:43 +00:00
|
|
|
* include/common/mini-clist.h
|
|
|
|
* Circular list manipulation macros and structures.
|
2006-04-12 17:15:57 +00:00
|
|
|
*
|
2014-12-23 12:58:43 +00:00
|
|
|
* Copyright (C) 2002-2014 Willy Tarreau - w@1wt.eu
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-04-12 17:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#ifndef _COMMON_MINI_CLIST_H
|
|
|
|
#define _COMMON_MINI_CLIST_H
|
2006-04-12 17:15:57 +00:00
|
|
|
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <common/config.h>
|
|
|
|
|
2006-04-12 17:15:57 +00:00
|
|
|
/* these are circular or bidirectionnal lists only. Each list pointer points to
|
|
|
|
* another list pointer in a structure, and not the structure itself. The
|
|
|
|
* pointer to the next element MUST be the first one so that the list is easily
|
|
|
|
* cast as a single linked list or pointer.
|
|
|
|
*/
|
|
|
|
struct list {
|
|
|
|
struct list *n; /* next */
|
|
|
|
struct list *p; /* prev */
|
|
|
|
};
|
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
/* This is similar to struct list, but we want to be sure the compiler will
|
|
|
|
* yell at you if you use macroes for one when you're using the other. You have
|
|
|
|
* to expicitely cast if that's really what you want to do.
|
|
|
|
*/
|
|
|
|
struct mt_list {
|
|
|
|
struct mt_list *next;
|
|
|
|
struct mt_list *prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-07 19:00:15 +00:00
|
|
|
/* a back-ref is a pointer to a target list entry. It is used to detect when an
|
|
|
|
* element being deleted is currently being tracked by another user. The best
|
|
|
|
* example is a user dumping the session table. The table does not fit in the
|
|
|
|
* output buffer so we have to set a mark on a session and go on later. But if
|
|
|
|
* that marked session gets deleted, we don't want the user's pointer to go in
|
|
|
|
* the wild. So we can simply link this user's request to the list of this
|
|
|
|
* session's users, and put a pointer to the list element in ref, that will be
|
|
|
|
* used as the mark for next iteration.
|
|
|
|
*/
|
|
|
|
struct bref {
|
|
|
|
struct list users;
|
|
|
|
struct list *ref; /* pointer to the target's list entry */
|
|
|
|
};
|
|
|
|
|
2010-01-03 20:03:22 +00:00
|
|
|
/* a word list is a generic list with a pointer to a string in each element. */
|
|
|
|
struct wordlist {
|
|
|
|
struct list list;
|
|
|
|
char *s;
|
|
|
|
};
|
|
|
|
|
2010-01-28 17:10:50 +00:00
|
|
|
/* this is the same as above with an additional pointer to a condition. */
|
|
|
|
struct cond_wordlist {
|
|
|
|
struct list list;
|
|
|
|
void *cond;
|
|
|
|
char *s;
|
|
|
|
};
|
|
|
|
|
2007-10-28 10:41:06 +00:00
|
|
|
/* First undefine some macros which happen to also be defined on OpenBSD,
|
|
|
|
* in sys/queue.h, used by sys/event.h
|
|
|
|
*/
|
|
|
|
#undef LIST_HEAD
|
|
|
|
#undef LIST_INIT
|
|
|
|
#undef LIST_NEXT
|
|
|
|
|
2013-06-21 21:16:39 +00:00
|
|
|
/* ILH = Initialized List Head : used to prevent gcc from moving an empty
|
|
|
|
* list to BSS. Some older version tend to trim all the array and cause
|
|
|
|
* corruption.
|
|
|
|
*/
|
|
|
|
#define ILH { .n = (struct list *)1, .p = (struct list *)2 }
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
#define LIST_HEAD(a) ((void *)(&(a)))
|
|
|
|
|
2006-04-12 17:15:57 +00:00
|
|
|
#define LIST_INIT(l) ((l)->n = (l)->p = (l))
|
|
|
|
|
2007-05-06 22:18:32 +00:00
|
|
|
#define LIST_HEAD_INIT(l) { &l, &l }
|
|
|
|
|
2006-04-12 17:15:57 +00:00
|
|
|
/* adds an element at the beginning of a list ; returns the element */
|
|
|
|
#define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); })
|
|
|
|
|
|
|
|
/* adds an element at the end of a list ; returns the element */
|
|
|
|
#define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); })
|
|
|
|
|
2019-08-16 09:27:50 +00:00
|
|
|
/* adds the contents of a list <old> at the beginning of another list <new>. The old list head remains untouched. */
|
|
|
|
#define LIST_SPLICE(new, old) do { \
|
|
|
|
if (!LIST_ISEMPTY(old)) { \
|
|
|
|
(old)->p->n = (new)->n; (old)->n->p = (new); \
|
|
|
|
(new)->n->p = (old)->p; (new)->n = (old)->n; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2019-10-04 16:01:39 +00:00
|
|
|
/* adds the contents of a list whose first element is <old> and last one is
|
|
|
|
* <old->prev> at the end of another list <new>. The old list DOES NOT have
|
|
|
|
* any head here.
|
|
|
|
*/
|
|
|
|
#define LIST_SPLICE_END_DETACHED(new, old) do { \
|
|
|
|
typeof(new) __t; \
|
|
|
|
(new)->p->n = (old); \
|
|
|
|
(old)->p->n = (new); \
|
|
|
|
__t = (old)->p; \
|
|
|
|
(old)->p = (new)->p; \
|
|
|
|
(new)->p = __t; \
|
|
|
|
} while (0)
|
|
|
|
|
2006-04-12 17:15:57 +00:00
|
|
|
/* removes an element from a list and returns it */
|
|
|
|
#define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); })
|
|
|
|
|
2019-03-06 18:32:11 +00:00
|
|
|
/* removes an element from a list, initializes it and returns it.
|
|
|
|
* This is faster than LIST_DEL+LIST_INIT as we avoid reloading the pointers.
|
|
|
|
*/
|
|
|
|
#define LIST_DEL_INIT(el) ({ \
|
|
|
|
typeof(el) __ret = (el); \
|
|
|
|
typeof(__ret->n) __n = __ret->n; \
|
|
|
|
typeof(__ret->p) __p = __ret->p; \
|
|
|
|
__n->p = __p; __p->n = __n; \
|
|
|
|
__ret->n = __ret->p = __ret; \
|
|
|
|
__ret; \
|
|
|
|
})
|
|
|
|
|
2006-04-12 17:15:57 +00:00
|
|
|
/* returns a pointer of type <pt> to a structure containing a list head called
|
|
|
|
* <el> at address <lh>. Note that <lh> can be the result of a function or macro
|
|
|
|
* since it's used only once.
|
|
|
|
* Example: LIST_ELEM(cur_node->args.next, struct node *, args)
|
|
|
|
*/
|
|
|
|
#define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el)))
|
|
|
|
|
|
|
|
/* checks if the list head <lh> is empty or not */
|
|
|
|
#define LIST_ISEMPTY(lh) ((lh)->n == (lh))
|
|
|
|
|
2019-05-13 15:48:46 +00:00
|
|
|
/* checks if the list element <el> was added to a list or not. This only
|
|
|
|
* works when detached elements are reinitialized (using LIST_DEL_INIT)
|
|
|
|
*/
|
|
|
|
#define LIST_ADDED(el) ((el)->n != (el))
|
|
|
|
|
2006-04-12 17:15:57 +00:00
|
|
|
/* returns a pointer of type <pt> to a structure following the element
|
|
|
|
* which contains list head <lh>, which is known as element <el> in
|
|
|
|
* struct pt.
|
|
|
|
* Example: LIST_NEXT(args, struct node *, list)
|
|
|
|
*/
|
|
|
|
#define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el))
|
|
|
|
|
|
|
|
|
2018-11-25 18:57:13 +00:00
|
|
|
/* returns a pointer of type <pt> to a structure preceding the element
|
2006-04-12 17:15:57 +00:00
|
|
|
* which contains list head <lh>, which is known as element <el> in
|
|
|
|
* struct pt.
|
|
|
|
*/
|
2015-11-03 18:17:37 +00:00
|
|
|
#undef LIST_PREV
|
2006-04-12 17:15:57 +00:00
|
|
|
#define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el))
|
|
|
|
|
2007-05-02 18:46:49 +00:00
|
|
|
/*
|
|
|
|
* Simpler FOREACH_ITEM macro inspired from Linux sources.
|
|
|
|
* Iterates <item> through a list of items of type "typeof(*item)" which are
|
|
|
|
* linked via a "struct list" member named <member>. A pointer to the head of
|
|
|
|
* the list is passed in <list_head>. No temporary variable is needed. Note
|
|
|
|
* that <item> must not be modified during the loop.
|
|
|
|
* Example: list_for_each_entry(cur_acl, known_acl, list) { ... };
|
|
|
|
*/
|
|
|
|
#define list_for_each_entry(item, list_head, member) \
|
|
|
|
for (item = LIST_ELEM((list_head)->n, typeof(item), member); \
|
|
|
|
&item->member != (list_head); \
|
|
|
|
item = LIST_ELEM(item->member.n, typeof(item), member))
|
|
|
|
|
2017-09-24 09:26:02 +00:00
|
|
|
/*
|
|
|
|
* Same as list_for_each_entry but starting from current point
|
|
|
|
* Iterates <item> through the list starting from <item>
|
|
|
|
* It's basically the same macro but without initializing item to the head of
|
|
|
|
* the list.
|
|
|
|
*/
|
|
|
|
#define list_for_each_entry_from(item, list_head, member) \
|
|
|
|
for ( ; &item->member != (list_head); \
|
|
|
|
item = LIST_ELEM(item->member.n, typeof(item), member))
|
|
|
|
|
2007-05-02 18:46:49 +00:00
|
|
|
/*
|
|
|
|
* Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources.
|
|
|
|
* Iterates <item> through a list of items of type "typeof(*item)" which are
|
|
|
|
* linked via a "struct list" member named <member>. A pointer to the head of
|
|
|
|
* the list is passed in <list_head>. A temporary variable <back> of same type
|
|
|
|
* as <item> is needed so that <item> may safely be deleted if needed.
|
|
|
|
* Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list) { ... };
|
|
|
|
*/
|
|
|
|
#define list_for_each_entry_safe(item, back, list_head, member) \
|
|
|
|
for (item = LIST_ELEM((list_head)->n, typeof(item), member), \
|
|
|
|
back = LIST_ELEM(item->member.n, typeof(item), member); \
|
|
|
|
&item->member != (list_head); \
|
|
|
|
item = back, back = LIST_ELEM(back->member.n, typeof(back), member))
|
|
|
|
|
|
|
|
|
2017-09-24 09:26:02 +00:00
|
|
|
/*
|
|
|
|
* Same as list_for_each_entry_safe but starting from current point
|
|
|
|
* Iterates <item> through the list starting from <item>
|
|
|
|
* It's basically the same macro but without initializing item to the head of
|
|
|
|
* the list.
|
|
|
|
*/
|
|
|
|
#define list_for_each_entry_safe_from(item, back, list_head, member) \
|
|
|
|
for (back = LIST_ELEM(item->member.n, typeof(item), member); \
|
|
|
|
&item->member != (list_head); \
|
|
|
|
item = back, back = LIST_ELEM(back->member.n, typeof(back), member))
|
|
|
|
|
2019-01-18 16:26:26 +00:00
|
|
|
#include <common/hathreads.h>
|
2019-08-08 13:47:21 +00:00
|
|
|
#define MT_LIST_BUSY ((struct mt_list *)1)
|
2019-01-18 16:26:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Locked version of list manipulation macros.
|
|
|
|
* It is OK to use those concurrently from multiple threads, as long as the
|
2019-09-20 15:32:47 +00:00
|
|
|
* list is only used with the locked variants.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an item at the beginning of a list.
|
|
|
|
* Returns 1 if we added the item, 0 otherwise (because it was already in a
|
|
|
|
* list).
|
2019-01-18 16:26:26 +00:00
|
|
|
*/
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_ADD(_lh, _el) \
|
2019-09-20 15:32:47 +00:00
|
|
|
({ \
|
|
|
|
int _ret = 0; \
|
2019-10-11 14:55:11 +00:00
|
|
|
struct mt_list *lh = (_lh), *el = (_el); \
|
2020-02-11 09:17:52 +00:00
|
|
|
while (1) { \
|
|
|
|
struct mt_list *n; \
|
|
|
|
struct mt_list *p; \
|
|
|
|
n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
|
|
|
|
if (n == MT_LIST_BUSY) \
|
|
|
|
continue; \
|
|
|
|
p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \
|
|
|
|
if (p == MT_LIST_BUSY) { \
|
|
|
|
(lh)->next = n; \
|
2019-02-28 10:14:22 +00:00
|
|
|
__ha_barrier_store(); \
|
2020-02-11 09:17:52 +00:00
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
if ((el)->next != (el) || (el)->prev != (el)) { \
|
|
|
|
(n)->prev = p; \
|
|
|
|
(lh)->next = n; \
|
2019-01-18 16:26:26 +00:00
|
|
|
break; \
|
|
|
|
} \
|
2020-02-11 09:17:52 +00:00
|
|
|
(el)->next = n; \
|
|
|
|
(el)->prev = p; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
n->prev = (el); \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
p->next = (el); \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
_ret = 1; \
|
|
|
|
break; \
|
|
|
|
} \
|
2019-09-20 15:32:47 +00:00
|
|
|
(_ret); \
|
|
|
|
})
|
2019-01-18 16:26:26 +00:00
|
|
|
|
2019-09-20 15:32:47 +00:00
|
|
|
/*
|
|
|
|
* Add an item at the end of a list.
|
|
|
|
* Returns 1 if we added the item, 0 otherwise (because it was already in a
|
|
|
|
* list).
|
|
|
|
*/
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_ADDQ(_lh, _el) \
|
2019-09-20 15:32:47 +00:00
|
|
|
({ \
|
2020-02-11 09:17:52 +00:00
|
|
|
int _ret = 0; \
|
|
|
|
struct mt_list *lh = (_lh), *el = (_el); \
|
|
|
|
while (1) { \
|
|
|
|
struct mt_list *n; \
|
|
|
|
struct mt_list *p; \
|
|
|
|
p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \
|
|
|
|
if (p == MT_LIST_BUSY) \
|
|
|
|
continue; \
|
|
|
|
n = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY); \
|
|
|
|
if (n == MT_LIST_BUSY) { \
|
|
|
|
(lh)->prev = p; \
|
2019-01-18 16:26:26 +00:00
|
|
|
__ha_barrier_store(); \
|
2020-02-11 09:17:52 +00:00
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
if ((el)->next != (el) || (el)->prev != (el)) { \
|
|
|
|
p->next = n; \
|
|
|
|
(lh)->prev = p; \
|
2019-01-18 16:26:26 +00:00
|
|
|
break; \
|
|
|
|
} \
|
2020-02-11 09:17:52 +00:00
|
|
|
(el)->next = n; \
|
|
|
|
(el)->prev = p; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
p->next = (el); \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
n->prev = (el); \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
_ret = 1; \
|
|
|
|
break; \
|
|
|
|
} \
|
2019-09-20 15:32:47 +00:00
|
|
|
(_ret); \
|
|
|
|
})
|
2019-01-18 16:26:26 +00:00
|
|
|
|
2019-10-04 16:02:40 +00:00
|
|
|
/*
|
|
|
|
* Detach a list from its head. A pointer to the first element is returned
|
|
|
|
* and the list is closed. If the list was empty, NULL is returned. This may
|
|
|
|
* exclusively be used with lists modified by MT_LIST_ADD/MT_LIST_ADDQ. This
|
|
|
|
* is incompatible with MT_LIST_DEL run concurrently.
|
2019-10-17 15:46:01 +00:00
|
|
|
* If there's at least one element, the next of the last element will always
|
|
|
|
* be NULL.
|
2019-10-04 16:02:40 +00:00
|
|
|
*/
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_BEHEAD(_lh) ({ \
|
|
|
|
struct mt_list *lh = (_lh); \
|
2019-10-04 16:02:40 +00:00
|
|
|
struct mt_list *_n; \
|
|
|
|
struct mt_list *_p; \
|
|
|
|
while (1) { \
|
|
|
|
_p = _HA_ATOMIC_XCHG(&(lh)->prev, MT_LIST_BUSY); \
|
|
|
|
if (_p == MT_LIST_BUSY) \
|
|
|
|
continue; \
|
|
|
|
if (_p == (lh)) { \
|
|
|
|
(lh)->prev = _p; \
|
|
|
|
_n = NULL; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
_n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
|
|
|
|
if (_n == MT_LIST_BUSY) { \
|
|
|
|
(lh)->prev = _p; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
if (_n == (lh)) { \
|
|
|
|
(lh)->next = _n; \
|
|
|
|
(lh)->prev = _p; \
|
|
|
|
_n = NULL; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
(lh)->next = (lh); \
|
|
|
|
(lh)->prev = (lh); \
|
|
|
|
_n->prev = _p; \
|
2019-10-17 15:46:01 +00:00
|
|
|
_p->next = NULL; \
|
2019-10-04 16:02:40 +00:00
|
|
|
__ha_barrier_store(); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
(_n); \
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-09-20 15:32:47 +00:00
|
|
|
/* Remove an item from a list.
|
|
|
|
* Returns 1 if we removed the item, 0 otherwise (because it was in no list).
|
|
|
|
*/
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_DEL(_el) \
|
2019-09-20 15:32:47 +00:00
|
|
|
({ \
|
|
|
|
int _ret = 0; \
|
2019-10-11 14:55:11 +00:00
|
|
|
struct mt_list *el = (_el); \
|
2020-02-11 09:17:52 +00:00
|
|
|
while (1) { \
|
|
|
|
struct mt_list *n, *n2; \
|
|
|
|
struct mt_list *p, *p2 = NULL; \
|
|
|
|
n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \
|
|
|
|
if (n == MT_LIST_BUSY) \
|
|
|
|
continue; \
|
|
|
|
p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \
|
|
|
|
if (p == MT_LIST_BUSY) { \
|
|
|
|
(el)->next = n; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
if (p != (el)) { \
|
|
|
|
p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY); \
|
|
|
|
if (p2 == MT_LIST_BUSY) { \
|
|
|
|
(el)->prev = p; \
|
2019-08-08 13:47:21 +00:00
|
|
|
(el)->next = n; \
|
2019-01-18 16:26:26 +00:00
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
2020-02-11 09:17:52 +00:00
|
|
|
} \
|
|
|
|
if (n != (el)) { \
|
|
|
|
n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \
|
|
|
|
if (n2 == MT_LIST_BUSY) { \
|
|
|
|
if (p2 != NULL) \
|
|
|
|
p->next = p2; \
|
|
|
|
(el)->prev = p; \
|
|
|
|
(el)->next = n; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
2019-01-18 16:26:26 +00:00
|
|
|
} \
|
|
|
|
} \
|
2020-02-11 09:17:52 +00:00
|
|
|
n->prev = p; \
|
|
|
|
p->next = n; \
|
|
|
|
if (p != (el) && n != (el)) \
|
|
|
|
_ret = 1; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
(el)->prev = (el); \
|
|
|
|
(el)->next = (el); \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
break; \
|
|
|
|
} \
|
2019-09-20 15:32:47 +00:00
|
|
|
(_ret); \
|
|
|
|
})
|
2019-01-18 16:26:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Remove the first element from the list, and return it */
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_POP(_lh, pt, el) \
|
2019-01-18 16:26:26 +00:00
|
|
|
({ \
|
|
|
|
void *_ret; \
|
2019-10-11 14:55:11 +00:00
|
|
|
struct mt_list *lh = (_lh); \
|
2019-01-18 16:26:26 +00:00
|
|
|
while (1) { \
|
2019-08-08 13:47:21 +00:00
|
|
|
struct mt_list *n, *n2; \
|
|
|
|
struct mt_list *p, *p2; \
|
2019-10-11 14:57:43 +00:00
|
|
|
n = _HA_ATOMIC_XCHG(&(lh)->next, MT_LIST_BUSY); \
|
|
|
|
if (n == MT_LIST_BUSY) \
|
2019-01-18 16:26:26 +00:00
|
|
|
continue; \
|
|
|
|
if (n == (lh)) { \
|
2019-08-08 13:47:21 +00:00
|
|
|
(lh)->next = lh; \
|
2019-02-28 10:14:22 +00:00
|
|
|
__ha_barrier_store(); \
|
2019-01-18 16:26:26 +00:00
|
|
|
_ret = NULL; \
|
|
|
|
break; \
|
|
|
|
} \
|
2019-10-11 14:57:43 +00:00
|
|
|
p = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY); \
|
|
|
|
if (p == MT_LIST_BUSY) { \
|
2019-08-08 13:47:21 +00:00
|
|
|
(lh)->next = n; \
|
2019-01-18 16:26:26 +00:00
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
2019-10-11 14:57:43 +00:00
|
|
|
n2 = _HA_ATOMIC_XCHG(&n->next, MT_LIST_BUSY); \
|
|
|
|
if (n2 == MT_LIST_BUSY) { \
|
2019-08-08 13:47:21 +00:00
|
|
|
n->prev = p; \
|
2019-02-28 10:14:22 +00:00
|
|
|
__ha_barrier_store(); \
|
2019-08-08 13:47:21 +00:00
|
|
|
(lh)->next = n; \
|
2019-01-18 16:26:26 +00:00
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
2019-10-11 14:57:43 +00:00
|
|
|
p2 = _HA_ATOMIC_XCHG(&n2->prev, MT_LIST_BUSY); \
|
|
|
|
if (p2 == MT_LIST_BUSY) { \
|
2019-08-08 13:47:21 +00:00
|
|
|
n->next = n2; \
|
|
|
|
n->prev = p; \
|
2019-02-28 10:14:22 +00:00
|
|
|
__ha_barrier_store(); \
|
2019-08-08 13:47:21 +00:00
|
|
|
(lh)->next = n; \
|
2019-01-18 16:26:26 +00:00
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
2019-08-08 13:47:21 +00:00
|
|
|
(lh)->next = n2; \
|
|
|
|
(n2)->prev = (lh); \
|
2019-01-18 16:26:26 +00:00
|
|
|
__ha_barrier_store(); \
|
2019-08-08 13:47:21 +00:00
|
|
|
(n)->prev = (n); \
|
|
|
|
(n)->next = (n); \
|
2019-02-28 14:05:53 +00:00
|
|
|
__ha_barrier_store(); \
|
2019-08-08 13:47:21 +00:00
|
|
|
_ret = MT_LIST_ELEM(n, pt, el); \
|
2019-01-18 16:26:26 +00:00
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
(_ret); \
|
|
|
|
})
|
2017-09-24 09:26:02 +00:00
|
|
|
|
2019-08-08 13:47:21 +00:00
|
|
|
#define MT_LIST_HEAD(a) ((void *)(&(a)))
|
|
|
|
|
|
|
|
#define MT_LIST_INIT(l) ((l)->next = (l)->prev = (l))
|
|
|
|
|
|
|
|
#define MT_LIST_HEAD_INIT(l) { &l, &l }
|
|
|
|
/* returns a pointer of type <pt> to a structure containing a list head called
|
|
|
|
* <el> at address <lh>. Note that <lh> can be the result of a function or macro
|
|
|
|
* since it's used only once.
|
|
|
|
* Example: MT_LIST_ELEM(cur_node->args.next, struct node *, args)
|
|
|
|
*/
|
|
|
|
#define MT_LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el)))
|
|
|
|
|
|
|
|
/* checks if the list head <lh> is empty or not */
|
|
|
|
#define MT_LIST_ISEMPTY(lh) ((lh)->next == (lh))
|
|
|
|
|
|
|
|
/* returns a pointer of type <pt> to a structure following the element
|
|
|
|
* which contains list head <lh>, which is known as element <el> in
|
|
|
|
* struct pt.
|
|
|
|
* Example: MT_LIST_NEXT(args, struct node *, list)
|
|
|
|
*/
|
|
|
|
#define MT_LIST_NEXT(lh, pt, el) (MT_LIST_ELEM((lh)->next, pt, el))
|
|
|
|
|
|
|
|
|
|
|
|
/* returns a pointer of type <pt> to a structure preceding the element
|
|
|
|
* which contains list head <lh>, which is known as element <el> in
|
|
|
|
* struct pt.
|
|
|
|
*/
|
|
|
|
#undef MT_LIST_PREV
|
|
|
|
#define MT_LIST_PREV(lh, pt, el) (MT_LIST_ELEM((lh)->prev, pt, el))
|
|
|
|
|
|
|
|
/* checks if the list element <el> was added to a list or not. This only
|
|
|
|
* works when detached elements are reinitialized (using LIST_DEL_INIT)
|
|
|
|
*/
|
|
|
|
#define MT_LIST_ADDED(el) ((el)->next != (el))
|
|
|
|
|
2019-08-12 12:10:12 +00:00
|
|
|
/* Lock an element in the list, to be sure it won't be removed.
|
|
|
|
* It needs to be synchronized somehow to be sure it's not removed
|
|
|
|
* from the list in the meanwhile.
|
|
|
|
* This returns a struct mt_list, that will be needed at unlock time.
|
|
|
|
*/
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_LOCK_ELT(_el) \
|
2019-08-12 12:10:12 +00:00
|
|
|
({ \
|
|
|
|
struct mt_list ret; \
|
2019-10-11 14:55:11 +00:00
|
|
|
struct mt_liet *el = (_el); \
|
2019-08-12 12:10:12 +00:00
|
|
|
while (1) { \
|
|
|
|
struct mt_list *n, *n2; \
|
|
|
|
struct mt_list *p, *p2 = NULL; \
|
2019-10-11 14:57:43 +00:00
|
|
|
n = _HA_ATOMIC_XCHG(&(el)->next, MT_LIST_BUSY); \
|
|
|
|
if (n == MT_LIST_BUSY) \
|
2019-08-12 12:10:12 +00:00
|
|
|
continue; \
|
2019-10-11 14:57:43 +00:00
|
|
|
p = _HA_ATOMIC_XCHG(&(el)->prev, MT_LIST_BUSY); \
|
|
|
|
if (p == MT_LIST_BUSY) { \
|
2019-08-12 12:10:12 +00:00
|
|
|
(el)->next = n; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
if (p != (el)) { \
|
|
|
|
p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
|
2019-10-11 14:57:43 +00:00
|
|
|
if (p2 == MT_LIST_BUSY) { \
|
2019-08-12 12:10:12 +00:00
|
|
|
(el)->prev = p; \
|
|
|
|
(el)->next = n; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (n != (el)) { \
|
|
|
|
n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
|
2019-10-11 14:57:43 +00:00
|
|
|
if (n2 == MT_LIST_BUSY) { \
|
2019-08-12 12:10:12 +00:00
|
|
|
if (p2 != NULL) \
|
|
|
|
p->next = p2; \
|
|
|
|
(el)->prev = p; \
|
|
|
|
(el)->next = n; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
ret.next = n; \
|
|
|
|
ret.prev = p; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
/* Unlock an element previously locked by MT_LIST_LOCK_ELT. "np" is the
|
|
|
|
* struct mt_list returned by MT_LIST_LOCK_ELT().
|
|
|
|
*/
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_UNLOCK_ELT(_el, np) \
|
2019-08-12 12:10:12 +00:00
|
|
|
do { \
|
|
|
|
struct mt_list *n = (np).next, *p = (np).prev; \
|
2019-10-11 14:55:11 +00:00
|
|
|
struct mt_list *el = (_el); \
|
2019-08-12 12:10:12 +00:00
|
|
|
(el)->next = n; \
|
|
|
|
(el)->prev = p; \
|
|
|
|
if (n != (el)) \
|
|
|
|
n->prev = (el); \
|
|
|
|
if (p != (el)) \
|
|
|
|
p->next = (el); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Internal macroes for the foreach macroes */
|
|
|
|
#define _MT_LIST_UNLOCK_NEXT(el, np) \
|
|
|
|
do { \
|
|
|
|
struct mt_list *n = (np); \
|
|
|
|
(el)->next = n; \
|
|
|
|
if (n != (el)) \
|
|
|
|
n->prev = (el); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Internal macroes for the foreach macroes */
|
|
|
|
#define _MT_LIST_UNLOCK_PREV(el, np) \
|
|
|
|
do { \
|
|
|
|
struct mt_list *p = (np); \
|
|
|
|
(el)->prev = p; \
|
|
|
|
if (p != (el)) \
|
|
|
|
p->next = (el); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define _MT_LIST_LOCK_NEXT(el) \
|
|
|
|
({ \
|
|
|
|
struct mt_list *n = NULL; \
|
|
|
|
while (1) { \
|
|
|
|
struct mt_list *n2; \
|
2019-10-11 14:57:43 +00:00
|
|
|
n = _HA_ATOMIC_XCHG(&((el)->next), MT_LIST_BUSY); \
|
|
|
|
if (n == MT_LIST_BUSY) \
|
2019-08-12 12:10:12 +00:00
|
|
|
continue; \
|
|
|
|
if (n != (el)) { \
|
|
|
|
n2 = _HA_ATOMIC_XCHG(&n->prev, MT_LIST_BUSY);\
|
2019-10-11 14:57:43 +00:00
|
|
|
if (n2 == MT_LIST_BUSY) { \
|
2019-08-12 12:10:12 +00:00
|
|
|
(el)->next = n; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
n; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define _MT_LIST_LOCK_PREV(el) \
|
|
|
|
({ \
|
|
|
|
struct mt_list *p = NULL; \
|
|
|
|
while (1) { \
|
|
|
|
struct mt_list *p2; \
|
2019-10-11 14:57:43 +00:00
|
|
|
p = _HA_ATOMIC_XCHG(&((el)->prev), MT_LIST_BUSY); \
|
|
|
|
if (p == MT_LIST_BUSY) \
|
2019-08-12 12:10:12 +00:00
|
|
|
continue; \
|
|
|
|
if (p != (el)) { \
|
|
|
|
p2 = _HA_ATOMIC_XCHG(&p->next, MT_LIST_BUSY);\
|
2019-10-11 14:57:43 +00:00
|
|
|
if (p2 == MT_LIST_BUSY) { \
|
2019-08-12 12:10:12 +00:00
|
|
|
(el)->prev = p; \
|
|
|
|
__ha_barrier_store(); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
p; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define _MT_LIST_RELINK_DELETED(elt2) \
|
|
|
|
do { \
|
|
|
|
struct mt_list *n = elt2.next, *p = elt2.prev; \
|
|
|
|
n->prev = p; \
|
|
|
|
p->next = n; \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
/* Equivalent of MT_LIST_DEL(), to be used when parsing the list with mt_list_entry_for_each_safe().
|
|
|
|
* It should be the element currently parsed (tmpelt1)
|
|
|
|
*/
|
2019-10-11 14:55:11 +00:00
|
|
|
#define MT_LIST_DEL_SAFE(_el) \
|
2019-08-12 12:10:12 +00:00
|
|
|
do { \
|
2019-10-11 14:55:11 +00:00
|
|
|
struct mt_list *el = (_el); \
|
2019-08-12 12:10:12 +00:00
|
|
|
(el)->prev = (el); \
|
|
|
|
(el)->next = (el); \
|
|
|
|
(el) = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources.
|
|
|
|
* Iterates <item> through a list of items of type "typeof(*item)" which are
|
|
|
|
* linked via a "struct list" member named <member>. A pointer to the head of
|
|
|
|
* the list is passed in <list_head>. A temporary variable <back> of same type
|
|
|
|
* as <item> is needed so that <item> may safely be deleted if needed.
|
|
|
|
* tmpelt1 is a temporary struct mt_list *, and tmpelt2 is a temporary
|
|
|
|
* struct mt_list, used internally, both are needed for MT_LIST_DEL_SAFE.
|
|
|
|
* Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list, elt1, elt2)
|
|
|
|
* { ... };
|
|
|
|
* If you want to remove the current element, please use MT_LIST_DEL_SAFE.
|
|
|
|
*/
|
|
|
|
#define mt_list_for_each_entry_safe(item, list_head, member, tmpelt, tmpelt2) \
|
|
|
|
for ((tmpelt) = NULL; (tmpelt) != MT_LIST_BUSY; ({ \
|
|
|
|
if (tmpelt) { \
|
|
|
|
if (tmpelt2.prev) \
|
|
|
|
MT_LIST_UNLOCK_ELT(tmpelt, tmpelt2); \
|
|
|
|
else \
|
|
|
|
_MT_LIST_UNLOCK_NEXT(tmpelt, tmpelt2.next); \
|
|
|
|
} else \
|
|
|
|
_MT_LIST_RELINK_DELETED(tmpelt2); \
|
|
|
|
(tmpelt) = MT_LIST_BUSY; \
|
2019-10-11 14:57:43 +00:00
|
|
|
})) \
|
2019-10-11 14:55:11 +00:00
|
|
|
for ((tmpelt) = (list_head), (tmpelt2).prev = NULL, (tmpelt2).next = _MT_LIST_LOCK_NEXT(tmpelt); ({ \
|
2019-08-12 12:10:12 +00:00
|
|
|
(item) = MT_LIST_ELEM((tmpelt2.next), typeof(item), member); \
|
|
|
|
if (&item->member != (list_head)) { \
|
|
|
|
if (tmpelt2.prev != &item->member) \
|
|
|
|
tmpelt2.next = _MT_LIST_LOCK_NEXT(&item->member); \
|
|
|
|
else \
|
|
|
|
tmpelt2.next = tmpelt; \
|
|
|
|
if (tmpelt != NULL) { \
|
|
|
|
if (tmpelt2.prev) \
|
|
|
|
_MT_LIST_UNLOCK_PREV(tmpelt, tmpelt2.prev); \
|
|
|
|
tmpelt2.prev = tmpelt; \
|
|
|
|
} \
|
|
|
|
(tmpelt) = &item->member; \
|
|
|
|
} \
|
|
|
|
}), \
|
|
|
|
&item->member != (list_head);)
|
2006-06-29 15:53:05 +00:00
|
|
|
#endif /* _COMMON_MINI_CLIST_H */
|