New feature optionally to accelerate auto-repeated scrolling.
[emacs.git] / lib / pselect.c
blob695a3e84832af401f37a0675e79ff7387f440bc2
1 /* pselect - synchronous I/O multiplexing
3 Copyright 2011-2014 Free Software Foundation, Inc.
5 This file is part of gnulib.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, see <http://www.gnu.org/licenses/>. */
20 /* written by Paul Eggert */
22 #include <config.h>
24 #include <sys/select.h>
26 #include <errno.h>
27 #include <signal.h>
29 /* Examine the size-NFDS file descriptor sets in RFDS, WFDS, and XFDS
30 to see whether some of their descriptors are ready for reading,
31 ready for writing, or have exceptions pending. Wait for at most
32 TIMEOUT seconds, and use signal mask SIGMASK while waiting. A null
33 pointer parameter stands for no descriptors, an infinite timeout,
34 or an unaffected signal mask. */
36 #if !HAVE_PSELECT
38 int
39 pselect (int nfds, fd_set *restrict rfds,
40 fd_set *restrict wfds, fd_set *restrict xfds,
41 struct timespec const *restrict timeout,
42 sigset_t const *restrict sigmask)
44 int select_result;
45 sigset_t origmask;
46 struct timeval tv, *tvp;
48 if (timeout)
50 if (! (0 <= timeout->tv_nsec && timeout->tv_nsec < 1000000000))
52 errno = EINVAL;
53 return -1;
56 tv.tv_sec = timeout->tv_sec;
57 tv.tv_usec = (timeout->tv_nsec + 999) / 1000;
58 tvp = &tv;
60 else
61 tvp = NULL;
63 /* Signal mask munging should be atomic, but this is the best we can
64 do in this emulation. */
65 if (sigmask)
66 pthread_sigmask (SIG_SETMASK, sigmask, &origmask);
68 select_result = select (nfds, rfds, wfds, xfds, tvp);
70 if (sigmask)
72 int select_errno = errno;
73 pthread_sigmask (SIG_SETMASK, &origmask, NULL);
74 errno = select_errno;
77 return select_result;
80 #else /* HAVE_PSELECT */
81 # include <unistd.h>
82 # undef pselect
84 int
85 rpl_pselect (int nfds, fd_set *restrict rfds,
86 fd_set *restrict wfds, fd_set *restrict xfds,
87 struct timespec const *restrict timeout,
88 sigset_t const *restrict sigmask)
90 int i;
92 /* FreeBSD 8.2 has a bug: it does not always detect invalid fds. */
93 if (nfds < 0 || nfds > FD_SETSIZE)
95 errno = EINVAL;
96 return -1;
98 for (i = 0; i < nfds; i++)
100 if (((rfds && FD_ISSET (i, rfds))
101 || (wfds && FD_ISSET (i, wfds))
102 || (xfds && FD_ISSET (i, xfds)))
103 && dup2 (i, i) != i)
104 return -1;
107 return pselect (nfds, rfds, wfds, xfds, timeout, sigmask);
110 #endif