comparison src/event/modules/ngx_select_module.c @ 245:e6c005b66b3a

nginx-0.0.1-2004-01-30-00:45:01 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 29 Jan 2004 21:45:01 +0000
parents 1119faf4635a
children e885208c518b
comparison
equal deleted inserted replaced
244:187dc0a3197d 245:e6c005b66b3a
129 { 129 {
130 ngx_connection_t *c; 130 ngx_connection_t *c;
131 131
132 c = ev->data; 132 c = ev->data;
133 133
134 #if (NGX_DEBUG_EVENT) 134 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
135 ngx_log_debug(ev->log, "select fd:%d event:%d" _ c->fd _ event); 135 "select add event fd:%d ev:%d", c->fd, event);
136 #endif
137 136
138 if (ev->index != NGX_INVALID_INDEX) { 137 if (ev->index != NGX_INVALID_INDEX) {
139 ngx_log_error(NGX_LOG_ALERT, ev->log, 0, 138 ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
140 "%d:%d is already set", c->fd, event); 139 "%d:%d is already set", c->fd, event);
141 return NGX_OK; 140 return NGX_OK;
195 194
196 if (ev->index == NGX_INVALID_INDEX) { 195 if (ev->index == NGX_INVALID_INDEX) {
197 return NGX_OK; 196 return NGX_OK;
198 } 197 }
199 198
200 #if (NGX_DEBUG_EVENT) 199 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
201 ngx_log_debug(c->log, "del event: %d, %d" _ c->fd _ event); 200 "select del event fd:%d ev:%d", c->fd, event);
202 #endif
203 201
204 #if (WIN32) 202 #if (WIN32)
205 203
206 if (event == NGX_READ_EVENT) { 204 if (event == NGX_READ_EVENT) {
207 FD_CLR(c->fd, &master_read_fd_set); 205 FD_CLR(c->fd, &master_read_fd_set);
257 255
258 timer = ngx_event_find_timer(); 256 timer = ngx_event_find_timer();
259 ngx_old_elapsed_msec = ngx_elapsed_msec; 257 ngx_old_elapsed_msec = ngx_elapsed_msec;
260 258
261 if (timer) { 259 if (timer) {
262 #if (HAVE_SELECT_CHANGE_TIMEOUT)
263 delta = 0;
264 #else
265 ngx_gettimeofday(&tv);
266 delta = tv.tv_sec * 1000 + tv.tv_usec / 1000;
267 #endif
268
269 tv.tv_sec = timer / 1000; 260 tv.tv_sec = timer / 1000;
270 tv.tv_usec = (timer % 1000) * 1000; 261 tv.tv_usec = (timer % 1000) * 1000;
271 tp = &tv; 262 tp = &tv;
272 263
273 } else { 264 } else {
274 delta = 0;
275 tp = NULL; 265 tp = NULL;
276 } 266 }
277 267
278 #if !(WIN32) 268 #if !(WIN32)
279 if (max_fd == -1) { 269 if (max_fd == -1) {
282 if (max_fd < c->fd) { 272 if (max_fd < c->fd) {
283 max_fd = c->fd; 273 max_fd = c->fd;
284 } 274 }
285 } 275 }
286 276
287 #if (NGX_DEBUG_EVENT) 277 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, log, 0,
288 ngx_log_debug(log, "change max_fd: %d" _ max_fd); 278 "change max_fd: %d", max_fd);
289 #endif 279 }
290 } 280 #endif
291 #endif 281
292 282 #if (NGX_DEBUG)
293 #if (NGX_DEBUG_EVENT)
294 for (i = 0; i < nevents; i++) { 283 for (i = 0; i < nevents; i++) {
295 ev = event_index[i]; 284 ev = event_index[i];
296 c = (ngx_connection_t *) ev->data; 285 c = (ngx_connection_t *) ev->data;
297 ngx_log_debug(log, "select: %d:%d" _ c->fd _ ev->write); 286 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, log, 0,
298 } 287 "select event: fd:%d wr:%d", c->fd,ev->write);
299 288 }
300 ngx_log_debug(log, "select timer: %d" _ timer); 289 #endif
301 #endif 290
291 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, log, 0, "select timer: %d", timer);
302 292
303 #if (WIN32) 293 #if (WIN32)
304 ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp); 294 ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp);
305 #else 295 #else
306 ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set, NULL, tp); 296 ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set, NULL, tp);
310 err = ngx_socket_errno; 300 err = ngx_socket_errno;
311 } else { 301 } else {
312 err = 0; 302 err = 0;
313 } 303 }
314 304
315 #if (NGX_DEBUG_EVENT)
316 ngx_log_debug(log, "select ready %d" _ ready);
317 #endif
318
319 #if (HAVE_SELECT_CHANGE_TIMEOUT) 305 #if (HAVE_SELECT_CHANGE_TIMEOUT)
320 306
321 if (timer) { 307 if (timer) {
322 delta = timer - (tv.tv_sec * 1000 + tv.tv_usec / 1000); 308 delta = timer - (tv.tv_sec * 1000 + tv.tv_usec / 1000);
323 309
328 314
329 deltas += delta; 315 deltas += delta;
330 if (deltas > 1000) { 316 if (deltas > 1000) {
331 ngx_gettimeofday(&tv); 317 ngx_gettimeofday(&tv);
332 deltas = tv.tv_usec / 1000; 318 deltas = tv.tv_usec / 1000;
333 319 ngx_time_update(tv.tv_sec);
334 if (ngx_cached_time != tv.tv_sec) { 320 }
335 ngx_cached_time = tv.tv_sec; 321
336 ngx_time_update(); 322 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, log, 0,
337 } 323 "select timer: %d, delta: %d", timer, (int) delta);
338 }
339
340 #if (NGX_DEBUG_EVENT)
341 ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ (int) delta);
342 #endif
343
344 ngx_event_expire_timers((ngx_msec_t) delta);
345 324
346 } else { 325 } else {
347 ngx_gettimeofday(&tv); 326 ngx_gettimeofday(&tv);
348 327 ngx_time_update(tv.tv_sec);
349 if (ngx_cached_time != tv.tv_sec) {
350 ngx_cached_time = tv.tv_sec;
351 ngx_time_update();
352 }
353 328
354 if (ready == 0) { 329 if (ready == 0) {
355 ngx_log_error(NGX_LOG_ALERT, log, 0, 330 ngx_log_error(NGX_LOG_ALERT, log, 0,
356 "select() returned no events without timeout"); 331 "select() returned no events without timeout");
357 return NGX_ERROR; 332 return NGX_ERROR;
358 } 333 }
359 334 }
360 #if (NGX_DEBUG_EVENT) 335
361 ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ (int) delta); 336 #else /* !(HAVE_SELECT_CHANGE_TIMEOUT) */
362 #endif
363 }
364
365 #else /* HAVE_SELECT_CHANGE_TIMEOUT */
366 337
367 ngx_gettimeofday(&tv); 338 ngx_gettimeofday(&tv);
368 ngx_time_update(tv.tv_sec); 339 ngx_time_update(tv.tv_sec);
369 340
341 delta = ngx_elapsed_msec;
342 ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000 - ngx_start_msec;
343
370 if (timer) { 344 if (timer) {
371 delta = tv.tv_sec * 1000 + tv.tv_usec / 1000 - delta; 345 delta = ngx_elapsed_msec - delta;
372 346
373 #if (NGX_DEBUG_EVENT) 347 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, log, 0,
374 ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ (int) delta); 348 "select timer: %d, delta: %d", timer, (int) delta);
375 #endif
376
377 ngx_event_expire_timers((ngx_msec_t) delta);
378 349
379 } else { 350 } else {
380 if (ready == 0) { 351 if (ready == 0) {
381 ngx_log_error(NGX_LOG_ALERT, log, 0, 352 ngx_log_error(NGX_LOG_ALERT, log, 0,
382 "select() returned no events without timeout"); 353 "select() returned no events without timeout");
383 return NGX_ERROR; 354 return NGX_ERROR;
384 } 355 }
385
386 #if (NGX_DEBUG_EVENT)
387 ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ (int) delta);
388 #endif
389 } 356 }
390 357
391 #endif /* HAVE_SELECT_CHANGE_TIMEOUT */ 358 #endif /* HAVE_SELECT_CHANGE_TIMEOUT */
392 359
360 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, log, 0, "select ready %d", ready);
361
393 if (err) { 362 if (err) {
394 ngx_log_error(NGX_LOG_ALERT, log, err, "select() failed"); 363 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
364 log, err, "select() failed");
395 return NGX_ERROR; 365 return NGX_ERROR;
396 } 366 }
397 367
398 nready = 0; 368 nready = 0;
399 369
403 found = 0; 373 found = 0;
404 374
405 if (ev->write) { 375 if (ev->write) {
406 if (FD_ISSET(c->fd, &work_write_fd_set)) { 376 if (FD_ISSET(c->fd, &work_write_fd_set)) {
407 found = 1; 377 found = 1;
408 #if (NGX_DEBUG_EVENT) 378 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, log, 0,
409 ngx_log_debug(log, "select write %d" _ c->fd); 379 "select write %d", c->fd);
410 #endif
411 } 380 }
412 381
413 } else { 382 } else {
414 if (FD_ISSET(c->fd, &work_read_fd_set)) { 383 if (FD_ISSET(c->fd, &work_read_fd_set)) {
415 found = 1; 384 found = 1;
416 #if (NGX_DEBUG_EVENT) 385 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, log, 0,
417 ngx_log_debug(log, "select read %d" _ c->fd); 386 "select read %d", c->fd);
418 #endif
419 } 387 }
420 } 388 }
421 389
422 if (found) { 390 if (found) {
423 ready_index[nready++] = ev; 391 ready_index[nready++] = ev;
451 419
452 if (ready != 0) { 420 if (ready != 0) {
453 ngx_log_error(NGX_LOG_ALERT, log, 0, "select ready != events"); 421 ngx_log_error(NGX_LOG_ALERT, log, 0, "select ready != events");
454 } 422 }
455 423
424 if (timer && delta) {
425 ngx_event_expire_timers((ngx_msec_t) delta);
426 }
427
456 return NGX_OK; 428 return NGX_OK;
457 } 429 }
458 430
459 431
460 static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf) 432 static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf)