| OLD | NEW |
| 1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ | 1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ |
| 2 | 2 |
| 3 /* | 3 /* |
| 4 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu> | 4 * Copyright 2000-2002 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 #ifdef HAVE_SYS_SELECT_H | 39 #ifdef HAVE_SYS_SELECT_H |
| 40 #include <sys/select.h> | 40 #include <sys/select.h> |
| 41 #endif | 41 #endif |
| 42 #include <sys/queue.h> | 42 #include <sys/queue.h> |
| 43 #include <signal.h> | 43 #include <signal.h> |
| 44 #include <stdio.h> | 44 #include <stdio.h> |
| 45 #include <stdlib.h> | 45 #include <stdlib.h> |
| 46 #include <string.h> | 46 #include <string.h> |
| 47 #include <unistd.h> | 47 #include <unistd.h> |
| 48 #include <errno.h> | 48 #include <errno.h> |
| 49 #ifdef CHECK_INVARIANTS | 49 #ifdef CHECK_INVARIANTS |
| 50 #include <assert.h> | 50 #include <assert.h> |
| 51 #endif | 51 #endif |
| 52 | 52 |
| 53 #include "event.h" | 53 #include "event.h" |
| 54 #include "evutil.h" |
| 54 #include "event-internal.h" | 55 #include "event-internal.h" |
| 55 #include "evsignal.h" | 56 #include "evsignal.h" |
| 56 #include "log.h" | 57 #include "log.h" |
| 57 | 58 |
| 58 #ifndef howmany | 59 #ifndef howmany |
| 59 #define howmany(x, y) (((x)+((y)-1))/(y)) | 60 #define howmany(x, y) (((x)+((y)-1))/(y)) |
| 60 #endif | 61 #endif |
| 61 | 62 |
| 63 #ifndef _EVENT_HAVE_FD_MASK |
| 64 /* This type is mandatory, but Android doesn't define it. */ |
| 65 #undef NFDBITS |
| 66 #define NFDBITS (sizeof(long)*8) |
| 67 typedef unsigned long fd_mask; |
| 68 #endif |
| 69 |
| 62 struct selectop { | 70 struct selectop { |
| 63 int event_fds; /* Highest fd in fd set */ | 71 int event_fds; /* Highest fd in fd set */ |
| 64 int event_fdsz; | 72 int event_fdsz; |
| 65 fd_set *event_readset_in; | 73 fd_set *event_readset_in; |
| 66 fd_set *event_writeset_in; | 74 fd_set *event_writeset_in; |
| 67 fd_set *event_readset_out; | 75 fd_set *event_readset_out; |
| 68 fd_set *event_writeset_out; | 76 fd_set *event_writeset_out; |
| 69 struct event **event_r_by_fd; | 77 struct event **event_r_by_fd; |
| 70 struct event **event_w_by_fd; | 78 struct event **event_w_by_fd; |
| 71 }; | 79 }; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 87 }; | 95 }; |
| 88 | 96 |
| 89 static int select_resize(struct selectop *sop, int fdsz); | 97 static int select_resize(struct selectop *sop, int fdsz); |
| 90 | 98 |
| 91 static void * | 99 static void * |
| 92 select_init(struct event_base *base) | 100 select_init(struct event_base *base) |
| 93 { | 101 { |
| 94 struct selectop *sop; | 102 struct selectop *sop; |
| 95 | 103 |
| 96 /* Disable select when this environment variable is set */ | 104 /* Disable select when this environment variable is set */ |
| 97 » if (getenv("EVENT_NOSELECT")) | 105 » if (evutil_getenv("EVENT_NOSELECT")) |
| 98 return (NULL); | 106 return (NULL); |
| 99 | 107 |
| 100 if (!(sop = calloc(1, sizeof(struct selectop)))) | 108 if (!(sop = calloc(1, sizeof(struct selectop)))) |
| 101 return (NULL); | 109 return (NULL); |
| 102 | 110 |
| 103 select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask)); | 111 select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask)); |
| 104 | 112 |
| 105 evsignal_init(base); | 113 evsignal_init(base); |
| 106 | 114 |
| 107 return (sop); | 115 return (sop); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 130 } | 138 } |
| 131 | 139 |
| 132 } | 140 } |
| 133 #else | 141 #else |
| 134 #define check_selectop(sop) do { (void) sop; } while (0) | 142 #define check_selectop(sop) do { (void) sop; } while (0) |
| 135 #endif | 143 #endif |
| 136 | 144 |
| 137 static int | 145 static int |
| 138 select_dispatch(struct event_base *base, void *arg, struct timeval *tv) | 146 select_dispatch(struct event_base *base, void *arg, struct timeval *tv) |
| 139 { | 147 { |
| 140 » int res, i; | 148 » int res, i, j; |
| 141 struct selectop *sop = arg; | 149 struct selectop *sop = arg; |
| 142 | 150 |
| 143 check_selectop(sop); | 151 check_selectop(sop); |
| 144 | 152 |
| 145 memcpy(sop->event_readset_out, sop->event_readset_in, | 153 memcpy(sop->event_readset_out, sop->event_readset_in, |
| 146 sop->event_fdsz); | 154 sop->event_fdsz); |
| 147 memcpy(sop->event_writeset_out, sop->event_writeset_in, | 155 memcpy(sop->event_writeset_out, sop->event_writeset_in, |
| 148 sop->event_fdsz); | 156 sop->event_fdsz); |
| 149 | 157 |
| 150 res = select(sop->event_fds + 1, sop->event_readset_out, | 158 res = select(sop->event_fds + 1, sop->event_readset_out, |
| 151 sop->event_writeset_out, NULL, tv); | 159 sop->event_writeset_out, NULL, tv); |
| 152 | 160 |
| 153 check_selectop(sop); | 161 check_selectop(sop); |
| 154 | 162 |
| 155 if (res == -1) { | 163 if (res == -1) { |
| 156 if (errno != EINTR) { | 164 if (errno != EINTR) { |
| 157 event_warn("select"); | 165 event_warn("select"); |
| 158 return (-1); | 166 return (-1); |
| 159 } | 167 } |
| 160 | 168 |
| 161 evsignal_process(base); | 169 evsignal_process(base); |
| 162 return (0); | 170 return (0); |
| 163 } else if (base->sig.evsignal_caught) { | 171 } else if (base->sig.evsignal_caught) { |
| 164 evsignal_process(base); | 172 evsignal_process(base); |
| 165 } | 173 } |
| 166 | 174 |
| 167 event_debug(("%s: select reports %d", __func__, res)); | 175 event_debug(("%s: select reports %d", __func__, res)); |
| 168 | 176 |
| 169 check_selectop(sop); | 177 check_selectop(sop); |
| 170 » for (i = 0; i <= sop->event_fds; ++i) { | 178 » i = random() % (sop->event_fds+1); |
| 179 » for (j = 0; j <= sop->event_fds; ++j) { |
| 171 struct event *r_ev = NULL, *w_ev = NULL; | 180 struct event *r_ev = NULL, *w_ev = NULL; |
| 181 if (++i >= sop->event_fds+1) |
| 182 i = 0; |
| 183 |
| 172 res = 0; | 184 res = 0; |
| 173 if (FD_ISSET(i, sop->event_readset_out)) { | 185 if (FD_ISSET(i, sop->event_readset_out)) { |
| 174 r_ev = sop->event_r_by_fd[i]; | 186 r_ev = sop->event_r_by_fd[i]; |
| 175 res |= EV_READ; | 187 res |= EV_READ; |
| 176 } | 188 } |
| 177 if (FD_ISSET(i, sop->event_writeset_out)) { | 189 if (FD_ISSET(i, sop->event_writeset_out)) { |
| 178 w_ev = sop->event_w_by_fd[i]; | 190 w_ev = sop->event_w_by_fd[i]; |
| 179 res |= EV_WRITE; | 191 res |= EV_WRITE; |
| 180 } | 192 } |
| 181 if (r_ev && (res & r_ev->ev_events)) { | 193 if (r_ev && (res & r_ev->ev_events)) { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 if (sop->event_writeset_out) | 355 if (sop->event_writeset_out) |
| 344 free(sop->event_writeset_out); | 356 free(sop->event_writeset_out); |
| 345 if (sop->event_r_by_fd) | 357 if (sop->event_r_by_fd) |
| 346 free(sop->event_r_by_fd); | 358 free(sop->event_r_by_fd); |
| 347 if (sop->event_w_by_fd) | 359 if (sop->event_w_by_fd) |
| 348 free(sop->event_w_by_fd); | 360 free(sop->event_w_by_fd); |
| 349 | 361 |
| 350 memset(sop, 0, sizeof(struct selectop)); | 362 memset(sop, 0, sizeof(struct selectop)); |
| 351 free(sop); | 363 free(sop); |
| 352 } | 364 } |
| OLD | NEW |