[dovecot-cvs] dovecot/src/lib ioloop-select.c,1.23,1.24

tss at dovecot.org tss at dovecot.org
Wed Mar 7 17:57:43 EET 2007


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv376

Modified Files:
	ioloop-select.c 
Log Message:
Cleanups. Moved static variables inside ioloop context so it works with
multiple ioloops.



Index: ioloop-select.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/ioloop-select.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- ioloop-select.c	12 Dec 2005 17:55:04 -0000	1.23
+++ ioloop-select.c	7 Mar 2007 15:57:41 -0000	1.24
@@ -1,4 +1,4 @@
-/* Copyright (c) 2002-2003 Timo Sirainen */
+/* Copyright (c) 2002-2007 Timo Sirainen */
 
 #include "lib.h"
 #include "ioloop-internal.h"
@@ -14,37 +14,39 @@
 struct ioloop_handler_context {
 	int highest_fd;
 	fd_set read_fds, write_fds, except_fds;
+	fd_set tmp_read_fds, tmp_write_fds, tmp_except_fds;
 };
 
-static fd_set tmp_read_fds, tmp_write_fds, tmp_except_fds;
-
 static void update_highest_fd(struct ioloop *ioloop)
 {
+	struct ioloop_handler_context *ctx = ioloop->handler_context;
         struct io *io;
 	int max_highest_fd;
 
-        max_highest_fd = ioloop->handler_context->highest_fd-1;
-	ioloop->handler_context->highest_fd = -1;
+        max_highest_fd = ctx->highest_fd-1;
+	ctx->highest_fd = -1;
 
 	for (io = ioloop->ios; io != NULL; io = io->next) {
-		if (io->fd <= ioloop->handler_context->highest_fd)
+		if (io->fd <= ctx->highest_fd)
 			continue;
 
-		ioloop->handler_context->highest_fd = io->fd;
+		ctx->highest_fd = io->fd;
 
-		if (ioloop->handler_context->highest_fd == max_highest_fd)
+		if (ctx->highest_fd == max_highest_fd)
 			break;
 	}
 }
 
 void io_loop_handler_init(struct ioloop *ioloop)
 {
-	ioloop->handler_context =
+	struct ioloop_handler_context *ctx;
+
+	ioloop->handler_context = ctx =
 		p_new(ioloop->pool, struct ioloop_handler_context, 1);
-	ioloop->handler_context->highest_fd = -1;
-        FD_ZERO(&ioloop->handler_context->read_fds);
-	FD_ZERO(&ioloop->handler_context->write_fds);
-	FD_ZERO(&ioloop->handler_context->except_fds);
+	ctx->highest_fd = -1;
+        FD_ZERO(&ctx->read_fds);
+	FD_ZERO(&ctx->write_fds);
+	FD_ZERO(&ctx->except_fds);
 }
 
 void io_loop_handler_deinit(struct ioloop *ioloop)
