src/event/ngx_event_timer.h - nginx-1.7.10

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_EVENT_TIMER_H_INCLUDED_
  6. #define _NGX_EVENT_TIMER_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>
  9. #include <ngx_event.h>


  10. #define NGX_TIMER_INFINITE  (ngx_msec_t) -1

  11. #define NGX_TIMER_LAZY_DELAY  300


  12. ngx_int_t ngx_event_timer_init(ngx_log_t *log);
  13. ngx_msec_t ngx_event_find_timer(void);
  14. void ngx_event_expire_timers(void);
  15. void ngx_event_cancel_timers(void);


  16. extern ngx_rbtree_t  ngx_event_timer_rbtree;


  17. static ngx_inline void
  18. ngx_event_del_timer(ngx_event_t *ev)
  19. {
  20.     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
  21.                    "event timer del: %d: %M",
  22.                     ngx_event_ident(ev->data), ev->timer.key);

  23.     ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);

  24. #if (NGX_DEBUG)
  25.     ev->timer.left = NULL;
  26.     ev->timer.right = NULL;
  27.     ev->timer.parent = NULL;
  28. #endif

  29.     ev->timer_set = 0;
  30. }


  31. static ngx_inline void
  32. ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
  33. {
  34.     ngx_msec_t      key;
  35.     ngx_msec_int_t  diff;

  36.     key = ngx_current_msec + timer;

  37.     if (ev->timer_set) {

  38.         /*
  39.          * Use a previous timer value if difference between it and a new
  40.          * value is less than NGX_TIMER_LAZY_DELAY milliseconds: this allows
  41.          * to minimize the rbtree operations for fast connections.
  42.          */

  43.         diff = (ngx_msec_int_t) (key - ev->timer.key);

  44.         if (ngx_abs(diff) < NGX_TIMER_LAZY_DELAY) {
  45.             ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
  46.                            "event timer: %d, old: %M, new: %M",
  47.                             ngx_event_ident(ev->data), ev->timer.key, key);
  48.             return;
  49.         }

  50.         ngx_del_timer(ev);
  51.     }

  52.     ev->timer.key = key;

  53.     ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
  54.                    "event timer add: %d: %M:%M",
  55.                     ngx_event_ident(ev->data), timer, ev->timer.key);

  56.     ngx_rbtree_insert(&ngx_event_timer_rbtree, &ev->timer);

  57.     ev->timer_set = 1;
  58. }


  59. #endif /* _NGX_EVENT_TIMER_H_INCLUDED_ */