2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / pthread / list.h
blob43186a2d51b1756474981b6385eb1aa3241189c5
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _LIST_H
21 #define _LIST_H 1
23 /* The definitions of this file are adopted from those which can be
24 found in the Linux kernel headers to enable people familiar with
25 the latter find their way in these sources as well. */
28 /* Basic type for the double-link list. */
29 typedef struct list_head
31 struct list_head *next;
32 struct list_head *prev;
33 } list_t;
36 /* Define a variable with the head and tail of the list. */
37 #define LIST_HEAD(name) \
38 list_t name = { &(name), &(name) }
40 /* Initialize a new list head. */
41 #define INIT_LIST_HEAD(ptr) \
42 (ptr)->next = (ptr)->prev = (ptr)
45 /* Add new element at the head of the list. */
46 static inline void
47 list_add (list_t *newp, list_t *head)
49 head->next->prev = newp;
50 newp->next = head->next;
51 newp->prev = head;
52 head->next = newp;
56 /* Add new element at the tail of the list. */
57 static inline void
58 list_add_tail (list_t *newp, list_t *head)
60 head->prev->next = newp;
61 newp->next = head;
62 newp->prev = head->prev;
63 head->prev = newp;
67 /* Remove element from list. */
68 static inline void
69 list_del (list_t *elem)
71 elem->next->prev = elem->prev;
72 elem->prev->next = elem->next;
76 /* Join two lists. */
77 static inline void
78 list_splice (list_t *add, list_t *head)
80 /* Do nothing if the list which gets added is empty. */
81 if (add != add->next)
83 add->next->prev = head;
84 add->prev->next = head->next;
85 head->next->prev = add->prev;
86 head->next = add->next;
91 /* Get typed element from list at a given position. */
92 #define list_entry(ptr, type, member) \
93 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
97 /* Iterate forward over the elements of the list. */
98 #define list_for_each(pos, head) \
99 for (pos = (head)->next; pos != (head); pos = pos->next)
102 /* Iterate forward over the elements of the list. */
103 #define list_for_each_prev(pos, head) \
104 for (pos = (head)->prev; pos != (head); pos = pos->prev)
107 /* Iterate backwards over the elements list. The list elements can be
108 removed from the list while doing this. */
109 #define list_for_each_prev_safe(pos, p, head) \
110 for (pos = (head)->prev, p = pos->prev; \
111 pos != (head); \
112 pos = p, p = pos->prev)
114 #endif /* list.h */