poll.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 #include "asterisk.h"
00075
00076 #include <unistd.h>
00077 #include <sys/types.h>
00078 #include <sys/time.h>
00079 #include <assert.h>
00080 #include <string.h>
00081
00082 #include "asterisk/poll-compat.h"
00083
00084 #ifdef AST_POLL_COMPAT
00085
00086
00087
00088
00089
00090 static int map_poll_spec
00091 #if __STDC__ > 0
00092 (struct pollfd *pArray,
00093 unsigned long n_fds,
00094 fd_set *pReadSet,
00095 fd_set *pWriteSet,
00096 fd_set *pExceptSet)
00097 #else
00098 (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00099 struct pollfd *pArray;
00100 unsigned long n_fds;
00101 fd_set *pReadSet;
00102 fd_set *pWriteSet;
00103 fd_set *pExceptSet;
00104 #endif
00105 {
00106 register unsigned long i;
00107 register struct pollfd *pCur;
00108 register int max_fd = -1;
00109
00110
00111
00112
00113
00114 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00115
00116
00117 if (pCur->fd < 0)
00118 continue;
00119
00120 if (pCur->events & POLLIN) {
00121
00122 FD_SET(pCur->fd, pReadSet);
00123 }
00124
00125 if (pCur->events & POLLOUT) {
00126
00127 FD_SET(pCur->fd, pWriteSet);
00128 }
00129
00130 if (pCur->events & POLLPRI) {
00131
00132
00133
00134
00135 FD_SET(pCur->fd, pExceptSet);
00136 }
00137
00138 max_fd = MAX(max_fd, pCur->fd);
00139 }
00140
00141 return max_fd;
00142 }
00143
00144 static struct timeval *map_timeout
00145 #if __STDC__ > 0
00146 (int poll_timeout, struct timeval *pSelTimeout)
00147 #else
00148 (poll_timeout, pSelTimeout)
00149 int poll_timeout;
00150 struct timeval *pSelTimeout;
00151 #endif
00152 {
00153 struct timeval *pResult;
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 assert(pSelTimeout != (struct timeval *) NULL);
00171
00172 switch (poll_timeout) {
00173 case -1:
00174
00175
00176
00177 pResult = (struct timeval *) NULL;
00178 break;
00179
00180 case 0:
00181
00182
00183
00184
00185 pSelTimeout->tv_sec = 0;
00186 pSelTimeout->tv_usec = 0;
00187 pResult = pSelTimeout;
00188 break;
00189
00190 default:
00191
00192 pSelTimeout->tv_sec = poll_timeout / 1000;
00193 poll_timeout %= 1000;
00194 pSelTimeout->tv_usec = poll_timeout * 1000;
00195 pResult = pSelTimeout;
00196 break;
00197 }
00198
00199 return pResult;
00200 }
00201
00202 static void map_select_results
00203 #if __STDC__ > 0
00204 (struct pollfd *pArray,
00205 unsigned long n_fds,
00206 fd_set *pReadSet,
00207 fd_set *pWriteSet,
00208 fd_set *pExceptSet)
00209 #else
00210 (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00211 struct pollfd *pArray;
00212 unsigned long n_fds;
00213 fd_set *pReadSet;
00214 fd_set *pWriteSet;
00215 fd_set *pExceptSet;
00216 #endif
00217 {
00218 register unsigned long i;
00219 register struct pollfd *pCur;
00220
00221 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00222
00223
00224 if (pCur->fd < 0) {
00225 continue;
00226 }
00227
00228
00229
00230 pCur->revents = 0;
00231 if (FD_ISSET (pCur->fd, pExceptSet)) {
00232 pCur->revents |= POLLPRI;
00233 } else if (FD_ISSET (pCur->fd, pReadSet)) {
00234 pCur->revents |= POLLIN;
00235 }
00236
00237 if (FD_ISSET (pCur->fd, pWriteSet)) {
00238 pCur->revents |= POLLOUT;
00239 }
00240 }
00241
00242 return;
00243 }
00244
00245
00246
00247
00248
00249 int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
00250 {
00251 fd_set read_descs;
00252 fd_set write_descs;
00253 fd_set except_descs;
00254 struct timeval stime;
00255 int ready_descriptors;
00256 int max_fd = 0;
00257 struct timeval *pTimeout;
00258
00259 FD_ZERO (&read_descs);
00260 FD_ZERO (&write_descs);
00261 FD_ZERO (&except_descs);
00262
00263
00264
00265 if (pArray) {
00266 max_fd = map_poll_spec (pArray, n_fds,
00267 &read_descs, &write_descs, &except_descs);
00268 }
00269
00270
00271 pTimeout = map_timeout(timeout, &stime);
00272
00273
00274 ready_descriptors = select(max_fd + 1, &read_descs, &write_descs,
00275 &except_descs, pTimeout);
00276
00277 if (ready_descriptors >= 0) {
00278 map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
00279 }
00280
00281 return ready_descriptors;
00282 }
00283
00284 #endif