thr_select.c revision 35509
1/* 2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by John Birrell. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 */ 33#include <unistd.h> 34#include <errno.h> 35#include <string.h> 36#include <sys/types.h> 37#include <sys/time.h> 38#ifdef _THREAD_SAFE 39#include <pthread.h> 40#include "pthread_private.h" 41 42int 43select(int numfds, fd_set * readfds, fd_set * writefds, 44 fd_set * exceptfds, struct timeval * timeout) 45{ 46 fd_set read_locks, write_locks, rdwr_locks; 47 struct timespec ts; 48 struct timeval zero_timeout = {0, 0}; 49 int i, ret = 0, got_all_locks = 1; 50 struct pthread_select_data data; 51 52 if (numfds > _thread_dtablesize) { 53 numfds = _thread_dtablesize; 54 } 55 /* Check if a timeout was specified: */ 56 if (timeout) { 57 /* Convert the timeval to a timespec: */ 58 TIMEVAL_TO_TIMESPEC(timeout, &ts); 59 60 /* Set the wake up time: */ 61 _thread_kern_set_timeout(&ts); 62 } else { 63 /* Wait for ever: */ 64 _thread_kern_set_timeout(NULL); 65 } 66 67 FD_ZERO(&read_locks); 68 FD_ZERO(&write_locks); 69 FD_ZERO(&rdwr_locks); 70 71 /* lock readfds */ 72 if (readfds || writefds || exceptfds) { 73 for (i = 0; i < numfds; i++) { 74 if ((readfds && (FD_ISSET(i, readfds))) || (exceptfds && FD_ISSET(i, exceptfds))) { 75 if (writefds && FD_ISSET(i, writefds)) { 76 if ((ret = _thread_fd_lock(i, FD_RDWR, NULL, __FILE__, __LINE__)) != 0) { 77 got_all_locks = 0; 78 break; 79 } 80 FD_SET(i, &rdwr_locks); 81 } else { 82 if ((ret = _thread_fd_lock(i, FD_READ, NULL, __FILE__, __LINE__)) != 0) { 83 got_all_locks = 0; 84 break; 85 } 86 FD_SET(i, &read_locks); 87 } 88 } else { 89 if (writefds && FD_ISSET(i, writefds)) { 90 if ((ret = _thread_fd_lock(i, FD_WRITE, NULL, __FILE__, __LINE__)) != 0) { 91 got_all_locks = 0; 92 break; 93 } 94 FD_SET(i, &write_locks); 95 } 96 } 97 } 98 } 99 if (got_all_locks) { 100 data.nfds = numfds; 101 FD_ZERO(&data.readfds); 102 FD_ZERO(&data.writefds); 103 FD_ZERO(&data.exceptfds); 104 if (readfds != NULL) { 105 memcpy(&data.readfds, readfds, sizeof(data.readfds)); 106 } 107 if (writefds != NULL) { 108 memcpy(&data.writefds, writefds, sizeof(data.writefds)); 109 } 110 if (exceptfds != NULL) { 111 memcpy(&data.exceptfds, exceptfds, sizeof(data.exceptfds)); 112 } 113 if ((ret = _thread_sys_select(data.nfds, &data.readfds, &data.writefds, &data.exceptfds, &zero_timeout)) == 0) { 114 data.nfds = numfds; 115 FD_ZERO(&data.readfds); 116 FD_ZERO(&data.writefds); 117 FD_ZERO(&data.exceptfds); 118 if (readfds != NULL) { 119 memcpy(&data.readfds, readfds, sizeof(data.readfds)); 120 } 121 if (writefds != NULL) { 122 memcpy(&data.writefds, writefds, sizeof(data.writefds)); 123 } 124 if (exceptfds != NULL) { 125 memcpy(&data.exceptfds, exceptfds, sizeof(data.exceptfds)); 126 } 127 _thread_run->data.select_data = &data; 128 _thread_run->interrupted = 0; 129 _thread_kern_sched_state(PS_SELECT_WAIT, __FILE__, __LINE__); 130 if (_thread_run->interrupted) { 131 errno = EINTR; 132 ret = -1; 133 } else 134 ret = data.nfds; 135 } 136 } 137 /* clean up the locks */ 138 for (i = 0; i < numfds; i++) 139 if (FD_ISSET(i, &read_locks)) 140 _thread_fd_unlock(i, FD_READ); 141 for (i = 0; i < numfds; i++) 142 if (FD_ISSET(i, &rdwr_locks)) 143 _thread_fd_unlock(i, FD_RDWR); 144 for (i = 0; i < numfds; i++) 145 if (FD_ISSET(i, &write_locks)) 146 _thread_fd_unlock(i, FD_WRITE); 147 148 if (ret > 0) { 149 if (readfds != NULL) { 150 for (i = 0; i < numfds; i++) { 151 if (FD_ISSET(i, readfds) && 152 !FD_ISSET(i, &data.readfds)) { 153 FD_CLR(i, readfds); 154 } 155 } 156 } 157 if (writefds != NULL) { 158 for (i = 0; i < numfds; i++) { 159 if (FD_ISSET(i, writefds) && 160 !FD_ISSET(i, &data.writefds)) { 161 FD_CLR(i, writefds); 162 } 163 } 164 } 165 if (exceptfds != NULL) { 166 for (i = 0; i < numfds; i++) { 167 if (FD_ISSET(i, exceptfds) && 168 !FD_ISSET(i, &data.exceptfds)) { 169 FD_CLR(i, exceptfds); 170 } 171 } 172 } 173 } 174 175 return (ret); 176} 177#endif 178