src/event/ngx_event_timer.c - nginx-1.7.10

Global variables defined

Functions defined

Source code


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


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_event.h>


  8. ngx_rbtree_t              ngx_event_timer_rbtree;
  9. static ngx_rbtree_node_t  ngx_event_timer_sentinel;

  10. /*
  11. * the event timer rbtree may contain the duplicate keys, however,
  12. * it should not be a problem, because we use the rbtree to find
  13. * a minimum timer value only
  14. */

  15. ngx_int_t
  16. ngx_event_timer_init(ngx_log_t *log)
  17. {
  18.     ngx_rbtree_init(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel,
  19.                     ngx_rbtree_insert_timer_value);

  20.     return NGX_OK;
  21. }


  22. ngx_msec_t
  23. ngx_event_find_timer(void)
  24. {
  25.     ngx_msec_int_t      timer;
  26.     ngx_rbtree_node_t  *node, *root, *sentinel;

  27.     if (ngx_event_timer_rbtree.root == &ngx_event_timer_sentinel) {
  28.         return NGX_TIMER_INFINITE;
  29.     }

  30.     root = ngx_event_timer_rbtree.root;
  31.     sentinel = ngx_event_timer_rbtree.sentinel;

  32.     node = ngx_rbtree_min(root, sentinel);

  33.     timer = (ngx_msec_int_t) (node->key - ngx_current_msec);

  34.     return (ngx_msec_t) (timer > 0 ? timer : 0);
  35. }


  36. void
  37. ngx_event_expire_timers(void)
  38. {
  39.     ngx_event_t        *ev;
  40.     ngx_rbtree_node_t  *node, *root, *sentinel;

  41.     sentinel = ngx_event_timer_rbtree.sentinel;

  42.     for ( ;; ) {
  43.         root = ngx_event_timer_rbtree.root;

  44.         if (root == sentinel) {
  45.             return;
  46.         }

  47.         node = ngx_rbtree_min(root, sentinel);

  48.         /* node->key > ngx_current_time */

  49.         if ((ngx_msec_int_t) (node->key - ngx_current_msec) > 0) {
  50.             return;
  51.         }

  52.         ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));

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

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

  57. #if (NGX_DEBUG)
  58.         ev->timer.left = NULL;
  59.         ev->timer.right = NULL;
  60.         ev->timer.parent = NULL;
  61. #endif

  62.         ev->timer_set = 0;

  63.         ev->timedout = 1;

  64.         ev->handler(ev);
  65.     }
  66. }


  67. void
  68. ngx_event_cancel_timers(void)
  69. {
  70.     ngx_event_t        *ev;
  71.     ngx_rbtree_node_t  *node, *root, *sentinel;

  72.     sentinel = ngx_event_timer_rbtree.sentinel;

  73.     for ( ;; ) {
  74.         root = ngx_event_timer_rbtree.root;

  75.         if (root == sentinel) {
  76.             return;
  77.         }

  78.         node = ngx_rbtree_min(root, sentinel);

  79.         ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));

  80.         if (!ev->cancelable) {
  81.             return;
  82.         }

  83.         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
  84.                        "event timer cancel: %d: %M",
  85.                        ngx_event_ident(ev->data), ev->timer.key);

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

  87. #if (NGX_DEBUG)
  88.         ev->timer.left = NULL;
  89.         ev->timer.right = NULL;
  90.         ev->timer.parent = NULL;
  91. #endif

  92.         ev->timer_set = 0;

  93.         ev->handler(ev);
  94.     }
  95. }