Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_event.h>
0011 
0012 
0013 ngx_rbtree_t              ngx_event_timer_rbtree;
0014 static ngx_rbtree_node_t  ngx_event_timer_sentinel;
0015 
0016 /*
0017  * the event timer rbtree may contain the duplicate keys, however,
0018  * it should not be a problem, because we use the rbtree to find
0019  * a minimum timer value only
0020  */
0021 
0022 ngx_int_t
0023 ngx_event_timer_init(ngx_log_t *log)
0024 {
0025     ngx_rbtree_init(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel,
0026                     ngx_rbtree_insert_timer_value);
0027 
0028     return NGX_OK;
0029 }
0030 
0031 
0032 ngx_msec_t
0033 ngx_event_find_timer(void)
0034 {
0035     ngx_msec_int_t      timer;
0036     ngx_rbtree_node_t  *node, *root, *sentinel;
0037 
0038     if (ngx_event_timer_rbtree.root == &ngx_event_timer_sentinel) {
0039         return NGX_TIMER_INFINITE;
0040     }
0041 
0042     root = ngx_event_timer_rbtree.root;
0043     sentinel = ngx_event_timer_rbtree.sentinel;
0044 
0045     node = ngx_rbtree_min(root, sentinel);
0046 
0047     timer = (ngx_msec_int_t) (node->key - ngx_current_msec);
0048 
0049     return (ngx_msec_t) (timer > 0 ? timer : 0);
0050 }
0051 
0052 
0053 void
0054 ngx_event_expire_timers(void)
0055 {
0056     ngx_event_t        *ev;
0057     ngx_rbtree_node_t  *node, *root, *sentinel;
0058 
0059     sentinel = ngx_event_timer_rbtree.sentinel;
0060 
0061     for ( ;; ) {
0062         root = ngx_event_timer_rbtree.root;
0063 
0064         if (root == sentinel) {
0065             return;
0066         }
0067 
0068         node = ngx_rbtree_min(root, sentinel);
0069 
0070         /* node->key > ngx_current_msec */
0071 
0072         if ((ngx_msec_int_t) (node->key - ngx_current_msec) > 0) {
0073             return;
0074         }
0075 
0076         ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));
0077 
0078         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0079                        "event timer del: %d: %M",
0080                        ngx_event_ident(ev->data), ev->timer.key);
0081 
0082         ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
0083 
0084 #if (NGX_DEBUG)
0085         ev->timer.left = NULL;
0086         ev->timer.right = NULL;
0087         ev->timer.parent = NULL;
0088 #endif
0089 
0090         ev->timer_set = 0;
0091 
0092         ev->timedout = 1;
0093 
0094         ev->handler(ev);
0095     }
0096 }
0097 
0098 
0099 ngx_int_t
0100 ngx_event_no_timers_left(void)
0101 {
0102     ngx_event_t        *ev;
0103     ngx_rbtree_node_t  *node, *root, *sentinel;
0104 
0105     sentinel = ngx_event_timer_rbtree.sentinel;
0106     root = ngx_event_timer_rbtree.root;
0107 
0108     if (root == sentinel) {
0109         return NGX_OK;
0110     }
0111 
0112     for (node = ngx_rbtree_min(root, sentinel);
0113          node;
0114          node = ngx_rbtree_next(&ngx_event_timer_rbtree, node))
0115     {
0116         ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));
0117 
0118         if (!ev->cancelable) {
0119             return NGX_AGAIN;
0120         }
0121     }
0122 
0123     /* only cancelable timers left */
0124 
0125     return NGX_OK;
0126 }