Mercurial > hg > nginx
comparison src/core/ngx_times.c @ 370:54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sun, 27 Jun 2004 18:01:57 +0000 |
parents | 0a03c921c81d |
children | 80e72c428b39 |
comparison
equal
deleted
inserted
replaced
369:9c2515d70489 | 370:54f76b0b8dca |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 | 4 |
5 | 5 |
6 ngx_epoch_msec_t ngx_elapsed_msec; | |
7 ngx_epoch_msec_t ngx_old_elapsed_msec; | |
8 ngx_epoch_msec_t ngx_start_msec; | |
9 | |
10 static ngx_tm_t ngx_cached_gmtime; | |
11 static ngx_int_t ngx_gmtoff; | |
12 | |
13 | |
14 /* | |
15 * In the threaded mode only one thread updates cached time and strings | |
16 * and these operations are protected by the mutex. The reading of the cached | |
17 * time and strings is not protected by the mutex. To avoid the race | |
18 * conditions for non-atomic values we use the NGX_TIME_SLOTS slots to store | |
19 * time value and strings. Thus thread may get the corrupted values only | |
20 * if it is preempted while copying and then it is not scheduled to run | |
21 * more than NGX_TIME_SLOTS seconds. | |
22 */ | |
23 | |
6 #if (NGX_THREADS) | 24 #if (NGX_THREADS) |
7 static ngx_mutex_t *ngx_time_mutex; | 25 |
8 #endif | 26 #define NGX_TIME_SLOTS 60 |
9 | 27 static ngx_uint_t slot = NGX_TIME_SLOTS; |
10 | 28 |
11 ngx_epoch_msec_t ngx_elapsed_msec; | 29 static ngx_mutex_t *ngx_time_mutex; |
12 ngx_epoch_msec_t ngx_old_elapsed_msec; | 30 |
13 ngx_epoch_msec_t ngx_start_msec; | 31 #else |
14 | 32 |
15 volatile time_t ngx_cached_time; | 33 #define NGX_TIME_SLOTS 1 |
16 | 34 #define slot 0 |
17 volatile ngx_str_t ngx_cached_err_log_time; | 35 |
18 volatile ngx_str_t ngx_cached_http_time; | 36 #endif |
19 volatile ngx_str_t ngx_cached_http_log_time; | 37 |
20 | 38 |
21 static ngx_tm_t ngx_cached_gmtime; | 39 #if (NGX_THREADS && (TIME_T_SIZE > SIG_ATOMIC_T_SIZE)) |
22 static ngx_int_t ngx_gmtoff; | 40 |
23 | 41 volatile time_t *ngx_cached_time; |
24 static u_char cached_err_log_time0[] = "1970/09/28 12:00:00"; | 42 static time_t cached_time[NGX_TIME_SLOTS]; |
25 static u_char cached_err_log_time1[] = "1970/09/28 12:00:00"; | 43 |
26 | 44 #else |
27 static u_char cached_http_time0[] = "Mon, 28 Sep 1970 06:00:00 GMT"; | 45 |
28 static u_char cached_http_time1[] = "Mon, 28 Sep 1970 06:00:00 GMT"; | 46 volatile time_t ngx_cached_time; |
29 | 47 |
30 static u_char cached_http_log_time0[] = "28/Sep/1970:12:00:00 +0600"; | 48 #endif |
31 static u_char cached_http_log_time1[] = "28/Sep/1970:12:00:00 +0600"; | 49 |
50 | |
51 ngx_thread_volatile ngx_str_t ngx_cached_err_log_time; | |
52 ngx_thread_volatile ngx_str_t ngx_cached_http_time; | |
53 ngx_thread_volatile ngx_str_t ngx_cached_http_log_time; | |
54 | |
55 | |
56 static u_char cached_err_log_time[NGX_TIME_SLOTS] | |
57 [sizeof("1970/09/28 12:00:00")]; | |
58 static u_char cached_http_time[NGX_TIME_SLOTS] | |
59 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")]; | |
60 static u_char cached_http_log_time[NGX_TIME_SLOTS] | |
61 [sizeof("28/Sep/1970:12:00:00 +0600")]; | |
32 | 62 |
33 | 63 |
34 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; | 64 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
35 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 65 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
36 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | 66 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
43 ngx_memzero(&ngx_cached_gmtime, sizeof(ngx_tm_t)); | 73 ngx_memzero(&ngx_cached_gmtime, sizeof(ngx_tm_t)); |
44 #ifdef ngx_tm_zone | 74 #ifdef ngx_tm_zone |
45 ngx_cached_gmtime.ngx_tm_zone = "GMT"; | 75 ngx_cached_gmtime.ngx_tm_zone = "GMT"; |
46 #endif | 76 #endif |
47 | 77 |
48 ngx_cached_err_log_time.len = sizeof(cached_err_log_time0) - 1; | 78 ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1; |
49 ngx_cached_err_log_time.data = cached_err_log_time0; | 79 ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; |
50 | 80 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1; |
51 ngx_cached_http_time.len = sizeof(cached_http_time0) - 1; | 81 |
52 ngx_cached_http_time.data = cached_http_time0; | 82 #if (TIME_T_SIZE > SIG_ATOMIC_T_SIZE) |
53 | 83 ngx_cached_time = &cached_time[0]; |
54 ngx_cached_http_log_time.len = sizeof(cached_http_log_time0) - 1; | 84 #endif |
55 ngx_cached_http_log_time.data = cached_http_log_time0; | |
56 | |
57 ngx_cached_time = 0; | |
58 | 85 |
59 ngx_gettimeofday(&tv); | 86 ngx_gettimeofday(&tv); |
60 | 87 |
61 ngx_start_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; | 88 ngx_start_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
62 ngx_old_elapsed_msec = 0; | 89 ngx_old_elapsed_msec = 0; |
87 void ngx_time_update(time_t s) | 114 void ngx_time_update(time_t s) |
88 { | 115 { |
89 u_char *p; | 116 u_char *p; |
90 ngx_tm_t tm; | 117 ngx_tm_t tm; |
91 | 118 |
92 if (ngx_cached_time == s) { | 119 if (ngx_time() == s) { |
93 return; | 120 return; |
94 } | 121 } |
95 | 122 |
96 #if (NGX_THREADS) | 123 #if (NGX_THREADS) |
124 | |
97 if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) { | 125 if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) { |
98 return; | 126 return; |
99 } | 127 } |
100 #endif | 128 |
101 | 129 if (slot == NGX_TIME_SLOTS) { |
102 ngx_cached_time = s; | 130 slot = 0; |
103 | |
104 ngx_gmtime(ngx_cached_time, &ngx_cached_gmtime); | |
105 | |
106 | |
107 if (ngx_cached_http_time.data == cached_http_time0) { | |
108 p = cached_http_time1; | |
109 } else { | 131 } else { |
110 p = cached_http_time0; | 132 slot++; |
111 } | 133 } |
134 | |
135 #if (TIME_T_SIZE > SIG_ATOMIC_T_SIZE) | |
136 ngx_cached_time = &cached_time[slot]; | |
137 #endif | |
138 | |
139 #endif | |
140 | |
141 ngx_time() = s; | |
142 | |
143 ngx_gmtime(s, &ngx_cached_gmtime); | |
144 | |
145 | |
146 p = cached_http_time[slot]; | |
112 | 147 |
113 ngx_snprintf((char *) p, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"), | 148 ngx_snprintf((char *) p, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"), |
114 "%s, %02d %s %4d %02d:%02d:%02d GMT", | 149 "%s, %02d %s %4d %02d:%02d:%02d GMT", |
115 week[ngx_cached_gmtime.ngx_tm_wday], | 150 week[ngx_cached_gmtime.ngx_tm_wday], |
116 ngx_cached_gmtime.ngx_tm_mday, | 151 ngx_cached_gmtime.ngx_tm_mday, |
124 | 159 |
125 | 160 |
126 #if (HAVE_GETTIMEZONE) | 161 #if (HAVE_GETTIMEZONE) |
127 | 162 |
128 ngx_gmtoff = ngx_gettimezone(); | 163 ngx_gmtoff = ngx_gettimezone(); |
129 ngx_gmtime(ngx_cached_time + ngx_gmtoff * 60, &tm); | 164 ngx_gmtime(s + ngx_gmtoff * 60, &tm); |
130 | 165 |
131 #elif (HAVE_GMTOFF) | 166 #elif (HAVE_GMTOFF) |
132 | 167 |
133 ngx_localtime(&tm); | 168 ngx_localtime(&tm); |
134 ngx_gmtoff = tm.ngx_tm_gmtoff / 60; | 169 ngx_gmtoff = tm.ngx_tm_gmtoff / 60; |
139 ngx_gmtoff = ngx_timezone(tm.ngx_tm_isdst); | 174 ngx_gmtoff = ngx_timezone(tm.ngx_tm_isdst); |
140 | 175 |
141 #endif | 176 #endif |
142 | 177 |
143 | 178 |
144 if (ngx_cached_err_log_time.data == cached_err_log_time0) { | 179 p = cached_err_log_time[slot]; |
145 p = cached_err_log_time1; | |
146 } else { | |
147 p = cached_err_log_time0; | |
148 } | |
149 | 180 |
150 ngx_snprintf((char *) p, sizeof("1970/09/28 12:00:00"), | 181 ngx_snprintf((char *) p, sizeof("1970/09/28 12:00:00"), |
151 "%4d/%02d/%02d %02d:%02d:%02d", | 182 "%4d/%02d/%02d %02d:%02d:%02d", |
152 tm.ngx_tm_year, tm.ngx_tm_mon, | 183 tm.ngx_tm_year, tm.ngx_tm_mon, |
153 tm.ngx_tm_mday, tm.ngx_tm_hour, | 184 tm.ngx_tm_mday, tm.ngx_tm_hour, |
154 tm.ngx_tm_min, tm.ngx_tm_sec); | 185 tm.ngx_tm_min, tm.ngx_tm_sec); |
155 | 186 |
156 ngx_cached_err_log_time.data = p; | 187 ngx_cached_err_log_time.data = p; |
157 | 188 |
158 | 189 |
159 if (ngx_cached_http_log_time.data == cached_http_log_time0) { | 190 p = cached_http_log_time[slot]; |
160 p = cached_http_log_time1; | |
161 } else { | |
162 p = cached_http_log_time0; | |
163 } | |
164 | 191 |
165 ngx_snprintf((char *) p, sizeof("28/Sep/1970:12:00:00 +0600"), | 192 ngx_snprintf((char *) p, sizeof("28/Sep/1970:12:00:00 +0600"), |
166 "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d", | 193 "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d", |
167 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1], | 194 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1], |
168 tm.ngx_tm_year, tm.ngx_tm_hour, | 195 tm.ngx_tm_year, tm.ngx_tm_hour, |