00001 /* 00002 * Copyright (c) 2009 00003 * The President and Fellows of Harvard College. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the University nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 */ 00029 00030 #ifndef _THREADLIST_H_ 00031 #define _THREADLIST_H_ 00032 00033 00034 struct thread; /* from <thread.h> */ 00035 00036 /* 00037 * AmigaOS-style linked list of threads. 00038 * 00039 * The two threadlistnodes in the threadlist structure are always on 00040 * the list, as bookends; this removes all the special cases in the 00041 * list handling code. However, note how THREADLIST_FOREACH works: you 00042 * iterate by starting with tl_head.tln_next, and stop when 00043 * itervar->tln_next is null, not when itervar itself becomes null. 00044 * 00045 * ->tln_self always points to the thread that contains the 00046 * threadlistnode. We could avoid this if we wanted to instead use 00047 * 00048 * (struct thread *)((char *)node - offsetof(struct thread, t_listnode)) 00049 * 00050 * to get the thread pointer. But that's gross. 00051 */ 00052 00053 struct threadlistnode { 00054 struct threadlistnode *tln_prev; 00055 struct threadlistnode *tln_next; 00056 struct thread *tln_self; 00057 }; 00058 00059 struct threadlist { 00060 struct threadlistnode tl_head; 00061 struct threadlistnode tl_tail; 00062 unsigned tl_count; 00063 }; 00064 00065 /* Initialize and clean up a thread list node. */ 00066 void threadlistnode_init(struct threadlistnode *tln, struct thread *self); 00067 void threadlistnode_cleanup(struct threadlistnode *tln); 00068 00069 /* Initialize and clean up a thread list. Must be empty at cleanup. */ 00070 void threadlist_init(struct threadlist *tl); 00071 void threadlist_cleanup(struct threadlist *tl); 00072 00073 /* Check if it's empty */ 00074 bool threadlist_isempty(struct threadlist *tl); 00075 00076 /* Add and remove: at ends */ 00077 void threadlist_addhead(struct threadlist *tl, struct thread *t); 00078 void threadlist_addtail(struct threadlist *tl, struct thread *t); 00079 struct thread *threadlist_remhead(struct threadlist *tl); 00080 struct thread *threadlist_remtail(struct threadlist *tl); 00081 00082 /* Add and remove: in middle. (TL is needed to maintain ->tl_count.) */ 00083 void threadlist_insertafter(struct threadlist *tl, 00084 struct thread *onlist, struct thread *addee); 00085 void threadlist_insertbefore(struct threadlist *tl, 00086 struct thread *addee, struct thread *onlist); 00087 void threadlist_remove(struct threadlist *tl, struct thread *t); 00088 00089 /* Iteration; itervar should previously be declared as (struct thread *) */ 00090 #define THREADLIST_FORALL(itervar, tl) \ 00091 for ((itervar) = (tl).tl_head.tln_next->tln_self; \ 00092 (itervar)->t_listnode.tln_next != NULL; \ 00093 (itervar) = (itervar)->t_listnode.tln_next->tln_self) 00094 00095 #define THREADLIST_FORALL_REV(itervar, tl) \ 00096 for ((itervar) = (tl).tl_tail.tln_prev->tln_self; \ 00097 (itervar)->t_listnode.tln_prev != NULL; \ 00098 (itervar) = (itervar)->t_listnode.tln_prev->tln_self) 00099 00100 00101 #endif /* _THREADLIST_H_ */