OLD | NEW |
1 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ | 1 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ |
2 | 2 |
3 /* | 3 /* |
4 * Copyright 2000-2003 Niels Provos <provos@citi.umich.edu> | 4 * Copyright 2000-2003 Niels Provos <provos@citi.umich.edu> |
5 * All rights reserved. | 5 * All rights reserved. |
6 * | 6 * |
7 * Redistribution and use in source and binary forms, with or without | 7 * Redistribution and use in source and binary forms, with or without |
8 * modification, are permitted provided that the following conditions | 8 * modification, are permitted provided that the following conditions |
9 * are met: | 9 * are met: |
10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
(...skipping 16 matching lines...) Expand all Loading... |
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 */ | 28 */ |
29 #ifdef HAVE_CONFIG_H | 29 #ifdef HAVE_CONFIG_H |
30 #include "config.h" | 30 #include "config.h" |
31 #endif | 31 #endif |
32 | 32 |
33 #include <sys/types.h> | 33 #include <sys/types.h> |
34 #ifdef HAVE_SYS_TIME_H | 34 #ifdef HAVE_SYS_TIME_H |
35 #include <sys/time.h> | 35 #include <sys/time.h> |
36 #else | 36 #else |
37 #include <sys/_time.h> | 37 #include <sys/_libevent_time.h> |
38 #endif | 38 #endif |
39 #include <sys/queue.h> | 39 #include <sys/queue.h> |
40 #include <poll.h> | 40 #include <poll.h> |
41 #include <signal.h> | 41 #include <signal.h> |
42 #include <stdio.h> | 42 #include <stdio.h> |
43 #include <stdlib.h> | 43 #include <stdlib.h> |
44 #include <string.h> | 44 #include <string.h> |
45 #include <unistd.h> | 45 #include <unistd.h> |
46 #include <errno.h> | 46 #include <errno.h> |
47 #ifdef CHECK_INVARIANTS | 47 #ifdef CHECK_INVARIANTS |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 poll_dealloc, | 80 poll_dealloc, |
81 0 | 81 0 |
82 }; | 82 }; |
83 | 83 |
84 static void * | 84 static void * |
85 poll_init(struct event_base *base) | 85 poll_init(struct event_base *base) |
86 { | 86 { |
87 struct pollop *pollop; | 87 struct pollop *pollop; |
88 | 88 |
89 /* Disable poll when this environment variable is set */ | 89 /* Disable poll when this environment variable is set */ |
90 » if (getenv("EVENT_NOPOLL")) | 90 » if (evutil_getenv("EVENT_NOPOLL")) |
91 return (NULL); | 91 return (NULL); |
92 | 92 |
93 if (!(pollop = calloc(1, sizeof(struct pollop)))) | 93 if (!(pollop = calloc(1, sizeof(struct pollop)))) |
94 return (NULL); | 94 return (NULL); |
95 | 95 |
96 evsignal_init(base); | 96 evsignal_init(base); |
97 | 97 |
98 return (pollop); | 98 return (pollop); |
99 } | 99 } |
100 | 100 |
(...skipping 27 matching lines...) Expand all Loading... |
128 assert(pop->idxplus1_by_fd[pfd->fd] == i+1); | 128 assert(pop->idxplus1_by_fd[pfd->fd] == i+1); |
129 } | 129 } |
130 } | 130 } |
131 #else | 131 #else |
132 #define poll_check_ok(pop) | 132 #define poll_check_ok(pop) |
133 #endif | 133 #endif |
134 | 134 |
135 static int | 135 static int |
136 poll_dispatch(struct event_base *base, void *arg, struct timeval *tv) | 136 poll_dispatch(struct event_base *base, void *arg, struct timeval *tv) |
137 { | 137 { |
138 » int res, i, msec = -1, nfds; | 138 » int res, i, j, msec = -1, nfds; |
139 struct pollop *pop = arg; | 139 struct pollop *pop = arg; |
140 | 140 |
141 poll_check_ok(pop); | 141 poll_check_ok(pop); |
142 | 142 |
143 if (tv != NULL) | 143 if (tv != NULL) |
144 msec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; | 144 msec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; |
145 | 145 |
146 nfds = pop->nfds; | 146 nfds = pop->nfds; |
147 res = poll(pop->event_set, nfds, msec); | 147 res = poll(pop->event_set, nfds, msec); |
148 | 148 |
149 if (res == -1) { | 149 if (res == -1) { |
150 if (errno != EINTR) { | 150 if (errno != EINTR) { |
151 event_warn("poll"); | 151 event_warn("poll"); |
152 return (-1); | 152 return (-1); |
153 } | 153 } |
154 | 154 |
155 evsignal_process(base); | 155 evsignal_process(base); |
156 return (0); | 156 return (0); |
157 } else if (base->sig.evsignal_caught) { | 157 } else if (base->sig.evsignal_caught) { |
158 evsignal_process(base); | 158 evsignal_process(base); |
159 } | 159 } |
160 | 160 |
161 event_debug(("%s: poll reports %d", __func__, res)); | 161 event_debug(("%s: poll reports %d", __func__, res)); |
162 | 162 |
163 » if (res == 0) | 163 » if (res == 0 || nfds == 0) |
164 return (0); | 164 return (0); |
165 | 165 |
166 » for (i = 0; i < nfds; i++) { | 166 » i = random() % nfds; |
167 » » int what = pop->event_set[i].revents; | 167 » for (j = 0; j < nfds; j++) { |
168 struct event *r_ev = NULL, *w_ev = NULL; | 168 struct event *r_ev = NULL, *w_ev = NULL; |
| 169 int what; |
| 170 if (++i == nfds) |
| 171 i = 0; |
| 172 what = pop->event_set[i].revents; |
| 173 |
169 if (!what) | 174 if (!what) |
170 continue; | 175 continue; |
171 | 176 |
172 res = 0; | 177 res = 0; |
173 | 178 |
174 /* If the file gets closed notify */ | 179 /* If the file gets closed notify */ |
175 if (what & (POLLHUP|POLLERR)) | 180 if (what & (POLLHUP|POLLERR)) |
176 what |= POLLIN|POLLOUT; | 181 what |= POLLIN|POLLOUT; |
177 if (what & POLLIN) { | 182 if (what & POLLIN) { |
178 res |= EV_READ; | 183 res |= EV_READ; |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 if (pop->event_r_back) | 370 if (pop->event_r_back) |
366 free(pop->event_r_back); | 371 free(pop->event_r_back); |
367 if (pop->event_w_back) | 372 if (pop->event_w_back) |
368 free(pop->event_w_back); | 373 free(pop->event_w_back); |
369 if (pop->idxplus1_by_fd) | 374 if (pop->idxplus1_by_fd) |
370 free(pop->idxplus1_by_fd); | 375 free(pop->idxplus1_by_fd); |
371 | 376 |
372 memset(pop, 0, sizeof(struct pollop)); | 377 memset(pop, 0, sizeof(struct pollop)); |
373 free(pop); | 378 free(pop); |
374 } | 379 } |
OLD | NEW |