Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ngx_event_timer.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
6 
7 
8 #ifndef _NGX_EVENT_TIMER_H_INCLUDED_
9 #define _NGX_EVENT_TIMER_H_INCLUDED_
10 
11 
12 #include <ngx_config.h>
13 #include <ngx_core.h>
14 #include <ngx_event.h>
15 
16 
17 #define NGX_TIMER_INFINITE (ngx_msec_t) -1
18 
19 #define NGX_TIMER_LAZY_DELAY 300
20 
21 
24 void ngx_event_expire_timers(void);
25 
26 
27 #if (NGX_THREADS)
28 extern ngx_mutex_t *ngx_event_timer_mutex;
29 #endif
30 
31 
33 
34 
35 static ngx_inline void
36 ngx_event_del_timer(ngx_event_t *ev)
37 {
39  "event timer del: %d: %M",
40  ngx_event_ident(ev->data), ev->timer.key);
41 
42  ngx_mutex_lock(ngx_event_timer_mutex);
43 
45 
46  ngx_mutex_unlock(ngx_event_timer_mutex);
47 
48 #if (NGX_DEBUG)
49  ev->timer.left = NULL;
50  ev->timer.right = NULL;
51  ev->timer.parent = NULL;
52 #endif
53 
54  ev->timer_set = 0;
55 }
56 
57 
58 static ngx_inline void
59 ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
60 {
61  ngx_msec_t key;
62  ngx_msec_int_t diff;
63 
64  key = ngx_current_msec + timer;
65 
66  if (ev->timer_set) {
67 
68  /*
69  * Use a previous timer value if difference between it and a new
70  * value is less than NGX_TIMER_LAZY_DELAY milliseconds: this allows
71  * to minimize the rbtree operations for fast connections.
72  */
73 
74  diff = (ngx_msec_int_t) (key - ev->timer.key);
75 
76  if (ngx_abs(diff) < NGX_TIMER_LAZY_DELAY) {
78  "event timer: %d, old: %M, new: %M",
79  ngx_event_ident(ev->data), ev->timer.key, key);
80  return;
81  }
82 
83  ngx_del_timer(ev);
84  }
85 
86  ev->timer.key = key;
87 
89  "event timer add: %d: %M:%M",
90  ngx_event_ident(ev->data), timer, ev->timer.key);
91 
92  ngx_mutex_lock(ngx_event_timer_mutex);
93 
95 
96  ngx_mutex_unlock(ngx_event_timer_mutex);
97 
98  ev->timer_set = 1;
99 }
100 
101 
102 #endif /* _NGX_EVENT_TIMER_H_INCLUDED_ */