Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #ifndef _NGX_EVENT_TIMER_H_INCLUDED_
0009 #define _NGX_EVENT_TIMER_H_INCLUDED_
0010 
0011 
0012 #include <ngx_config.h>
0013 #include <ngx_core.h>
0014 #include <ngx_event.h>
0015 
0016 
0017 #define NGX_TIMER_INFINITE  (ngx_msec_t) -1
0018 
0019 #define NGX_TIMER_LAZY_DELAY  300
0020 
0021 
0022 ngx_int_t ngx_event_timer_init(ngx_log_t *log);
0023 ngx_msec_t ngx_event_find_timer(void);
0024 void ngx_event_expire_timers(void);
0025 ngx_int_t ngx_event_no_timers_left(void);
0026 
0027 
0028 extern ngx_rbtree_t  ngx_event_timer_rbtree;
0029 
0030 
0031 static ngx_inline void
0032 ngx_event_del_timer(ngx_event_t *ev)
0033 {
0034     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0035                    "event timer del: %d: %M",
0036                     ngx_event_ident(ev->data), ev->timer.key);
0037 
0038     ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
0039 
0040 #if (NGX_DEBUG)
0041     ev->timer.left = NULL;
0042     ev->timer.right = NULL;
0043     ev->timer.parent = NULL;
0044 #endif
0045 
0046     ev->timer_set = 0;
0047 }
0048 
0049 
0050 static ngx_inline void
0051 ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
0052 {
0053     ngx_msec_t      key;
0054     ngx_msec_int_t  diff;
0055 
0056     key = ngx_current_msec + timer;
0057 
0058     if (ev->timer_set) {
0059 
0060         /*
0061          * Use a previous timer value if difference between it and a new
0062          * value is less than NGX_TIMER_LAZY_DELAY milliseconds: this allows
0063          * to minimize the rbtree operations for fast connections.
0064          */
0065 
0066         diff = (ngx_msec_int_t) (key - ev->timer.key);
0067 
0068         if (ngx_abs(diff) < NGX_TIMER_LAZY_DELAY) {
0069             ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0070                            "event timer: %d, old: %M, new: %M",
0071                             ngx_event_ident(ev->data), ev->timer.key, key);
0072             return;
0073         }
0074 
0075         ngx_del_timer(ev);
0076     }
0077 
0078     ev->timer.key = key;
0079 
0080     ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0081                    "event timer add: %d: %M:%M",
0082                     ngx_event_ident(ev->data), timer, ev->timer.key);
0083 
0084     ngx_rbtree_insert(&ngx_event_timer_rbtree, &ev->timer);
0085 
0086     ev->timer_set = 1;
0087 }
0088 
0089 
0090 #endif /* _NGX_EVENT_TIMER_H_INCLUDED_ */