/* [<][>][^][v][top][bottom][index][help] */
1 /*
2 * Copyright (c) 2009
3 * The President and Fellows of Harvard College.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifndef _THREADLIST_H_
31 #define _THREADLIST_H_
32
33
34 struct thread; /* from <thread.h> */
35
36 /*
37 * AmigaOS-style linked list of threads.
38 *
39 * The two threadlistnodes in the threadlist structure are always on
40 * the list, as bookends; this removes all the special cases in the
41 * list handling code. However, note how THREADLIST_FOREACH works: you
42 * iterate by starting with tl_head.tln_next, and stop when
43 * itervar->tln_next is null, not when itervar itself becomes null.
44 *
45 * ->tln_self always points to the thread that contains the
46 * threadlistnode. We could avoid this if we wanted to instead use
47 *
48 * (struct thread *)((char *)node - offsetof(struct thread, t_listnode))
49 *
50 * to get the thread pointer. But that's gross.
51 */
52
53 struct threadlistnode {
54 struct threadlistnode *tln_prev;
55 struct threadlistnode *tln_next;
56 struct thread *tln_self;
57 };
58
59 struct threadlist {
60 struct threadlistnode tl_head;
61 struct threadlistnode tl_tail;
62 unsigned tl_count;
63 };
64
65 /* Initialize and clean up a thread list node. */
66 void threadlistnode_init(struct threadlistnode *tln, struct thread *self);
67 void threadlistnode_cleanup(struct threadlistnode *tln);
68
69 /* Initialize and clean up a thread list. Must be empty at cleanup. */
70 void threadlist_init(struct threadlist *tl);
71 void threadlist_cleanup(struct threadlist *tl);
72
73 /* Check if it's empty */
74 bool threadlist_isempty(struct threadlist *tl);
75
76 /* Add and remove: at ends */
77 void threadlist_addhead(struct threadlist *tl, struct thread *t);
78 void threadlist_addtail(struct threadlist *tl, struct thread *t);
79 struct thread *threadlist_remhead(struct threadlist *tl);
80 struct thread *threadlist_remtail(struct threadlist *tl);
81
82 /* Add and remove: in middle. (TL is needed to maintain ->tl_count.) */
83 void threadlist_insertafter(struct threadlist *tl,
84 struct thread *onlist, struct thread *addee);
85 void threadlist_insertbefore(struct threadlist *tl,
86 struct thread *addee, struct thread *onlist);
87 void threadlist_remove(struct threadlist *tl, struct thread *t);
88
89 /* Iteration; itervar should previously be declared as (struct thread *) */
90 #define THREADLIST_FORALL(itervar, tl) \
91 for ((itervar) = (tl).tl_head.tln_next->tln_self; \
92 (itervar)->t_listnode.tln_next != NULL; \
93 (itervar) = (itervar)->t_listnode.tln_next->tln_self)
94
95 #define THREADLIST_FORALL_REV(itervar, tl) \
96 for ((itervar) = (tl).tl_tail.tln_prev->tln_self; \
97 (itervar)->t_listnode.tln_prev != NULL; \
98 (itervar) = (itervar)->t_listnode.tln_prev->tln_self)
99
100
101 #endif /* _THREADLIST_H_ */