1 /* Copyright 2001-2004 The Apache Software Foundation
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "util_time.h"
18 /* Cache for exploded values of recent timestamps
21 struct exploded_time_cache_element {
24 apr_int64_t t_validate; /* please see comments in cached_explode() */
27 /* the "+ 1" is for the current second: */
28 #define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
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
36 #define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
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];
42 static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t,
43 struct exploded_time_cache_element *cache,
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;
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.
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.
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
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 */
101 return apr_time_exp_gmt(xt, t);
104 return apr_time_exp_lt(xt, t);
109 memcpy(xt, &(cache_element_snapshot.xt),
110 sizeof(apr_time_exp_t));
116 r = apr_time_exp_gmt(xt, t);
119 r = apr_time_exp_lt(xt, t);
121 if (!APR_STATUS_IS_SUCCESS(r)) {
124 cache_element->t = seconds;
125 memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
126 cache_element->t_validate = seconds;
128 xt->tm_usec = (int)apr_time_usec(t);
133 AP_DECLARE(apr_status_t) ap_explode_recent_localtime(apr_time_exp_t * tm,
136 return cached_explode(tm, t, exploded_cache_localtime, 0);
139 AP_DECLARE(apr_status_t) ap_explode_recent_gmt(apr_time_exp_t * tm,
142 return cached_explode(tm, t, exploded_cache_gmt, 1);
145 AP_DECLARE(apr_status_t) ap_recent_ctime(char *date_str, apr_time_t t)
147 /* ### This code is a clone of apr_ctime(), except that it
148 * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
154 /* example: "Wed Jun 30 21:49:08 1993" */
155 /* 123456789012345678901234 */
157 ap_explode_recent_localtime(&xt, t);
158 s = &apr_day_snames[xt.tm_wday][0];
163 s = &apr_month_snames[xt.tm_mon][0];
168 *date_str++ = xt.tm_mday / 10 + '0';
169 *date_str++ = xt.tm_mday % 10 + '0';
171 *date_str++ = xt.tm_hour / 10 + '0';
172 *date_str++ = xt.tm_hour % 10 + '0';
174 *date_str++ = xt.tm_min / 10 + '0';
175 *date_str++ = xt.tm_min % 10 + '0';
177 *date_str++ = xt.tm_sec / 10 + '0';
178 *date_str++ = xt.tm_sec % 10 + '0';
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';
190 AP_DECLARE(apr_status_t) ap_recent_rfc822_date(char *date_str, apr_time_t t)
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().
199 ap_explode_recent_gmt(&xt, t);
201 /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
202 /* 12345678901234567890123456789 */
204 s = &apr_day_snames[xt.tm_wday][0];
210 *date_str++ = xt.tm_mday / 10 + '0';
211 *date_str++ = xt.tm_mday % 10 + '0';
213 s = &apr_month_snames[xt.tm_mon][0];
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';
225 *date_str++ = xt.tm_hour / 10 + '0';
226 *date_str++ = xt.tm_hour % 10 + '0';
228 *date_str++ = xt.tm_min / 10 + '0';
229 *date_str++ = xt.tm_min % 10 + '0';
231 *date_str++ = xt.tm_sec / 10 + '0';
232 *date_str++ = xt.tm_sec % 10 + '0';