@@ -54,6 +56,7 @@
 
 void io_loop_handle_add(struct ioloop *ioloop, struct io *io)
 {
+	struct ioloop_handler_context *ctx = ioloop->handler_context;
 	enum io_condition condition = io->condition;
 	int fd = io->fd;
 
@@ -63,44 +66,45 @@
 		i_fatal("fd %d too large for select()", fd);
 
         if (condition & IO_READ)
-		FD_SET(fd, &ioloop->handler_context->read_fds);
+		FD_SET(fd, &ctx->read_fds);
         if (condition & IO_WRITE)
-		FD_SET(fd, &ioloop->handler_context->write_fds);
-	FD_SET(fd, &ioloop->handler_context->except_fds);
+		FD_SET(fd, &ctx->write_fds);
+	FD_SET(fd, &ctx->except_fds);
 
-	if (io->fd > ioloop->handler_context->highest_fd)
-		ioloop->handler_context->highest_fd = io->fd;
+	if (io->fd > ctx->highest_fd)
+		ctx->highest_fd = io->fd;
 }
 
 void io_loop_handle_remove(struct ioloop *ioloop, struct io *io)
 {
+	struct ioloop_handler_context *ctx = ioloop->handler_context;
 	enum io_condition condition = io->condition;
 	int fd = io->fd;
 
 	i_assert(fd >= 0 && fd < FD_SETSIZE);
 
         if (condition & IO_READ)
-		FD_CLR(fd, &ioloop->handler_context->read_fds);
+		FD_CLR(fd, &ctx->read_fds);
         if (condition & IO_WRITE)
-		FD_CLR(fd, &ioloop->handler_context->write_fds);
+		FD_CLR(fd, &ctx->write_fds);
 
-	if (!FD_ISSET(fd, &ioloop->handler_context->read_fds) &&
-	    !FD_ISSET(fd, &ioloop->handler_context->write_fds)) {
-		FD_CLR(fd, &ioloop->handler_context->except_fds);
+	if (!FD_ISSET(fd, &ctx->read_fds) && !FD_ISSET(fd, &ctx->write_fds)) {
+		FD_CLR(fd, &ctx->except_fds);
 
 		/* check if we removed the highest fd */
-		if (io->fd == ioloop->handler_context->highest_fd)
+		if (io->fd == ctx->highest_fd)
 			update_highest_fd(ioloop);
 	}
 }
 
-#define io_check_condition(fd, condition) \
-	((FD_ISSET((fd), &tmp_read_fds) && ((condition) & IO_READ)) || \
-	 (FD_ISSET((fd), &tmp_write_fds) && ((condition) & IO_WRITE)) || \
-	 (FD_ISSET((fd), &tmp_except_fds)))
+#define io_check_condition(ctx, fd, cond) \
+	((FD_ISSET((fd), &(ctx)->tmp_read_fds) && ((cond) & IO_READ)) || \
+	 (FD_ISSET((fd), &(ctx)->tmp_write_fds) && ((cond) & IO_WRITE)) || \
+	 (FD_ISSET((fd), &(ctx)->tmp_except_fds)))
 
 void io_loop_handler_run(struct ioloop *ioloop)
 {
+	struct ioloop_handler_context *ctx = ioloop->handler_context;
 	struct timeval tv;
 	struct io *io;
 	unsigned int t_id;
@@ -109,15 +113,12 @@
 	/* get the time left for next timeout task */
 	io_loop_get_wait_time(ioloop->timeouts, &tv, NULL);
 
-	memcpy(&tmp_read_fds, &ioloop->handler_context->read_fds,
-	       sizeof(fd_set));
-	memcpy(&tmp_write_fds, &ioloop->handler_context->write_fds,
-	       sizeof(fd_set));
-	memcpy(&tmp_except_fds, &ioloop->handler_context->except_fds,
-	       sizeof(fd_set));
+	memcpy(&ctx->tmp_read_fds, &ctx->read_fds, sizeof(fd_set));
+	memcpy(&ctx->tmp_write_fds, &ctx->write_fds, sizeof(fd_set));
+	memcpy(&ctx->tmp_except_fds, &ctx->except_fds, sizeof(fd_set));
 
-	ret = select(ioloop->handler_context->highest_fd + 1,
-		     &tmp_read_fds, &tmp_write_fds, &tmp_except_fds, &tv);
+	ret = select(ctx->highest_fd + 1, &ctx->tmp_read_fds,
+		     &ctx->tmp_write_fds, &ctx->tmp_except_fds, &tv);
 	if (ret < 0 && errno != EINTR)
 		i_warning("select() : %m");
 
@@ -132,7 +133,7 @@
 	for (io = ioloop->ios; io != NULL && ret > 0; io = ioloop->next_io) {
                 ioloop->next_io = io->next;
 
-		if (io_check_condition(io->fd, io->condition)) {
+		if (io_check_condition(ctx, io->fd, io->condition)) {
 			ret--;
 
 			t_id = t_push();



More information about the dovecot-cvs mailing list