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