Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Maxim Konovalov | f8d59e3 | 2012-01-18 15:07:43 +0000 | [diff] [blame] | 4 | * Copyright (C) Nginx, Inc. |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 8 | #include <ngx_config.h> |
| 9 | #include <ngx_core.h> |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 10 | #include <ngx_event.h> |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 11 | |
| 12 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 13 | static ngx_int_t ngx_poll_init(ngx_cycle_t *cycle, ngx_msec_t timer); |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 14 | static void ngx_poll_done(ngx_cycle_t *cycle); |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 15 | static ngx_int_t ngx_poll_add_event(ngx_event_t *ev, ngx_int_t event, |
| 16 | ngx_uint_t flags); |
| 17 | static ngx_int_t ngx_poll_del_event(ngx_event_t *ev, ngx_int_t event, |
| 18 | ngx_uint_t flags); |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 19 | static ngx_int_t ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, |
| 20 | ngx_uint_t flags); |
Igor Sysoev | 2b97993 | 2004-07-07 15:01:00 +0000 | [diff] [blame] | 21 | static char *ngx_poll_init_conf(ngx_cycle_t *cycle, void *conf); |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 22 | |
| 23 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 24 | static struct pollfd *event_list; |
Yves Crespin | 6a58555 | 2014-08-07 14:56:57 +0200 | [diff] [blame] | 25 | static ngx_uint_t nevents; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 26 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 27 | |
| 28 | static ngx_str_t poll_name = ngx_string("poll"); |
| 29 | |
| 30 | ngx_event_module_t ngx_poll_module_ctx = { |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 31 | &poll_name, |
| 32 | NULL, /* create configuration */ |
Igor Sysoev | 2b97993 | 2004-07-07 15:01:00 +0000 | [diff] [blame] | 33 | ngx_poll_init_conf, /* init configuration */ |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 34 | |
| 35 | { |
| 36 | ngx_poll_add_event, /* add an event */ |
| 37 | ngx_poll_del_event, /* delete an event */ |
| 38 | ngx_poll_add_event, /* enable an event */ |
| 39 | ngx_poll_del_event, /* disable an event */ |
| 40 | NULL, /* add an connection */ |
| 41 | NULL, /* delete an connection */ |
Igor Sysoev | c78c41c | 2004-07-07 06:15:04 +0000 | [diff] [blame] | 42 | NULL, /* process the changes */ |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 43 | ngx_poll_process_events, /* process the events */ |
| 44 | ngx_poll_init, /* init the events */ |
| 45 | ngx_poll_done /* done the events */ |
| 46 | } |
| 47 | |
| 48 | }; |
| 49 | |
| 50 | ngx_module_t ngx_poll_module = { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 51 | NGX_MODULE_V1, |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 52 | &ngx_poll_module_ctx, /* module context */ |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 53 | NULL, /* module directives */ |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 54 | NGX_EVENT_MODULE, /* module type */ |
Igor Sysoev | e573380 | 2005-09-08 14:36:09 +0000 | [diff] [blame] | 55 | NULL, /* init master */ |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 56 | NULL, /* init module */ |
Igor Sysoev | e573380 | 2005-09-08 14:36:09 +0000 | [diff] [blame] | 57 | NULL, /* init process */ |
| 58 | NULL, /* init thread */ |
| 59 | NULL, /* exit thread */ |
| 60 | NULL, /* exit process */ |
| 61 | NULL, /* exit master */ |
| 62 | NGX_MODULE_V1_PADDING |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | |
| 66 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 67 | static ngx_int_t |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 68 | ngx_poll_init(ngx_cycle_t *cycle, ngx_msec_t timer) |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 69 | { |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 70 | struct pollfd *list; |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 71 | |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 72 | if (event_list == NULL) { |
| 73 | nevents = 0; |
| 74 | } |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 75 | |
Igor Sysoev | 72db760 | 2009-11-23 15:46:21 +0000 | [diff] [blame] | 76 | if (ngx_process >= NGX_PROCESS_WORKER |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 77 | || cycle->old_cycle == NULL |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 78 | || cycle->old_cycle->connection_n < cycle->connection_n) |
| 79 | { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 80 | list = ngx_alloc(sizeof(struct pollfd) * cycle->connection_n, |
| 81 | cycle->log); |
| 82 | if (list == NULL) { |
| 83 | return NGX_ERROR; |
| 84 | } |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 85 | |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 86 | if (event_list) { |
| 87 | ngx_memcpy(list, event_list, sizeof(ngx_event_t *) * nevents); |
| 88 | ngx_free(event_list); |
| 89 | } |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 90 | |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 91 | event_list = list; |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 94 | ngx_io = ngx_os_io; |
| 95 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 96 | ngx_event_actions = ngx_poll_module_ctx.actions; |
| 97 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 98 | ngx_event_flags = NGX_USE_LEVEL_EVENT|NGX_USE_FD_EVENT; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 99 | |
| 100 | return NGX_OK; |
| 101 | } |
| 102 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 103 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 104 | static void |
| 105 | ngx_poll_done(ngx_cycle_t *cycle) |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 106 | { |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 107 | ngx_free(event_list); |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 108 | |
| 109 | event_list = NULL; |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 113 | static ngx_int_t |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 114 | ngx_poll_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 115 | { |
Igor Sysoev | 3a17f24 | 2002-12-24 17:30:59 +0000 | [diff] [blame] | 116 | ngx_event_t *e; |
| 117 | ngx_connection_t *c; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 118 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 119 | c = ev->data; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 120 | |
Igor Sysoev | b3968b3 | 2004-04-14 17:44:28 +0000 | [diff] [blame] | 121 | ev->active = 1; |
| 122 | |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 123 | if (ev->index != NGX_INVALID_INDEX) { |
| 124 | ngx_log_error(NGX_LOG_ALERT, ev->log, 0, |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 125 | "poll event fd:%d ev:%i is already set", c->fd, event); |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 126 | return NGX_OK; |
| 127 | } |
| 128 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 129 | if (event == NGX_READ_EVENT) { |
| 130 | e = c->write; |
| 131 | #if (NGX_READ_EVENT != POLLIN) |
| 132 | event = POLLIN; |
| 133 | #endif |
| 134 | |
| 135 | } else { |
| 136 | e = c->read; |
| 137 | #if (NGX_WRITE_EVENT != POLLOUT) |
| 138 | event = POLLOUT; |
| 139 | #endif |
| 140 | } |
| 141 | |
Igor Sysoev | 10318a2 | 2004-01-29 21:45:01 +0000 | [diff] [blame] | 142 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 143 | "poll add event: fd:%d ev:%i", c->fd, event); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 144 | |
| 145 | if (e == NULL || e->index == NGX_INVALID_INDEX) { |
| 146 | event_list[nevents].fd = c->fd; |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 147 | event_list[nevents].events = (short) event; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 148 | event_list[nevents].revents = 0; |
| 149 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 150 | ev->index = nevents; |
| 151 | nevents++; |
| 152 | |
| 153 | } else { |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 154 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 155 | "poll add index: %i", e->index); |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 156 | |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 157 | event_list[e->index].events |= (short) event; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 158 | ev->index = e->index; |
| 159 | } |
| 160 | |
| 161 | return NGX_OK; |
| 162 | } |
| 163 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 164 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 165 | static ngx_int_t |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 166 | ngx_poll_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 167 | { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 168 | ngx_event_t *e; |
| 169 | ngx_connection_t *c; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 170 | |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 171 | c = ev->data; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 172 | |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 173 | ev->active = 0; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 174 | |
Igor Sysoev | 3a17f24 | 2002-12-24 17:30:59 +0000 | [diff] [blame] | 175 | if (ev->index == NGX_INVALID_INDEX) { |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 176 | ngx_log_error(NGX_LOG_ALERT, ev->log, 0, |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 177 | "poll event fd:%d ev:%i is already deleted", |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 178 | c->fd, event); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 179 | return NGX_OK; |
Igor Sysoev | 3a17f24 | 2002-12-24 17:30:59 +0000 | [diff] [blame] | 180 | } |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 181 | |
| 182 | if (event == NGX_READ_EVENT) { |
| 183 | e = c->write; |
| 184 | #if (NGX_READ_EVENT != POLLIN) |
| 185 | event = POLLIN; |
| 186 | #endif |
| 187 | |
| 188 | } else { |
| 189 | e = c->read; |
| 190 | #if (NGX_WRITE_EVENT != POLLOUT) |
| 191 | event = POLLOUT; |
| 192 | #endif |
| 193 | } |
| 194 | |
Igor Sysoev | 10318a2 | 2004-01-29 21:45:01 +0000 | [diff] [blame] | 195 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 196 | "poll del event: fd:%d ev:%i", c->fd, event); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 197 | |
| 198 | if (e == NULL || e->index == NGX_INVALID_INDEX) { |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 199 | nevents--; |
| 200 | |
Yves Crespin | 6a58555 | 2014-08-07 14:56:57 +0200 | [diff] [blame] | 201 | if (ev->index < nevents) { |
Igor Sysoev | 7b6062a | 2004-02-12 20:57:10 +0000 | [diff] [blame] | 202 | |
| 203 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 204 | "index: copy event %ui to %i", nevents, ev->index); |
Igor Sysoev | 7b6062a | 2004-02-12 20:57:10 +0000 | [diff] [blame] | 205 | |
Igor Sysoev | 295bb63 | 2002-12-23 18:22:18 +0000 | [diff] [blame] | 206 | event_list[ev->index] = event_list[nevents]; |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 207 | |
Igor Sysoev | 31eb8c0 | 2005-09-23 11:02:22 +0000 | [diff] [blame] | 208 | c = ngx_cycle->files[event_list[nevents].fd]; |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 209 | |
| 210 | if (c->fd == -1) { |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 211 | ngx_log_error(NGX_LOG_ALERT, ev->log, 0, |
| 212 | "unexpected last event"); |
| 213 | |
| 214 | } else { |
Yves Crespin | 6a58555 | 2014-08-07 14:56:57 +0200 | [diff] [blame] | 215 | if (c->read->index == nevents) { |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 216 | c->read->index = ev->index; |
Igor Sysoev | 7b6062a | 2004-02-12 20:57:10 +0000 | [diff] [blame] | 217 | } |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 218 | |
Yves Crespin | 6a58555 | 2014-08-07 14:56:57 +0200 | [diff] [blame] | 219 | if (c->write->index == nevents) { |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 220 | c->write->index = ev->index; |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } else { |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 226 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 227 | "poll del index: %i", e->index); |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 228 | |
Igor Sysoev | 83a6851 | 2007-07-29 18:24:53 +0000 | [diff] [blame] | 229 | event_list[e->index].events &= (short) ~event; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | ev->index = NGX_INVALID_INDEX; |
| 233 | |
| 234 | return NGX_OK; |
| 235 | } |
| 236 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 237 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 238 | static ngx_int_t |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 239 | ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags) |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 240 | { |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 241 | int ready, revents; |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 242 | ngx_err_t err; |
Yves Crespin | 6a58555 | 2014-08-07 14:56:57 +0200 | [diff] [blame] | 243 | ngx_uint_t i, found, level; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 244 | ngx_event_t *ev, **queue; |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 245 | ngx_connection_t *c; |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 246 | |
| 247 | /* NGX_TIMER_INFINITE == INFTIM */ |
| 248 | |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 249 | #if (NGX_DEBUG0) |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 250 | if (cycle->log->log_level & NGX_LOG_DEBUG_ALL) { |
| 251 | for (i = 0; i < nevents; i++) { |
| 252 | ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Yves Crespin | 3c0c66c | 2014-08-07 15:04:43 +0200 | [diff] [blame] | 253 | "poll: %ui: fd:%d ev:%04Xd", |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 254 | i, event_list[i].fd, event_list[i].events); |
| 255 | } |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 256 | } |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 257 | #endif |
| 258 | |
Igor Sysoev | 208eed2 | 2005-10-07 13:30:52 +0000 | [diff] [blame] | 259 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "poll timer: %M", timer); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 260 | |
Igor Sysoev | d9d0ca1 | 2003-11-21 06:30:49 +0000 | [diff] [blame] | 261 | ready = poll(event_list, (u_int) nevents, (int) timer); |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 262 | |
Igor Sysoev | 8e7e69e | 2009-08-25 09:06:21 +0000 | [diff] [blame] | 263 | err = (ready == -1) ? ngx_errno : 0; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 264 | |
Igor Sysoev | b80f68a | 2010-03-12 14:31:47 +0000 | [diff] [blame] | 265 | if (flags & NGX_UPDATE_TIME || ngx_event_timer_alarm) { |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 266 | ngx_time_update(); |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 267 | } |
Igor Sysoev | 10318a2 | 2004-01-29 21:45:01 +0000 | [diff] [blame] | 268 | |
Igor Sysoev | c972a3f | 2004-04-02 15:13:20 +0000 | [diff] [blame] | 269 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Yves Crespin | 3c0c66c | 2014-08-07 15:04:43 +0200 | [diff] [blame] | 270 | "poll ready %d of %ui", ready, nevents); |
Igor Sysoev | bbcea6c | 2004-01-30 17:39:00 +0000 | [diff] [blame] | 271 | |
| 272 | if (err) { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 273 | if (err == NGX_EINTR) { |
| 274 | |
| 275 | if (ngx_event_timer_alarm) { |
| 276 | ngx_event_timer_alarm = 0; |
| 277 | return NGX_OK; |
| 278 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 279 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 280 | level = NGX_LOG_INFO; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 281 | |
| 282 | } else { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 283 | level = NGX_LOG_ALERT; |
| 284 | } |
| 285 | |
| 286 | ngx_log_error(level, cycle->log, err, "poll() failed"); |
Igor Sysoev | bbcea6c | 2004-01-30 17:39:00 +0000 | [diff] [blame] | 287 | return NGX_ERROR; |
| 288 | } |
| 289 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 290 | if (ready == 0) { |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 291 | if (timer != NGX_TIMER_INFINITE) { |
| 292 | return NGX_OK; |
| 293 | } |
| 294 | |
| 295 | ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, |
| 296 | "poll() returned no events without timeout"); |
| 297 | return NGX_ERROR; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 300 | ngx_mutex_lock(ngx_posted_events_mutex); |
| 301 | |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 302 | for (i = 0; i < nevents && ready; i++) { |
Igor Sysoev | 328af6e | 2004-02-01 08:10:52 +0000 | [diff] [blame] | 303 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 304 | revents = event_list[i].revents; |
| 305 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 306 | #if 1 |
Igor Sysoev | c972a3f | 2004-04-02 15:13:20 +0000 | [diff] [blame] | 307 | ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Yves Crespin | 3c0c66c | 2014-08-07 15:04:43 +0200 | [diff] [blame] | 308 | "poll: %ui: fd:%d ev:%04Xd rev:%04Xd", |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 309 | i, event_list[i].fd, event_list[i].events, revents); |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 310 | #else |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 311 | if (revents) { |
Igor Sysoev | c972a3f | 2004-04-02 15:13:20 +0000 | [diff] [blame] | 312 | ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Yves Crespin | 3c0c66c | 2014-08-07 15:04:43 +0200 | [diff] [blame] | 313 | "poll: %ui: fd:%d ev:%04Xd rev:%04Xd", |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 314 | i, event_list[i].fd, event_list[i].events, revents); |
Igor Sysoev | 9260294 | 2004-02-05 16:58:36 +0000 | [diff] [blame] | 315 | } |
| 316 | #endif |
Igor Sysoev | 328af6e | 2004-02-01 08:10:52 +0000 | [diff] [blame] | 317 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 318 | if (revents & POLLNVAL) { |
Igor Sysoev | c972a3f | 2004-04-02 15:13:20 +0000 | [diff] [blame] | 319 | ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 320 | "poll() error fd:%d ev:%04Xd rev:%04Xd", |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 321 | event_list[i].fd, event_list[i].events, revents); |
Igor Sysoev | 328af6e | 2004-02-01 08:10:52 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 324 | if (revents & ~(POLLIN|POLLOUT|POLLERR|POLLHUP|POLLNVAL)) { |
Igor Sysoev | c972a3f | 2004-04-02 15:13:20 +0000 | [diff] [blame] | 325 | ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 326 | "strange poll() events fd:%d ev:%04Xd rev:%04Xd", |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 327 | event_list[i].fd, event_list[i].events, revents); |
Igor Sysoev | 328af6e | 2004-02-01 08:10:52 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Igor Sysoev | c537167 | 2004-02-04 20:30:08 +0000 | [diff] [blame] | 330 | if (event_list[i].fd == -1) { |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 331 | /* |
Igor Sysoev | a367724 | 2004-04-14 05:57:36 +0000 | [diff] [blame] | 332 | * the disabled event, a workaround for our possible bug, |
| 333 | * see the comment below |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 334 | */ |
Igor Sysoev | c537167 | 2004-02-04 20:30:08 +0000 | [diff] [blame] | 335 | continue; |
| 336 | } |
| 337 | |
Igor Sysoev | 31eb8c0 | 2005-09-23 11:02:22 +0000 | [diff] [blame] | 338 | c = ngx_cycle->files[event_list[i].fd]; |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 339 | |
| 340 | if (c->fd == -1) { |
Igor Sysoev | c972a3f | 2004-04-02 15:13:20 +0000 | [diff] [blame] | 341 | ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "unexpected event"); |
Igor Sysoev | af57922 | 2004-02-03 20:27:11 +0000 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * it is certainly our fault and it should be investigated, |
| 345 | * in the meantime we disable this event to avoid a CPU spinning |
| 346 | */ |
| 347 | |
| 348 | if (i == nevents - 1) { |
| 349 | nevents--; |
| 350 | } else { |
| 351 | event_list[i].fd = -1; |
| 352 | } |
| 353 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 354 | continue; |
| 355 | } |
| 356 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 357 | if ((revents & (POLLERR|POLLHUP|POLLNVAL)) |
| 358 | && (revents & (POLLIN|POLLOUT)) == 0) |
| 359 | { |
| 360 | /* |
| 361 | * if the error events were returned without POLLIN or POLLOUT, |
| 362 | * then add these flags to handle the events at least in one |
| 363 | * active handler |
| 364 | */ |
| 365 | |
| 366 | revents |= POLLIN|POLLOUT; |
| 367 | } |
| 368 | |
Igor Sysoev | 328af6e | 2004-02-01 08:10:52 +0000 | [diff] [blame] | 369 | found = 0; |
| 370 | |
Ruslan Ermilov | 33e934c | 2013-01-25 09:59:28 +0000 | [diff] [blame] | 371 | if ((revents & POLLIN) && c->read->active) { |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 372 | found = 1; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 373 | |
| 374 | ev = c->read; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 375 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 376 | if ((flags & NGX_POST_THREAD_EVENTS) && !ev->accept) { |
| 377 | ev->posted_ready = 1; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 378 | |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 379 | } else { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 380 | ev->ready = 1; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 383 | queue = (ngx_event_t **) (ev->accept ? &ngx_posted_accept_events: |
| 384 | &ngx_posted_events); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 385 | ngx_locked_post_event(ev, queue); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Ruslan Ermilov | 33e934c | 2013-01-25 09:59:28 +0000 | [diff] [blame] | 388 | if ((revents & POLLOUT) && c->write->active) { |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 389 | found = 1; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 390 | ev = c->write; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 391 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 392 | if (flags & NGX_POST_THREAD_EVENTS) { |
| 393 | ev->posted_ready = 1; |
| 394 | |
| 395 | } else { |
| 396 | ev->ready = 1; |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 399 | ngx_locked_post_event(ev, &ngx_posted_events); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 402 | if (found) { |
| 403 | ready--; |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 404 | continue; |
| 405 | } |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 408 | ngx_mutex_unlock(ngx_posted_events_mutex); |
Igor Sysoev | 3d54061 | 2004-04-13 15:08:48 +0000 | [diff] [blame] | 409 | |
Igor Sysoev | 3a17f24 | 2002-12-24 17:30:59 +0000 | [diff] [blame] | 410 | if (ready != 0) { |
Igor Sysoev | c972a3f | 2004-04-02 15:13:20 +0000 | [diff] [blame] | 411 | ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "poll ready != events"); |
Igor Sysoev | 3a17f24 | 2002-12-24 17:30:59 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Maxim Dounin | 870b962 | 2014-08-10 17:44:46 +0400 | [diff] [blame^] | 414 | return NGX_OK; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 415 | } |
Igor Sysoev | 2b97993 | 2004-07-07 15:01:00 +0000 | [diff] [blame] | 416 | |
| 417 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 418 | static char * |
| 419 | ngx_poll_init_conf(ngx_cycle_t *cycle, void *conf) |
Igor Sysoev | 2b97993 | 2004-07-07 15:01:00 +0000 | [diff] [blame] | 420 | { |
| 421 | ngx_event_conf_t *ecf; |
| 422 | |
| 423 | ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); |
| 424 | |
| 425 | if (ecf->use != ngx_poll_module.ctx_index) { |
| 426 | return NGX_CONF_OK; |
| 427 | } |
| 428 | |
| 429 | #if (NGX_THREADS) |
| 430 | |
| 431 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| 432 | "poll() is not supported in the threaded mode"); |
| 433 | return NGX_CONF_ERROR; |
| 434 | |
| 435 | #else |
| 436 | |
| 437 | return NGX_CONF_OK; |
| 438 | |
| 439 | #endif |
| 440 | } |