OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [sysdeps/] [linux/] [common/] [poll.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1994,1996,1997,1998,1999,2001,2002
2
   Free Software Foundation, Inc.
3
   This file is part of the GNU C Library.
4
 
5
   The GNU C Library is free software; you can redistribute it and/or
6
   modify it under the terms of the GNU Lesser General Public
7
   License as published by the Free Software Foundation; either
8
   version 2.1 of the License, or (at your option) any later version.
9
 
10
   The GNU C Library is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
   Lesser General Public License for more details.
14
 
15
   You should have received a copy of the GNU Lesser General Public
16
   License along with the GNU C Library; if not, write to the Free
17
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18
   02111-1307 USA.  */
19
 
20
#include <alloca.h>
21
#include <sys/poll.h>
22
#include <sys/types.h>
23
#include <errno.h>
24
#include <string.h>
25
#include <sys/time.h>
26
#include <sys/param.h>
27
#include <unistd.h>
28
 
29
/* Poll the file descriptors described by the NFDS structures starting at
30
   FDS.  If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
31
   an event to occur; if TIMEOUT is -1, block until an event occurs.
32
   Returns the number of file descriptors with events, zero if timed out,
33
   or -1 for errors.  */
34
 
35
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
36
{
37
    static int max_fd_size;
38
    struct timeval tv;
39
    fd_set *rset, *wset, *xset;
40
    struct pollfd *f;
41
    int ready;
42
    int maxfd = 0;
43
    int bytes;
44
 
45
    if (!max_fd_size)
46
        max_fd_size = getdtablesize ();
47
 
48
    bytes = howmany (max_fd_size, __NFDBITS);
49
    rset = alloca (bytes);
50
    wset = alloca (bytes);
51
    xset = alloca (bytes);
52
 
53
    /* We can't call FD_ZERO, since FD_ZERO only works with sets
54
       of exactly __FD_SETSIZE size.  */
55
    bzero (rset, bytes);
56
    bzero (wset, bytes);
57
    bzero (xset, bytes);
58
 
59
    for (f = fds; f < &fds[nfds]; ++f)
60
    {
61
        f->revents = 0;
62
        if (f->fd >= 0)
63
        {
64
            if (f->fd >= max_fd_size)
65
            {
66
                /* The user provides a file descriptor number which is higher
67
                   than the maximum we got from the `getdtablesize' call.
68
                   Maybe this is ok so enlarge the arrays.  */
69
                fd_set *nrset, *nwset, *nxset;
70
                int nbytes;
71
 
72
                max_fd_size = roundup (f->fd, __NFDBITS);
73
                nbytes = howmany (max_fd_size, __NFDBITS);
74
 
75
                nrset = alloca (nbytes);
76
                nwset = alloca (nbytes);
77
                nxset = alloca (nbytes);
78
 
79
                bzero ((char *) nrset + bytes, nbytes - bytes);
80
                bzero ((char *) nwset + bytes, nbytes - bytes);
81
                bzero ((char *) nxset + bytes, nbytes - bytes);
82
 
83
                rset = memcpy (nrset, rset, bytes);
84
                wset = memcpy (nwset, wset, bytes);
85
                xset = memcpy (nxset, xset, bytes);
86
 
87
                bytes = nbytes;
88
            }
89
 
90
            if (f->events & POLLIN)
91
                FD_SET (f->fd, rset);
92
            if (f->events & POLLOUT)
93
                FD_SET (f->fd, wset);
94
            if (f->events & POLLPRI)
95
                FD_SET (f->fd, xset);
96
            if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
97
                maxfd = f->fd;
98
        }
99
    }
100
 
101
    tv.tv_sec = timeout / 1000;
102
    tv.tv_usec = (timeout % 1000) * 1000;
103
 
104
    while (1)
105
    {
106
        ready = select (maxfd + 1, rset, wset, xset,
107
                timeout == -1 ? NULL : &tv);
108
 
109
        /* It might be that one or more of the file descriptors is invalid.
110
           We now try to find and mark them and then try again.  */
111
        if (ready == -1 && errno == EBADF)
112
        {
113
            fd_set *sngl_rset = alloca (bytes);
114
            fd_set *sngl_wset = alloca (bytes);
115
            fd_set *sngl_xset = alloca (bytes);
116
            struct timeval sngl_tv;
117
 
118
            /* Clear the original set.  */
119
            bzero (rset, bytes);
120
            bzero (wset, bytes);
121
            bzero (xset, bytes);
122
 
123
            /* This means we don't wait for input.  */
124
            sngl_tv.tv_sec = 0;
125
            sngl_tv.tv_usec = 0;
126
 
127
            maxfd = -1;
128
 
129
            /* Reset the return value.  */
130
            ready = 0;
131
 
132
            for (f = fds; f < &fds[nfds]; ++f)
133
                if (f->fd != -1 && (f->events & (POLLIN|POLLOUT|POLLPRI))
134
                        && (f->revents & POLLNVAL) == 0)
135
                {
136
                    int n;
137
 
138
                    bzero (sngl_rset, bytes);
139
                    bzero (sngl_wset, bytes);
140
                    bzero (sngl_xset, bytes);
141
 
142
                    if (f->events & POLLIN)
143
                        FD_SET (f->fd, sngl_rset);
144
                    if (f->events & POLLOUT)
145
                        FD_SET (f->fd, sngl_wset);
146
                    if (f->events & POLLPRI)
147
                        FD_SET (f->fd, sngl_xset);
148
 
149
                    n = select (f->fd + 1, sngl_rset, sngl_wset, sngl_xset,
150
                            &sngl_tv);
151
                    if (n != -1)
152
                    {
153
                        /* This descriptor is ok.  */
154
                        if (f->events & POLLIN)
155
                            FD_SET (f->fd, rset);
156
                        if (f->events & POLLOUT)
157
                            FD_SET (f->fd, wset);
158
                        if (f->events & POLLPRI)
159
                            FD_SET (f->fd, xset);
160
                        if (f->fd > maxfd)
161
                            maxfd = f->fd;
162
                        if (n > 0)
163
                            /* Count it as being available.  */
164
                            ++ready;
165
                    }
166
                    else if (errno == EBADF)
167
                        f->revents |= POLLNVAL;
168
                }
169
            /* Try again.  */
170
            continue;
171
        }
172
 
173
        break;
174
    }
175
 
176
    if (ready > 0)
177
        for (f = fds; f < &fds[nfds]; ++f)
178
        {
179
            if (f->fd >= 0)
180
            {
181
                if (FD_ISSET (f->fd, rset))
182
                    f->revents |= POLLIN;
183
                if (FD_ISSET (f->fd, wset))
184
                    f->revents |= POLLOUT;
185
                if (FD_ISSET (f->fd, xset))
186
                    f->revents |= POLLPRI;
187
            }
188
        }
189
 
190
    return ready;
191
}
192
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.