Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 2 Jul 2002 09:54:02 -0500
From:      Jonathan Lemon <jlemon@flugsvamp.com>
To:        current@freebsd.org
Subject:   additional queue macro
Message-ID:  <20020702095402.D1020@prism.flugsvamp.com>

next in thread | raw e-mail | index | archive | help
What do people think about adding the following macro to <sys/queue.h>?
(I don't care much about the name, just the functionality)

    #define TAILQ_FOREACH_TMP(var, tmp, head, field)                     \
            for ((var) = TAILQ_FIRST((head));                            \
                (var) && (((tmp) = TAILQ_NEXT((var), field)) || 1);      \
                (var) = (tmp))

Essentially, this provides a traversal of the tailq that is safe 
from element removal, while being simple to drop in to those sections
of the code that need updating, as evidenced in the patch below.
-- 
Jonathan


Index: uthread_kern.c
===================================================================
RCS file: /ncvs/src/lib/libc_r/uthread/uthread_kern.c,v
retrieving revision 1.40
diff -u -r1.40 uthread_kern.c
--- uthread_kern.c	2002/02/09 19:58:41	1.40
+++ uthread_kern.c	2002/07/02 14:52:00
@@ -664,7 +664,7 @@
 	int		kern_pipe_added = 0;
 	int             nfds = 0;
 	int		timeout_ms = 0;
-	struct pthread	*pthread;
+	struct pthread	*pthread, *pthread_next;
 	struct timespec ts;
 	struct timeval  tv;
 
@@ -746,7 +746,7 @@
 	}
 
 	PTHREAD_WAITQ_SETACTIVE();
-	TAILQ_FOREACH(pthread, &_workq, qe) {
+	TAILQ_FOREACH_TMP(pthread, pthread_next, &_workq, qe) {
 		switch (pthread->state) {
 		case PS_SPINBLOCK:
 			/*
@@ -858,7 +858,7 @@
 		 * _poll syscall:
 		 */
 		PTHREAD_WAITQ_SETACTIVE();
-		TAILQ_FOREACH(pthread, &_workq, qe) {
+		TAILQ_FOREACH_TMP(pthread, pthread_next, &_workq, qe) {
 			switch (pthread->state) {
 			case PS_SPINBLOCK:
 				/*
@@ -947,7 +947,7 @@
 		 * that is now available.
 		 */
 		PTHREAD_WAITQ_SETACTIVE();
-		TAILQ_FOREACH(pthread, &_workq, qe) {
+		TAILQ_FOREACH_TMP(pthread, pthread_next, &_workq, qe) {
 			if (pthread->state == PS_SPINBLOCK) {
 				/*
 				 * If the lock is available, let the thread run.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020702095402.D1020>