thr_select.c revision 71581
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 AUTHOR 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 * $FreeBSD: head/lib/libkse/thread/thr_select.c 71581 2001-01-24 13:03:38Z deischen $ 33 */ 34#include <unistd.h> 35#include <errno.h> 36#include <poll.h> 37#include <stdlib.h> 38#include <string.h> 39#include <sys/param.h> 40#include <sys/types.h> 41#include <sys/time.h> 42#include <sys/fcntl.h> 43#include <pthread.h> 44#include "pthread_private.h" 45 46#pragma weak select=_select 47 48int 49_select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, 50 struct timeval * timeout) 51{ 52 struct pthread *curthread = _get_curthread(); 53 struct timespec ts; 54 int i, ret = 0, f_wait = 1; 55 int pfd_index, got_one = 0, fd_count = 0; 56 struct pthread_poll_data data; 57 58 if (numfds > _thread_dtablesize) { 59 numfds = _thread_dtablesize; 60 } 61 /* Check if a timeout was specified: */ 62 if (timeout) { 63 if (timeout->tv_sec < 0 || 64 timeout->tv_usec < 0 || timeout->tv_usec >= 1000000) { 65 errno = EINVAL; 66 return (-1); 67 } 68 69 /* Convert the timeval to a timespec: */ 70 TIMEVAL_TO_TIMESPEC(timeout, &ts); 71 72 /* Set the wake up time: */ 73 _thread_kern_set_timeout(&ts); 74 if (ts.tv_sec == 0 && ts.tv_nsec == 0) 75 f_wait = 0; 76 } else { 77 /* Wait for ever: */ 78 _thread_kern_set_timeout(NULL); 79 } 80 81 /* Count the number of file descriptors to be polled: */ 82 if (readfds || writefds || exceptfds) { 83 for (i = 0; i < numfds; i++) { 84 if ((readfds && FD_ISSET(i, readfds)) || 85 (exceptfds && FD_ISSET(i, exceptfds)) || 86 (writefds && FD_ISSET(i, writefds))) { 87 fd_count++; 88 } 89 } 90 } 91 92 /* 93 * Allocate memory for poll data if it hasn't already been 94 * allocated or if previously allocated memory is insufficient. 95 */ 96 if ((curthread->poll_data.fds == NULL) || 97 (curthread->poll_data.nfds < fd_count)) { 98 data.fds = (struct pollfd *) realloc(curthread->poll_data.fds, 99 sizeof(struct pollfd) * MAX(128, fd_count)); 100 if (data.fds == NULL) { 101 errno = ENOMEM; 102 ret = -1; 103 } 104 else { 105 /* 106 * Note that the threads poll data always 107 * indicates what is allocated, not what is 108 * currently being polled. 109 */ 110 curthread->poll_data.fds = data.fds; 111 curthread->poll_data.nfds = MAX(128, fd_count); 112 } 113 } 114 if (ret == 0) { 115 /* Setup the wait data. */ 116 data.fds = curthread->poll_data.fds; 117 data.nfds = fd_count; 118 119 /* 120 * Setup the array of pollfds. Optimize this by 121 * running the loop in reverse and stopping when 122 * the number of selected file descriptors is reached. 123 */ 124 for (i = numfds - 1, pfd_index = fd_count - 1; 125 (i >= 0) && (pfd_index >= 0); i--) { 126 data.fds[pfd_index].events = 0; 127 if (readfds && FD_ISSET(i, readfds)) { 128 data.fds[pfd_index].events = POLLRDNORM; 129 } 130 if (exceptfds && FD_ISSET(i, exceptfds)) { 131 data.fds[pfd_index].events |= POLLRDBAND; 132 } 133 if (writefds && FD_ISSET(i, writefds)) { 134 data.fds[pfd_index].events |= POLLWRNORM; 135 } 136 if (data.fds[pfd_index].events != 0) { 137 /* 138 * Set the file descriptor to be polled and 139 * clear revents in case of a timeout which 140 * leaves fds unchanged: 141 */ 142 data.fds[pfd_index].fd = i; 143 data.fds[pfd_index].revents = 0; 144 pfd_index--; 145 } 146 } 147 if (((ret = __sys_poll(data.fds, data.nfds, 0)) == 0) && 148 (f_wait != 0)) { 149 curthread->data.poll_data = &data; 150 curthread->interrupted = 0; 151 _thread_kern_sched_state(PS_SELECT_WAIT, __FILE__, __LINE__); 152 if (curthread->interrupted) { 153 errno = EINTR; 154 data.nfds = 0; 155 ret = -1; 156 } else 157 ret = data.nfds; 158 } 159 } 160 161 if (ret >= 0) { 162 numfds = 0; 163 for (i = 0; i < fd_count; i++) { 164 /* 165 * Check the results of the poll and clear 166 * this file descriptor from the fdset if 167 * the requested event wasn't ready. 168 */ 169 got_one = 0; 170 if (readfds != NULL) { 171 if (FD_ISSET(data.fds[i].fd, readfds)) { 172 if (data.fds[i].revents & (POLLIN | 173 POLLRDNORM)) 174 got_one = 1; 175 else 176 FD_CLR(data.fds[i].fd, readfds); 177 } 178 } 179 if (writefds != NULL) { 180 if (FD_ISSET(data.fds[i].fd, writefds)) { 181 if (data.fds[i].revents & (POLLOUT | 182 POLLWRNORM | POLLWRBAND)) 183 got_one = 1; 184 else 185 FD_CLR(data.fds[i].fd, 186 writefds); 187 } 188 } 189 if (exceptfds != NULL) { 190 if (FD_ISSET(data.fds[i].fd, exceptfds)) { 191 if (data.fds[i].revents & (POLLRDBAND | 192 POLLPRI | POLLHUP | POLLERR | 193 POLLNVAL)) 194 got_one = 1; 195 else 196 FD_CLR(data.fds[i].fd, 197 exceptfds); 198 } 199 } 200 if (got_one) 201 numfds++; 202 } 203 ret = numfds; 204 } 205 206 return (ret); 207} 208