Home | History | Annotate | Line # | Download | only in libpthread
pthread_queue.h revision 1.1.2.1
      1  1.1.2.1  nathanw /*	$Id: pthread_queue.h,v 1.1.2.1 2001/07/13 02:04:33 nathanw Exp $	*/
      2  1.1.2.1  nathanw 
      3  1.1.2.1  nathanw /*
      4  1.1.2.1  nathanw  * Copyright
      5  1.1.2.1  nathanw  */
      6  1.1.2.1  nathanw 
      7  1.1.2.1  nathanw /* pthread_queue.h
      8  1.1.2.1  nathanw  * Definition of a queue interface for the pthread library.
      9  1.1.2.1  nathanw  * Style modeled on the sys/queue.h macros; implementation taken from
     10  1.1.2.1  nathanw  * the tail queue, with the added property of static initializability
     11  1.1.2.1  nathanw  * (and a corresponding extra cost in the _INSERT_TAIL() function.
     12  1.1.2.1  nathanw */
     13  1.1.2.1  nathanw 
     14  1.1.2.1  nathanw 
     15  1.1.2.1  nathanw 
     16  1.1.2.1  nathanw /*
     17  1.1.2.1  nathanw  * Queue definitions.
     18  1.1.2.1  nathanw  */
     19  1.1.2.1  nathanw #define PTQ_HEAD(name, type)						\
     20  1.1.2.1  nathanw struct name {								\
     21  1.1.2.1  nathanw 	struct type *ptqh_first;/* first element */			\
     22  1.1.2.1  nathanw 	struct type **ptqh_last;/* addr of last next element */		\
     23  1.1.2.1  nathanw }
     24  1.1.2.1  nathanw 
     25  1.1.2.1  nathanw #define PTQ_HEAD_INITIALIZER { NULL, NULL }
     26  1.1.2.1  nathanw 
     27  1.1.2.1  nathanw 
     28  1.1.2.1  nathanw #define PTQ_ENTRY(type)						\
     29  1.1.2.1  nathanw struct {								\
     30  1.1.2.1  nathanw 	struct type *ptqe_next;	/* next element */			\
     31  1.1.2.1  nathanw 	struct type **ptqe_prev;/* address of previous next element */	\
     32  1.1.2.1  nathanw }
     33  1.1.2.1  nathanw 
     34  1.1.2.1  nathanw /*
     35  1.1.2.1  nathanw  * Queue functions.
     36  1.1.2.1  nathanw  */
     37  1.1.2.1  nathanw 
     38  1.1.2.1  nathanw #define	PTQ_INIT(head) do {						\
     39  1.1.2.1  nathanw 	(head)->ptqh_first = NULL;					\
     40  1.1.2.1  nathanw 	(head)->ptqh_last = &(head)->ptqh_first;			\
     41  1.1.2.1  nathanw } while (/*CONSTCOND*/0)
     42  1.1.2.1  nathanw 
     43  1.1.2.1  nathanw #define PTQ_INSERT_HEAD(head, elm, field) do {			\
     44  1.1.2.1  nathanw 	if (((elm)->field.ptqe_next = (head)->ptqh_first) != NULL)	\
     45  1.1.2.1  nathanw 		(head)->ptqh_first->field.ptqe_prev =			\
     46  1.1.2.1  nathanw 		    &(elm)->field.ptqe_next;				\
     47  1.1.2.1  nathanw 	else								\
     48  1.1.2.1  nathanw 		(head)->ptqh_last = &(elm)->field.ptqe_next;		\
     49  1.1.2.1  nathanw 	(head)->ptqh_first = (elm);					\
     50  1.1.2.1  nathanw 	(elm)->field.ptqe_prev = &(head)->ptqh_first;			\
     51  1.1.2.1  nathanw } while (/*CONSTCOND*/0)
     52  1.1.2.1  nathanw 
     53  1.1.2.1  nathanw #define PTQ_INSERT_TAIL(head, elm, field) do {				\
     54  1.1.2.1  nathanw 	(elm)->field.ptqe_next = NULL;					\
     55  1.1.2.1  nathanw 	if ((head)->ptqh_last == NULL)					\
     56  1.1.2.1  nathanw 		(head)->ptqh_last = &(head)->ptqh_first;		\
     57  1.1.2.1  nathanw 	(elm)->field.ptqe_prev = (head)->ptqh_last;			\
     58  1.1.2.1  nathanw 	*(head)->ptqh_last = (elm);					\
     59  1.1.2.1  nathanw 	(head)->ptqh_last = &(elm)->field.ptqe_next;			\
     60  1.1.2.1  nathanw } while (/*CONSTCOND*/0)
     61  1.1.2.1  nathanw 
     62  1.1.2.1  nathanw #define PTQ_INSERT_AFTER(head, listelm, elm, field) do {		\
     63  1.1.2.1  nathanw 	if (((elm)->field.ptqe_next = (listelm)->field.ptqe_next) != NULL)\
     64  1.1.2.1  nathanw 		(elm)->field.ptqe_next->field.ptqe_prev = 		\
     65  1.1.2.1  nathanw 		    &(elm)->field.ptqe_next;				\
     66  1.1.2.1  nathanw 	else								\
     67  1.1.2.1  nathanw 		(head)->ptqh_last = &(elm)->field.ptqe_next;		\
     68  1.1.2.1  nathanw 	(listelm)->field.ptqe_next = (elm);				\
     69  1.1.2.1  nathanw 	(elm)->field.ptqe_prev = &(listelm)->field.ptqe_next;		\
     70  1.1.2.1  nathanw } while (/*CONSTCOND*/0)
     71  1.1.2.1  nathanw 
     72  1.1.2.1  nathanw #define	PTQ_INSERT_BEFORE(listelm, elm, field) do {			\
     73  1.1.2.1  nathanw 	(elm)->field.ptqe_prev = (listelm)->field.ptqe_prev;		\
     74  1.1.2.1  nathanw 	(elm)->field.ptqe_next = (listelm);				\
     75  1.1.2.1  nathanw 	*(listelm)->field.ptqe_prev = (elm);				\
     76  1.1.2.1  nathanw 	(listelm)->field.ptqe_prev = &(elm)->field.ptqe_next;		\
     77  1.1.2.1  nathanw } while (/*CONSTCOND*/0)
     78  1.1.2.1  nathanw 
     79  1.1.2.1  nathanw #define PTQ_REMOVE(head, elm, field) do {				\
     80  1.1.2.1  nathanw 	if (((elm)->field.ptqe_next) != NULL)				\
     81  1.1.2.1  nathanw 		(elm)->field.ptqe_next->field.ptqe_prev = 		\
     82  1.1.2.1  nathanw 		    (elm)->field.ptqe_prev;				\
     83  1.1.2.1  nathanw 	else								\
     84  1.1.2.1  nathanw 		(head)->ptqh_last = (elm)->field.ptqe_prev;		\
     85  1.1.2.1  nathanw 	*(elm)->field.ptqe_prev = (elm)->field.ptqe_next;		\
     86  1.1.2.1  nathanw } while (/*CONSTCOND*/0)
     87  1.1.2.1  nathanw 
     88  1.1.2.1  nathanw /*
     89  1.1.2.1  nathanw  * Queue access methods.
     90  1.1.2.1  nathanw  */
     91  1.1.2.1  nathanw #define	PTQ_EMPTY(head)		((head)->ptqh_first == NULL)
     92  1.1.2.1  nathanw #define	PTQ_FIRST(head)		((head)->ptqh_first)
     93  1.1.2.1  nathanw #define	PTQ_NEXT(elm, field)		((elm)->field.ptqe_next)
     94  1.1.2.1  nathanw 
     95  1.1.2.1  nathanw #define PTQ_LAST(head, headname) \
     96  1.1.2.1  nathanw 	(*(((struct headname *)((head)->ptqh_last))->ptqh_last))
     97  1.1.2.1  nathanw #define PTQ_PREV(elm, headname, field) \
     98  1.1.2.1  nathanw 	(*(((struct headname *)((elm)->field.ptqe_prev))->ptqh_last))
     99  1.1.2.1  nathanw 
    100  1.1.2.1  nathanw #define PTQ_FOREACH(var, head, field)					\
    101  1.1.2.1  nathanw 	for ((var) = ((head)->ptqh_first);				\
    102  1.1.2.1  nathanw 		(var);							\
    103  1.1.2.1  nathanw 		(var) = ((var)->field.ptqe_next))
    104  1.1.2.1  nathanw 
    105  1.1.2.1  nathanw #define PTQ_FOREACH_REVERSE(var, head, headname, field)		\
    106  1.1.2.1  nathanw 	for ((var) = (*(((struct headname *)((head)->ptqh_last))->ptqh_last));	\
    107  1.1.2.1  nathanw 		(var);							\
    108  1.1.2.1  nathanw 		(var) = (*(((struct headname *)((var)->field.ptqe_prev))->ptqh_last)))
    109