diff options
Diffstat (limited to 'src/io_sleep.c')
-rw-r--r-- | src/io_sleep.c | 171 |
1 files changed, 88 insertions, 83 deletions
diff --git a/src/io_sleep.c b/src/io_sleep.c index d4728c6..3116e44 100644 --- a/src/io_sleep.c +++ b/src/io_sleep.c @@ -8,7 +8,7 @@ of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -35,20 +35,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static ioentry_t entries[FD_SETSIZE]; static int numfds; -ioentry_t *IO_Add( int fd ) { +ioentry_t *IO_Add(int fd) +{ ioentry_t *e; #ifdef _WIN32 int i; - for( i = 0, e = entries; i < numfds; i++, e++ ) { - if( !e->inuse ) { + for (i = 0, e = entries; i < numfds; i++, e++) { + if (!e->inuse) { break; } } - if( i == numfds ) { - if( ++numfds > FD_SETSIZE ) { - Com_Error( ERR_FATAL, "%s: no more space for fd: %d", __func__, fd ); + if (i == numfds) { + if (++numfds > FD_SETSIZE) { + Com_Error(ERR_FATAL, "%s: no more space for fd: %d", __func__, fd); } } @@ -62,7 +63,7 @@ ioentry_t *IO_Add( int fd ) { CHECK_FD - if( fd >= numfds ) { + if (fd >= numfds) { numfds = fd + 1; } @@ -72,16 +73,17 @@ ioentry_t *IO_Add( int fd ) { return e; } -void IO_Remove( int fd ) { +void IO_Remove(int fd) +{ ioentry_t *e; int i; - e = IO_Get( fd ); - memset( e, 0, sizeof( *e ) ); + e = IO_Get(fd); + memset(e, 0, sizeof(*e)); - for( i = numfds - 1; i >= 0; i-- ) { + for (i = numfds - 1; i >= 0; i--) { e = &entries[i]; - if( e->inuse ) { + if (e->inuse) { break; } } @@ -89,21 +91,22 @@ void IO_Remove( int fd ) { numfds = i + 1; } -ioentry_t *IO_Get( int fd ) { +ioentry_t *IO_Get(int fd) +{ ioentry_t *e; #ifdef _WIN32 int i; - for( i = 0, e = entries; i < numfds; i++, e++ ) { - if( !e->inuse ) { + for (i = 0, e = entries; i < numfds; i++, e++) { + if (!e->inuse) { continue; } - if( e->fd == fd ) { + if (e->fd == fd) { return e; } } - - Com_Error( ERR_FATAL, "%s: fd not found: %d", __func__, fd ); + + Com_Error(ERR_FATAL, "%s: fd not found: %d", __func__, fd); #else CHECK_FD @@ -122,7 +125,8 @@ IO_Sleep Sleeps msec or until some file descriptor is ready ==================== */ -int IO_Sleep( int msec ) { +int IO_Sleep(int msec) +{ struct timeval timeout; fd_set rfd, wfd; #ifdef _WIN32 @@ -131,16 +135,16 @@ int IO_Sleep( int msec ) { ioentry_t *e; int i, ret; - if( !numfds ) { + if (!numfds) { // don't bother with select() - Sys_Sleep( msec ); + Sys_Sleep(msec); return 0; } - FD_ZERO( &rfd ); - FD_ZERO( &wfd ); + FD_ZERO(&rfd); + FD_ZERO(&wfd); #ifdef _WIN32 - FD_ZERO( &efd ); + FD_ZERO(&efd); #endif #ifdef _WIN32 @@ -149,64 +153,64 @@ int IO_Sleep( int msec ) { #define FD i #endif - for( i = 0, e = entries; i < numfds; i++, e++ ) { - if( !e->inuse ) { + for (i = 0, e = entries; i < numfds; i++, e++) { + if (!e->inuse) { continue; } e->canread = qfalse; - if( e->wantread ) { - FD_SET( FD, &rfd ); + if (e->wantread) { + FD_SET(FD, &rfd); } e->canwrite = qfalse; - if( e->wantwrite ) { - FD_SET( FD, &wfd ); + if (e->wantwrite) { + FD_SET(FD, &wfd); } #ifdef _WIN32 e->canexcept = qfalse; - if( e->wantexcept ) { - FD_SET( FD, &efd ); + if (e->wantexcept) { + FD_SET(FD, &efd); } #endif } timeout.tv_sec = msec / 1000; - timeout.tv_usec = ( msec % 1000 ) * 1000; + timeout.tv_usec = (msec % 1000) * 1000; - ret = select( numfds, &rfd, &wfd, + ret = select(numfds, &rfd, &wfd, #ifdef _WIN32 - &efd, + & efd, #else - NULL, + NULL, #endif - &timeout ); - if( ret == -1 ) { + & timeout); + if (ret == -1) { #ifdef _WIN32 // TODO: report WSA error #else - if( errno != EINTR ) { - Com_EPrintf( "%s: %s\n", __func__, strerror( errno ) ); + if (errno != EINTR) { + Com_EPrintf("%s: %s\n", __func__, strerror(errno)); } #endif return ret; } - if( !ret ) { + if (!ret) { return ret; } - for( i = 0; i < numfds; i++ ) { + for (i = 0; i < numfds; i++) { e = &entries[i]; - if( !e->inuse ) { + if (!e->inuse) { continue; } - if( e->wantread && FD_ISSET( FD, &rfd ) ) { + if (e->wantread && FD_ISSET(FD, &rfd)) { e->canread = qtrue; } - if( e->wantwrite && FD_ISSET( FD, &wfd ) ) { + if (e->wantwrite && FD_ISSET(FD, &wfd)) { e->canwrite = qtrue; } #ifdef _WIN32 - if( e->wantexcept && FD_ISSET( FD, &efd ) ) { + if (e->wantexcept && FD_ISSET(FD, &efd)) { e->canexcept = qtrue; } #endif @@ -224,7 +228,8 @@ IO_Sleep Sleeps msec or until some file descriptor from a given subset is ready ==================== */ -int IO_Sleepv( int msec, ... ) { +int IO_Sleepv(int msec, ...) +{ va_list argptr; struct timeval timeout; fd_set rfd, wfd; @@ -234,93 +239,93 @@ int IO_Sleepv( int msec, ... ) { ioentry_t *e; int i, ret; - if( !numfds ) { + if (!numfds) { // don't bother with select() - Sys_Sleep( msec ); + Sys_Sleep(msec); return 0; } - FD_ZERO( &rfd ); - FD_ZERO( &wfd ); + FD_ZERO(&rfd); + FD_ZERO(&wfd); #ifdef _WIN32 - FD_ZERO( &efd ); + FD_ZERO(&efd); #endif - va_start( argptr, msec ); - while( 1 ) { - i = va_arg( argptr, int ); - if( i == -1 ) { + va_start(argptr, msec); + while (1) { + i = va_arg(argptr, int); + if (i == -1) { break; } e = &entries[i]; - if( !e->inuse ) { + if (!e->inuse) { continue; } e->canread = qfalse; - if( e->wantread ) { - FD_SET( FD, &rfd ); + if (e->wantread) { + FD_SET(FD, &rfd); } e->canwrite = qfalse; - if( e->wantwrite ) { - FD_SET( FD, &wfd ); + if (e->wantwrite) { + FD_SET(FD, &wfd); } #ifdef _WIN32 e->canexcept = qfalse; - if( e->wantexcept ) { - FD_SET( FD, &efd ); + if (e->wantexcept) { + FD_SET(FD, &efd); } #endif } - va_end( argptr ); + va_end(argptr); timeout.tv_sec = msec / 1000; - timeout.tv_usec = ( msec % 1000 ) * 1000; + timeout.tv_usec = (msec % 1000) * 1000; - ret = select( numfds, &rfd, &wfd, + ret = select(numfds, &rfd, &wfd, #ifdef _WIN32 - &efd, + & efd, #else - NULL, + NULL, #endif - &timeout ); - if( ret == -1 ) { + & timeout); + if (ret == -1) { #ifdef _WIN32 // TODO: report WSA error #else - if( errno != EINTR ) { - Com_EPrintf( "%s: %s\n", __func__, strerror( errno ) ); + if (errno != EINTR) { + Com_EPrintf("%s: %s\n", __func__, strerror(errno)); } #endif return ret; } - if( !ret ) { + if (!ret) { return ret; } - va_start( argptr, msec ); - while( 1 ) { - i = va_arg( argptr, int ); - if( i == -1 ) { + va_start(argptr, msec); + while (1) { + i = va_arg(argptr, int); + if (i == -1) { break; } e = &entries[i]; - if( !e->inuse ) { + if (!e->inuse) { continue; } - if( e->wantread && FD_ISSET( FD, &rfd ) ) { + if (e->wantread && FD_ISSET(FD, &rfd)) { e->canread = qtrue; } - if( e->wantwrite && FD_ISSET( FD, &wfd ) ) { + if (e->wantwrite && FD_ISSET(FD, &wfd)) { e->canwrite = qtrue; } #ifdef _WIN32 - if( e->wantexcept && FD_ISSET( FD, &efd ) ) { + if (e->wantexcept && FD_ISSET(FD, &efd)) { e->canexcept = qtrue; } #endif } - va_end( argptr ); + va_end(argptr); return ret; } |