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/utils.h"
00083 #include "asterisk/poll-compat.h"
00084
00085 #ifdef AST_POLL_COMPAT
00086
00087
00088
00089
00090
00091 static int map_poll_spec
00092 #if __STDC__ > 0
00093 (struct pollfd *pArray,
00094 unsigned long n_fds,
00095 fd_set *pReadSet,
00096 fd_set *pWriteSet,
00097 fd_set *pExceptSet)
00098 #else
00099 (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00100 struct pollfd *pArray;
00101 unsigned long n_fds;
00102 fd_set *pReadSet;
00103 fd_set *pWriteSet;
00104 fd_set *pExceptSet;
00105 #endif
00106 {
00107 register unsigned long i;
00108 register struct pollfd *pCur;
00109 register int max_fd = -1;
00110
00111
00112
00113
00114
00115 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00116
00117
00118 if (pCur->fd < 0)
00119 continue;
00120
00121 if (pCur->events & POLLIN) {
00122
00123 FD_SET(pCur->fd, pReadSet);
00124 }
00125
00126 if (pCur->events & POLLOUT) {
00127
00128 FD_SET(pCur->fd, pWriteSet);
00129 }
00130
00131 if (pCur->events & POLLPRI) {
00132
00133
00134
00135
00136 FD_SET(pCur->fd, pExceptSet);
00137 }
00138
00139 max_fd = MAX(max_fd, pCur->fd);
00140 }
00141
00142 return max_fd;
00143 }
00144
00145 static struct timeval *map_timeout
00146 #if __STDC__ > 0
00147 (int poll_timeout, struct timeval *pSelTimeout)
00148 #else
00149 (poll_timeout, pSelTimeout)
00150 int poll_timeout;
00151 struct timeval *pSelTimeout;
00152 #endif
00153 {
00154 struct timeval *pResult;
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 assert(pSelTimeout != (struct timeval *) NULL);
00172
00173 switch (poll_timeout) {
00174 case -1:
00175
00176
00177
00178 pResult = (struct timeval *) NULL;
00179 break;
00180
00181 case 0:
00182
00183
00184
00185
00186 pSelTimeout->tv_sec = 0;
00187 pSelTimeout->tv_usec = 0;
00188 pResult = pSelTimeout;
00189 break;
00190
00191 default:
00192
00193 pSelTimeout->tv_sec = poll_timeout / 1000;
00194 poll_timeout %= 1000;
00195 pSelTimeout->tv_usec = poll_timeout * 1000;
00196 pResult = pSelTimeout;
00197 break;
00198 }
00199
00200 return pResult;
00201 }
00202
00203 static void map_select_results
00204 #if __STDC__ > 0
00205 (struct pollfd *pArray,
00206 unsigned long n_fds,
00207 fd_set *pReadSet,
00208 fd_set *pWriteSet,
00209 fd_set *pExceptSet)
00210 #else
00211 (pArray, n_fds, pReadSet, pWriteSet, pExceptSet)
00212 struct pollfd *pArray;
00213 unsigned long n_fds;
00214 fd_set *pReadSet;
00215 fd_set *pWriteSet;
00216 fd_set *pExceptSet;
00217 #endif
00218 {
00219 register unsigned long i;
00220 register struct pollfd *pCur;
00221
00222 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00223
00224
00225 if (pCur->fd < 0) {
00226 continue;
00227 }
00228
00229
00230
00231 pCur->revents = 0;
00232 if (FD_ISSET (pCur->fd, pExceptSet)) {
00233 pCur->revents |= POLLPRI;
00234 } else if (FD_ISSET (pCur->fd, pReadSet)) {
00235 pCur->revents |= POLLIN;
00236 }
00237
00238 if (FD_ISSET (pCur->fd, pWriteSet)) {
00239 pCur->revents |= POLLOUT;
00240 }
00241 }
00242
00243 return;
00244 }
00245
00246
00247
00248
00249
00250 int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
00251 {
00252 fd_set read_descs;
00253 fd_set write_descs;
00254 fd_set except_descs;
00255 struct timeval stime;
00256 int ready_descriptors;
00257 int max_fd = 0;
00258 struct timeval *pTimeout;
00259
00260 FD_ZERO (&read_descs);
00261 FD_ZERO (&write_descs);
00262 FD_ZERO (&except_descs);
00263
00264
00265
00266 if (pArray) {
00267 max_fd = map_poll_spec (pArray, n_fds,
00268 &read_descs, &write_descs, &except_descs);
00269 }
00270
00271
00272 pTimeout = map_timeout(timeout, &stime);
00273
00274
00275 ready_descriptors = select(max_fd + 1, &read_descs, &write_descs,
00276 &except_descs, pTimeout);
00277
00278 if (ready_descriptors >= 0) {
00279 map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
00280 }
00281
00282 return ready_descriptors;
00283 }
00284
00285 #endif