/*1* Copyright (c) 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#ifndef _THREADLIST_H_30#define _THREADLIST_H_313233struct thread; /* from <thread.h> */3435/*36* AmigaOS-style linked list of threads.37*38* The two threadlistnodes in the threadlist structure are always on39* the list, as bookends; this removes all the special cases in the40* list handling code. However, note how THREADLIST_FOREACH works: you41* iterate by starting with tl_head.tln_next, and stop when42* itervar->tln_next is null, not when itervar itself becomes null.43*44* ->tln_self always points to the thread that contains the45* threadlistnode. We could avoid this if we wanted to instead use46*47* (struct thread *)((char *)node - offsetof(struct thread, t_listnode))48*49* to get the thread pointer. But that's gross.50*/5152struct threadlistnode {53struct threadlistnode *tln_prev;54struct threadlistnode *tln_next;55struct thread *tln_self;56};5758struct threadlist {59struct threadlistnode tl_head;60struct threadlistnode tl_tail;61unsigned tl_count;62};6364/* Initialize and clean up a thread list node. */65void threadlistnode_init(struct threadlistnode *tln, struct thread *self);66void threadlistnode_cleanup(struct threadlistnode *tln);6768/* Initialize and clean up a thread list. Must be empty at cleanup. */69void threadlist_init(struct threadlist *tl);70void threadlist_cleanup(struct threadlist *tl);7172/* Check if it's empty */73bool threadlist_isempty(struct threadlist *tl);7475/* Add and remove: at ends */76void threadlist_addhead(struct threadlist *tl, struct thread *t);77void threadlist_addtail(struct threadlist *tl, struct thread *t);78struct thread *threadlist_remhead(struct threadlist *tl);79struct thread *threadlist_remtail(struct threadlist *tl);8081/* Add and remove: in middle. (TL is needed to maintain ->tl_count.) */82void threadlist_insertafter(struct threadlist *tl,83struct thread *onlist, struct thread *addee);84void threadlist_insertbefore(struct threadlist *tl,85struct thread *addee, struct thread *onlist);86void threadlist_remove(struct threadlist *tl, struct thread *t);8788/* Iteration; itervar should previously be declared as (struct thread *) */89#define THREADLIST_FORALL(itervar, tl) \90for ((itervar) = (tl).tl_head.tln_next->tln_self; \91(itervar)->t_listnode.tln_next != NULL; \92(itervar) = (itervar)->t_listnode.tln_next->tln_self)9394#define THREADLIST_FORALL_REV(itervar, tl) \95for ((itervar) = (tl).tl_tail.tln_prev->tln_self; \96(itervar)->t_listnode.tln_prev != NULL; \97(itervar) = (itervar)->t_listnode.tln_prev->tln_self)9899100#endif /* _THREADLIST_H_ */101102103