Mercurial > hg > nginx
comparison src/event/ngx_event_timer.c @ 270:7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 25 Feb 2004 20:16:15 +0000 |
parents | 5238e93961a1 |
children | d4e65d74db9f |
comparison
equal
deleted
inserted
replaced
269:f082cb6bcdd7 | 270:7bb9562216ce |
---|---|
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_event.h> | 4 #include <ngx_event.h> |
5 | 5 |
6 | 6 |
7 /* | |
8 * TODO: in multithreaded enviroment all timer operations must be | |
9 * protected by the single mutex | |
10 */ | |
11 | |
12 #if (NGX_THREADS) | 7 #if (NGX_THREADS) |
13 static ngx_mutex_t *ngx_event_timer_mutex; | 8 ngx_mutex_t *ngx_event_timer_mutex; |
14 #endif | 9 #endif |
15 | 10 |
16 | 11 |
17 ngx_rbtree_t *ngx_event_timer_rbtree; | 12 ngx_rbtree_t *ngx_event_timer_rbtree; |
18 ngx_rbtree_t ngx_event_timer_sentinel; | 13 ngx_rbtree_t ngx_event_timer_sentinel; |
19 | 14 |
20 | 15 |
21 void ngx_event_timer_init(void) | 16 ngx_int_t ngx_event_timer_init(ngx_log_t *log) |
22 { | 17 { |
23 if (ngx_event_timer_rbtree) { | 18 if (ngx_event_timer_rbtree) { |
24 return; | 19 ngx_event_timer_mutex->log = log; |
20 return NGX_OK; | |
25 } | 21 } |
26 | 22 |
27 ngx_event_timer_rbtree = &ngx_event_timer_sentinel; | 23 ngx_event_timer_rbtree = &ngx_event_timer_sentinel; |
24 | |
25 if (!(ngx_event_timer_mutex = ngx_mutex_init(log, 0))) { | |
26 return NGX_ERROR; | |
27 } | |
28 | |
29 return NGX_OK; | |
28 } | 30 } |
29 | 31 |
30 | 32 |
31 ngx_msec_t ngx_event_find_timer(void) | 33 ngx_msec_t ngx_event_find_timer(void) |
32 { | 34 { |
34 | 36 |
35 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) { | 37 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) { |
36 return 0; | 38 return 0; |
37 } | 39 } |
38 | 40 |
41 #if (NGX_THREADS) | |
42 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { | |
43 return NGX_TIMER_ERROR; | |
44 } | |
45 #endif | |
46 | |
39 node = ngx_rbtree_min(ngx_event_timer_rbtree, &ngx_event_timer_sentinel); | 47 node = ngx_rbtree_min(ngx_event_timer_rbtree, &ngx_event_timer_sentinel); |
48 | |
49 #if (NGX_THREADS) | |
50 ngx_mutex_unlock(ngx_event_timer_mutex); | |
51 #endif | |
40 | 52 |
41 return (ngx_msec_t) | 53 return (ngx_msec_t) |
42 (node->key * NGX_TIMER_RESOLUTION - | 54 (node->key * NGX_TIMER_RESOLUTION - |
43 ngx_elapsed_msec / NGX_TIMER_RESOLUTION * NGX_TIMER_RESOLUTION); | 55 ngx_elapsed_msec / NGX_TIMER_RESOLUTION * NGX_TIMER_RESOLUTION); |
44 #if 0 | 56 #if 0 |
56 | 68 |
57 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) { | 69 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) { |
58 break; | 70 break; |
59 } | 71 } |
60 | 72 |
73 #if (NGX_THREADS) | |
74 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { | |
75 return; | |
76 } | |
77 #endif | |
78 | |
61 node = ngx_rbtree_min(ngx_event_timer_rbtree, | 79 node = ngx_rbtree_min(ngx_event_timer_rbtree, |
62 &ngx_event_timer_sentinel); | 80 &ngx_event_timer_sentinel); |
81 | |
82 #if (NGX_THREADS) | |
83 ngx_mutex_unlock(ngx_event_timer_mutex); | |
84 #endif | |
63 | 85 |
64 if ((ngx_msec_t) node->key <= (ngx_msec_t) | 86 if ((ngx_msec_t) node->key <= (ngx_msec_t) |
65 (ngx_old_elapsed_msec + timer) / NGX_TIMER_RESOLUTION) | 87 (ngx_old_elapsed_msec + timer) / NGX_TIMER_RESOLUTION) |
66 { | 88 { |
67 ev = (ngx_event_t *) | 89 ev = (ngx_event_t *) |
77 | 99 |
78 } else { | 100 } else { |
79 ev->timedout = 1; | 101 ev->timedout = 1; |
80 } | 102 } |
81 | 103 |
104 #if (NGX_THREADS) | |
105 /* STUB: post event */ | |
106 #endif | |
107 | |
82 ev->event_handler(ev); | 108 ev->event_handler(ev); |
83 continue; | 109 continue; |
84 } | 110 } |
85 | 111 |
86 break; | 112 break; |