From 6e9187684c687eab654ee37f1d4a9aa9caff97a2 Mon Sep 17 00:00:00 2001 From: jenglish Date: Thu, 28 Feb 2008 20:12:09 +0000 Subject: Consolidate all code conditionalized on -DUSE_FIONBIO into one place. New routine TclUnixSetBlockingMode(). --- unix/tclUnixChan.c | 75 +++++----------------------------------------------- unix/tclUnixCompat.c | 35 +++++++++++++++++++++++- unix/tclUnixNotfy.c | 19 +++---------- unix/tclUnixPipe.c | 51 +++-------------------------------- unix/tclUnixPort.h | 5 +++- 5 files changed, 52 insertions(+), 133 deletions(-) diff --git a/unix/tclUnixChan.c b/unix/tclUnixChan.c index e0f7761..3e1c72d 100644 --- a/unix/tclUnixChan.c +++ b/unix/tclUnixChan.c @@ -10,7 +10,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclUnixChan.c,v 1.90 2008/02/27 03:35:50 jenglish Exp $ + * RCS: @(#) $Id: tclUnixChan.c,v 1.91 2008/02/28 20:12:09 jenglish Exp $ */ #include "tclInt.h" /* Internal definitions for Tcl. */ @@ -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..c26bef5 100644 --- a/unix/tclUnixCompat.c +++ b/unix/tclUnixCompat.c @@ -6,7 +6,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclUnixCompat.c,v 1.13 2007/12/13 15:28:42 dgp Exp $ + * RCS: @(#) $Id: tclUnixCompat.c,v 1.14 2008/02/28 20:12:09 jenglish Exp $ * */ @@ -17,6 +17,39 @@ #include /* + *--------------------------------------------------------------------------- + * + * 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..dba5bd3 100644 --- a/unix/tclUnixNotfy.c +++ b/unix/tclUnixNotfy.c @@ -10,7 +10,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclUnixNotfy.c,v 1.32 2006/08/21 01:08:03 das Exp $ + * RCS: @(#) $Id: tclUnixNotfy.c,v 1.33 2008/02/28 20:12:09 jenglish Exp $ */ #include "tclInt.h" @@ -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..ab468b8 100644 --- a/unix/tclUnixPipe.c +++ b/unix/tclUnixPipe.c @@ -10,7 +10,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclUnixPipe.c,v 1.40 2007/12/13 15:28:42 dgp Exp $ + * RCS: @(#) $Id: tclUnixPipe.c,v 1.41 2008/02/28 20:12:09 jenglish Exp $ */ #include "tclInt.h" @@ -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..a7313ca 100644 --- a/unix/tclUnixPort.h +++ b/unix/tclUnixPort.h @@ -19,7 +19,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclUnixPort.h,v 1.62 2008/02/27 03:35:50 jenglish Exp $ + * RCS: @(#) $Id: tclUnixPort.h,v 1.63 2008/02/28 20:12:10 jenglish Exp $ */ #ifndef _TCLUNIXPORT @@ -122,6 +122,9 @@ typedef off_t Tcl_SeekOffset; # include /* For FIONBIO. */ # endif #endif /* USE_FIONBIO */ + +MODULE_SCOPE int TclUnixSetBlockingMode(int fd, int mode); + #include /* -- cgit v0.12