]> granicus.if.org Git - apache/blob - server/util_time.c
fix name of The Apache Software Foundation
[apache] / server / util_time.c
1 /* Copyright 2001-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "util_time.h"
17
18 /* Cache for exploded values of recent timestamps
19  */
20
21 struct exploded_time_cache_element {
22     apr_int64_t t;
23     apr_time_exp_t xt;
24     apr_int64_t t_validate; /* please see comments in cached_explode() */
25 };
26
27 /* the "+ 1" is for the current second: */
28 #define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
29
30 /* Note that AP_TIME_RECENT_THRESHOLD is defined to
31  * be a power of two minus one in util_time.h, so that
32  * we can replace a modulo operation with a bitwise AND
33  * when hashing items into a cache of size
34  * AP_TIME_RECENT_THRESHOLD+1
35  */
36 #define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
37
38 static struct exploded_time_cache_element exploded_cache_localtime[TIME_CACHE_SIZE];
39 static struct exploded_time_cache_element exploded_cache_gmt[TIME_CACHE_SIZE];
40
41
42 static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t,
43                                    struct exploded_time_cache_element *cache,
44                                    int use_gmt)
45 {
46     apr_int64_t seconds = apr_time_sec(t);
47     struct exploded_time_cache_element *cache_element =
48         &(cache[seconds & TIME_CACHE_MASK]);
49     struct exploded_time_cache_element cache_element_snapshot;
50
51     /* The cache is implemented as a ring buffer.  Each second,
52      * it uses a different element in the buffer.  The timestamp
53      * in the element indicates whether the element contains the
54      * exploded time for the current second (vs the time
55      * 'now - AP_TIME_RECENT_THRESHOLD' seconds ago).  If the
56      * cached value is for the current time, we use it.  Otherwise,
57      * we compute the apr_time_exp_t and store it in this
58      * cache element. Note that the timestamp in the cache
59      * element is updated only after the exploded time.  Thus
60      * if two threads hit this cache element simultaneously
61      * at the start of a new second, they'll both explode the
62      * time and store it.  I.e., the writers will collide, but
63      * they'll be writing the same value.
64      */
65     if (cache_element->t >= seconds) {
66         /* There is an intentional race condition in this design:
67          * in a multithreaded app, one thread might be reading
68          * from this cache_element to resolve a timestamp from
69          * TIME_CACHE_SIZE seconds ago at the same time that
70          * another thread is copying the exploded form of the
71          * current time into the same cache_element.  (I.e., the
72          * first thread might hit this element of the ring buffer
73          * just as the element is being recycled.)  This can
74          * also happen at the start of a new second, if a
75          * reader accesses the cache_element after a writer
76          * has updated cache_element.t but before the writer
77          * has finished updating the whole cache_element.
78          *
79          * Rather than trying to prevent this race condition
80          * with locks, we allow it to happen and then detect
81          * and correct it.  The detection works like this:
82          *   Step 1: Take a "snapshot" of the cache element by
83          *           copying it into a temporary buffer.
84          *   Step 2: Check whether the snapshot contains consistent
85          *           data: the timestamps at the start and end of
86          *           the cache_element should both match the 'seconds'
87          *           value that we computed from the input time.
88          *           If these three don't match, then the snapshot
89          *           shows the cache_element in the middle of an
90          *           update, and its contents are invalid.
91          *   Step 3: If the snapshot is valid, use it.  Otherwise,
92          *           just give up on the cache and explode the
93          *           input time.
94          */
95         memcpy(&cache_element_snapshot, cache_element,
96                sizeof(struct exploded_time_cache_element));
97         if ((seconds != cache_element_snapshot.t) ||
98             (seconds != cache_element_snapshot.t_validate)) {
99             /* Invalid snapshot */
100             if (use_gmt) {
101                 return apr_time_exp_gmt(xt, t);
102             }
103             else {
104                 return apr_time_exp_lt(xt, t);
105             }
106         }
107         else {
108             /* Valid snapshot */
109             memcpy(xt, &(cache_element_snapshot.xt),
110                    sizeof(apr_time_exp_t));
111         }
112     }
113     else {
114         apr_status_t r;
115         if (use_gmt) {
116             r = apr_time_exp_gmt(xt, t);
117         }
118         else {
119             r = apr_time_exp_lt(xt, t);
120         }
121         if (!APR_STATUS_IS_SUCCESS(r)) {
122             return r;
123         }
124         cache_element->t = seconds;
125         memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
126         cache_element->t_validate = seconds;
127     }
128     xt->tm_usec = (int)apr_time_usec(t);
129     return APR_SUCCESS;
130 }
131
132
133 AP_DECLARE(apr_status_t) ap_explode_recent_localtime(apr_time_exp_t * tm,
134                                                      apr_time_t t)
135 {
136     return cached_explode(tm, t, exploded_cache_localtime, 0);
137 }
138
139 AP_DECLARE(apr_status_t) ap_explode_recent_gmt(apr_time_exp_t * tm,
140                                                apr_time_t t)
141 {
142     return cached_explode(tm, t, exploded_cache_gmt, 1);
143 }
144
145 AP_DECLARE(apr_status_t) ap_recent_ctime(char *date_str, apr_time_t t)
146 {
147     /* ### This code is a clone of apr_ctime(), except that it
148      * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
149      */
150     apr_time_exp_t xt;
151     const char *s;
152     int real_year;
153
154     /* example: "Wed Jun 30 21:49:08 1993" */
155     /*           123456789012345678901234  */
156
157     ap_explode_recent_localtime(&xt, t);
158     s = &apr_day_snames[xt.tm_wday][0];
159     *date_str++ = *s++;
160     *date_str++ = *s++;
161     *date_str++ = *s++;
162     *date_str++ = ' ';
163     s = &apr_month_snames[xt.tm_mon][0];
164     *date_str++ = *s++;
165     *date_str++ = *s++;
166     *date_str++ = *s++;
167     *date_str++ = ' ';
168     *date_str++ = xt.tm_mday / 10 + '0';
169     *date_str++ = xt.tm_mday % 10 + '0';
170     *date_str++ = ' ';
171     *date_str++ = xt.tm_hour / 10 + '0';
172     *date_str++ = xt.tm_hour % 10 + '0';
173     *date_str++ = ':';
174     *date_str++ = xt.tm_min / 10 + '0';
175     *date_str++ = xt.tm_min % 10 + '0';
176     *date_str++ = ':';
177     *date_str++ = xt.tm_sec / 10 + '0';
178     *date_str++ = xt.tm_sec % 10 + '0';
179     *date_str++ = ' ';
180     real_year = 1900 + xt.tm_year;
181     *date_str++ = real_year / 1000 + '0';
182     *date_str++ = real_year % 1000 / 100 + '0';
183     *date_str++ = real_year % 100 / 10 + '0';
184     *date_str++ = real_year % 10 + '0';
185     *date_str++ = 0;
186
187     return APR_SUCCESS;
188 }
189
190 AP_DECLARE(apr_status_t) ap_recent_rfc822_date(char *date_str, apr_time_t t)
191 {
192     /* ### This code is a clone of apr_rfc822_date(), except that it
193      * uses ap_explode_recent_gmt() instead of apr_time_exp_gmt().
194      */
195     apr_time_exp_t xt;
196     const char *s;
197     int real_year;
198
199     ap_explode_recent_gmt(&xt, t);
200
201     /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
202     /*           12345678901234567890123456789  */
203
204     s = &apr_day_snames[xt.tm_wday][0];
205     *date_str++ = *s++;
206     *date_str++ = *s++;
207     *date_str++ = *s++;
208     *date_str++ = ',';
209     *date_str++ = ' ';
210     *date_str++ = xt.tm_mday / 10 + '0';
211     *date_str++ = xt.tm_mday % 10 + '0';
212     *date_str++ = ' ';
213     s = &apr_month_snames[xt.tm_mon][0];
214     *date_str++ = *s++;
215     *date_str++ = *s++;
216     *date_str++ = *s++;
217     *date_str++ = ' ';
218     real_year = 1900 + xt.tm_year;
219     /* This routine isn't y10k ready. */
220     *date_str++ = real_year / 1000 + '0';
221     *date_str++ = real_year % 1000 / 100 + '0';
222     *date_str++ = real_year % 100 / 10 + '0';
223     *date_str++ = real_year % 10 + '0';
224     *date_str++ = ' ';
225     *date_str++ = xt.tm_hour / 10 + '0';
226     *date_str++ = xt.tm_hour % 10 + '0';
227     *date_str++ = ':';
228     *date_str++ = xt.tm_min / 10 + '0';
229     *date_str++ = xt.tm_min % 10 + '0';
230     *date_str++ = ':';
231     *date_str++ = xt.tm_sec / 10 + '0';
232     *date_str++ = xt.tm_sec % 10 + '0';
233     *date_str++ = ' ';
234     *date_str++ = 'G';
235     *date_str++ = 'M';
236     *date_str++ = 'T';
237     *date_str++ = 0;
238     return APR_SUCCESS;
239 }