Tcl Source Code

Artifact [d740f545a7]
Login

Artifact d740f545a7d193fad99112f04cd47d089307474f:

Attachment "0001-ifdef-reduction-USE_FIONBIO.patch" to ticket [1903339fff] added by jenglish 2008-02-28 02:24:00.
From 5fca914e4970a57bc6e5e1b91496e6952a45e258 Mon Sep 17 00:00:00 2001
From: Joe English <[email protected]>
Date: Wed, 27 Feb 2008 11:22:01 -0800
Subject: [PATCH] ifdef reduction - USE_FIONBIO

Consolidate all code conditionalized on -DUSE_FIONBIO into one place,
TclUnixSetBlockingMode() in unix/tclUnixCompat.c (new routine).

diff --git a/unix/tclUnixChan.c b/unix/tclUnixChan.c
index e0f7761..93c102a 100644
--- a/unix/tclUnixChan.c
+++ b/unix/tclUnixChan.c
@@ -376,29 +376,11 @@ FileBlockModeProc(
 				 * TCL_MODE_NONBLOCKING. */
 {
     FileState *fsPtr = (FileState *) instanceData;
-    int curStatus;
 
-#ifndef USE_FIONBIO
-    curStatus = fcntl(fsPtr->fd, F_GETFL);
-    if (mode == TCL_MODE_BLOCKING) {
-	CLEAR_BITS(curStatus, O_NONBLOCK);
-    } else {
-	SET_BITS(curStatus, O_NONBLOCK);
-    }
-    if (fcntl(fsPtr->fd, F_SETFL, curStatus) < 0) {
+    if (TclUnixSetBlockingMode(fsPtr->fd, mode) < 0) {
 	return errno;
     }
-    curStatus = fcntl(fsPtr->fd, F_GETFL);
-#else /* USE_FIONBIO */
-    if (mode == TCL_MODE_BLOCKING) {
-	curStatus = 0;
-    } else {
-	curStatus = 1;
-    }
-    if (ioctl(fsPtr->fd, (int) FIONBIO, &curStatus) < 0) {
-	return errno;
-    }
-#endif /* !USE_FIONBIO */
+
     return 0;
 }
 
@@ -1852,36 +1834,15 @@ TcpBlockModeProc(
 				 * TCL_MODE_NONBLOCKING. */
 {
     TcpState *statePtr = (TcpState *) instanceData;
-    int setting;
 
-#ifndef USE_FIONBIO
-    setting = fcntl(statePtr->fd, F_GETFL);
     if (mode == TCL_MODE_BLOCKING) {
 	CLEAR_BITS(statePtr->flags, TCP_ASYNC_SOCKET);
-	CLEAR_BITS(setting, O_NONBLOCK);
     } else {
 	SET_BITS(statePtr->flags, TCP_ASYNC_SOCKET);
-	SET_BITS(setting, O_NONBLOCK);
     }
-    if (fcntl(statePtr->fd, F_SETFL, setting) < 0) {
+    if (TclUnixSetBlockingMode(statePtr->fd, mode) < 0) {
 	return errno;
     }
-#else /* USE_FIONBIO */
-    if (mode == TCL_MODE_BLOCKING) {
-	CLEAR_BITS(statePtr->flags, TCP_ASYNC_SOCKET);
-	setting = 0;
-	if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) {
-	    return errno;
-	}
-    } else {
-	SET_BITS(statePtr->flags, TCP_ASYNC_SOCKET);
-	setting = 1;
-	if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) {
-	    return errno;
-	}
-    }
-#endif /* !USE_FIONBIO */
-
     return 0;
 }
 
@@ -1909,7 +1870,6 @@ WaitForConnect(
 {
     int timeOut;		/* How long to wait. */
     int state;			/* Of calling TclWaitForFile. */
-    int flags;			/* fcntl flags for the socket. */
 
     /*
      * If an asynchronous connect is in progress, attempt to wait for it to
@@ -1926,14 +1886,7 @@ WaitForConnect(
 	state = TclUnixWaitForFile(statePtr->fd,
 		TCL_WRITABLE | TCL_EXCEPTION, timeOut);
 	if (!(statePtr->flags & TCP_ASYNC_SOCKET)) {
-#ifndef USE_FIONBIO
-	    flags = fcntl(statePtr->fd, F_GETFL);
-	    CLEAR_BITS(flags, O_NONBLOCK);
-	    (void) fcntl(statePtr->fd, F_SETFL, flags);
-#else /* USE_FIONBIO */
-	    flags = 0;
-	    (void) ioctl(statePtr->fd, FIONBIO, &flags);
-#endif /* !USE_FIONBIO */
+	    (void) TclUnixSetBlockingMode(statePtr->fd, TCL_MODE_BLOCKING);
 	}
 	if (state & TCL_EXCEPTION) {
 	    return -1;
@@ -2419,14 +2372,7 @@ CreateSocket(
 	 */
 
 	if (async) {
-#ifndef USE_FIONBIO
-	    curState = fcntl(sock, F_GETFL);
-	    SET_BITS(curState, O_NONBLOCK);
-	    status = fcntl(sock, F_SETFL, curState);
-#else /* USE_FIONBIO */
-	    curState = 1;
-	    status = ioctl(sock, FIONBIO, &curState);
-#endif /* !USE_FIONBIO */
+	    status = TclUnixSetBlockingMode(sock, TCL_MODE_NONBLOCKING);
 	} else {
 	    status = 0;
 	}
@@ -2448,14 +2394,7 @@ CreateSocket(
 		 */
 
 		if (async) {
-#ifndef USE_FIONBIO
-		    curState = fcntl(sock, F_GETFL);
-		    CLEAR_BITS(curState, O_NONBLOCK);
-		    status = fcntl(sock, F_SETFL, curState);
-#else /* USE_FIONBIO */
-		    curState = 0;
-		    status = ioctl(sock, FIONBIO, &curState);
-#endif /* !USE_FIONBIO */
+		    status = TclUnixSetBlockingMode(sock, TCL_MODE_BLOCKING);
 		}
 	    }
 	}
diff --git a/unix/tclUnixCompat.c b/unix/tclUnixCompat.c
index 40caae2..48c813a 100644
--- a/unix/tclUnixCompat.c
+++ b/unix/tclUnixCompat.c
@@ -17,6 +17,39 @@
 #include <string.h>
 
 /*
+ *---------------------------------------------------------------------------
+ *
+ * TclUnixSetBlockingMode --
+ *
+ *	Set the blocking mode of a file descriptor.
+ *
+ * Results:
+ *
+ *	0 on success, -1 (with errno set) on error.
+ *
+ *---------------------------------------------------------------------------
+ */
+int
+TclUnixSetBlockingMode(
+    int fd,		/* File descriptor */
+    int mode)		/* TCL_MODE_BLOCKING or TCL_MODE_NONBLOCKING */
+{
+#ifndef USE_FIONBIO
+    int flags = fcntl(fd, F_GETFL);
+
+    if (mode == TCL_MODE_BLOCKING) {
+	flags &= ~O_NONBLOCK;
+    } else {
+	flags |= O_NONBLOCK;
+    }
+    return fcntl(fd, F_SETFL, flags);
+#else /* USE_FIONBIO */
+    int state = (mode == TCL_MODE_NONBLOCKING);
+    return ioctl(fd, FIONBIO, &state);
+#endif /* !USE_FIONBIO */
+}
+
+/*
  * Used to pad structures at size'd boundaries
  *
  * This macro assumes that the pointer 'buffer' was created from an aligned
diff --git a/unix/tclUnixNotfy.c b/unix/tclUnixNotfy.c
index 2574015..80dd43b 100644
--- a/unix/tclUnixNotfy.c
+++ b/unix/tclUnixNotfy.c
@@ -918,25 +918,12 @@ NotifierThreadProc(
 
     receivePipe = fds[0];
 
-#ifndef USE_FIONBIO
-    status = fcntl(receivePipe, F_GETFL);
-    status |= O_NONBLOCK;
-    if (fcntl(receivePipe, F_SETFL, status) < 0) {
+    if (TclUnixSetBlockingMode(receivePipe, TCL_MODE_NONBLOCKING) < 0) {
 	Tcl_Panic("NotifierThreadProc: could not make receive pipe non blocking");
     }
-    status = fcntl(fds[1], F_GETFL);
-    status |= O_NONBLOCK;
-    if (fcntl(fds[1], F_SETFL, status) < 0) {
+    if (TclUnixSetBlockingMode(fds[1], TCL_MODE_NONBLOCKING) < 0) {
 	Tcl_Panic("NotifierThreadProc: could not make trigger pipe non blocking");
     }
-#else
-    if (ioctl(receivePipe, (int) FIONBIO, &status) < 0) {
-	Tcl_Panic("NotifierThreadProc: could not make receive pipe non blocking");
-    }
-    if (ioctl(fds[1], (int) FIONBIO, &status) < 0) {
-	Tcl_Panic("NotifierThreadProc: could not make trigger pipe non blocking");
-    }
-#endif /* FIONBIO */
 
     /*
      * Install the write end of the pipe into the global variable.
diff --git a/unix/tclUnixPipe.c b/unix/tclUnixPipe.c
index cd7a0f2..f5d1139 100644
--- a/unix/tclUnixPipe.c
+++ b/unix/tclUnixPipe.c
@@ -841,61 +841,18 @@ PipeBlockModeProc(
 				 * TCL_MODE_BLOCKING or
 				 * TCL_MODE_NONBLOCKING. */
 {
-    PipeState *psPtr = (PipeState *) instanceData;
-    int curStatus;
-    int fd;
+    PipeState *psPtr = instanceData;
 
-#ifndef	USE_FIONBIO
     if (psPtr->inFile) {
-	fd = GetFd(psPtr->inFile);
-	curStatus = fcntl(fd, F_GETFL);
-	if (mode == TCL_MODE_BLOCKING) {
-	    curStatus &= (~(O_NONBLOCK));
-	} else {
-	    curStatus |= O_NONBLOCK;
-	}
-	if (fcntl(fd, F_SETFL, curStatus) < 0) {
+	if (TclUnixSetBlockingMode(GetFd(psPtr->inFile), mode) < 0) {
 	    return errno;
 	}
     }
     if (psPtr->outFile) {
-	fd = GetFd(psPtr->outFile);
-	curStatus = fcntl(fd, F_GETFL);
-	if (mode == TCL_MODE_BLOCKING) {
-	    curStatus &= (~(O_NONBLOCK));
-	} else {
-	    curStatus |= O_NONBLOCK;
-	}
-	if (fcntl(fd, F_SETFL, curStatus) < 0) {
-	    return errno;
-	}
-    }
-#endif	/* !FIONBIO */
-
-#ifdef	USE_FIONBIO
-    if (psPtr->inFile) {
-	fd = GetFd(psPtr->inFile);
-	if (mode == TCL_MODE_BLOCKING) {
-	    curStatus = 0;
-	} else {
-	    curStatus = 1;
-	}
-	if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) {
-	    return errno;
-	}
-    }
-    if (psPtr->outFile != NULL) {
-	fd = GetFd(psPtr->outFile);
-	if (mode == TCL_MODE_BLOCKING) {
-	    curStatus = 0;
-	} else {
-	    curStatus = 1;
-	}
-	if (ioctl(fd, (int) FIONBIO, &curStatus) < 0) {
+	if (TclUnixSetBlockingMode(GetFd(psPtr->outFile), mode) < 0) {
 	    return errno;
 	}
     }
-#endif	/* USE_FIONBIO */
 
     psPtr->isNonBlocking = (mode == TCL_MODE_NONBLOCKING);
 
diff --git a/unix/tclUnixPort.h b/unix/tclUnixPort.h
index dc0fb40..dbd2e28 100644
--- a/unix/tclUnixPort.h
+++ b/unix/tclUnixPort.h
@@ -122,6 +122,9 @@ typedef off_t		Tcl_SeekOffset;
 #	include	<sys/ioctl.h>	/* For FIONBIO. */
 #   endif
 #endif	/* USE_FIONBIO */
+
+MODULE_SCOPE int TclUnixSetBlockingMode(int fd, int mode);
+
 #include <utime.h>
 
 /*
-- 
1.5.0.2