1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "util_time.h"
19 /* Cache for exploded values of recent timestamps
22 struct exploded_time_cache_element {
25 apr_int64_t t_validate; /* please see comments in cached_explode() */
28 /* the "+ 1" is for the current second: */
29 #define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
31 /* Note that AP_TIME_RECENT_THRESHOLD is defined to
32 * be a power of two minus one in util_time.h, so that
33 * we can replace a modulo operation with a bitwise AND
34 * when hashing items into a cache of size
35 * AP_TIME_RECENT_THRESHOLD+1
37 #define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
39 static struct exploded_time_cache_element exploded_cache_localtime[TIME_CACHE_SIZE];
40 static struct exploded_time_cache_element exploded_cache_gmt[TIME_CACHE_SIZE];
43 static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t,
44 struct exploded_time_cache_element *cache,
47 apr_int64_t seconds = apr_time_sec(t);
48 struct exploded_time_cache_element *cache_element =
49 &(cache[seconds & TIME_CACHE_MASK]);
50 struct exploded_time_cache_element cache_element_snapshot;
52 /* The cache is implemented as a ring buffer. Each second,
53 * it uses a different element in the buffer. The timestamp
54 * in the element indicates whether the element contains the
55 * exploded time for the current second (vs the time
56 * 'now - AP_TIME_RECENT_THRESHOLD' seconds ago). If the
57 * cached value is for the current time, we use it. Otherwise,
58 * we compute the apr_time_exp_t and store it in this
59 * cache element. Note that the timestamp in the cache
60 * element is updated only after the exploded time. Thus
61 * if two threads hit this cache element simultaneously
62 * at the start of a new second, they'll both explode the
63 * time and store it. I.e., the writers will collide, but
64 * they'll be writing the same value.
66 if (cache_element->t >= seconds) {
67 /* There is an intentional race condition in this design:
68 * in a multithreaded app, one thread might be reading
69 * from this cache_element to resolve a timestamp from
70 * TIME_CACHE_SIZE seconds ago at the same time that
71 * another thread is copying the exploded form of the
72 * current time into the same cache_element. (I.e., the
73 * first thread might hit this element of the ring buffer
74 * just as the element is being recycled.) This can
75 * also happen at the start of a new second, if a
76 * reader accesses the cache_element after a writer
77 * has updated cache_element.t but before the writer
78 * has finished updating the whole cache_element.
80 * Rather than trying to prevent this race condition
81 * with locks, we allow it to happen and then detect
82 * and correct it. The detection works like this:
83 * Step 1: Take a "snapshot" of the cache element by
84 * copying it into a temporary buffer.
85 * Step 2: Check whether the snapshot contains consistent
86 * data: the timestamps at the start and end of
87 * the cache_element should both match the 'seconds'
88 * value that we computed from the input time.
89 * If these three don't match, then the snapshot
90 * shows the cache_element in the middle of an
91 * update, and its contents are invalid.
92 * Step 3: If the snapshot is valid, use it. Otherwise,
93 * just give up on the cache and explode the
96 memcpy(&cache_element_snapshot, cache_element,
97 sizeof(struct exploded_time_cache_element));
98 if ((seconds != cache_element_snapshot.t) ||
99 (seconds != cache_element_snapshot.t_validate)) {
100 /* Invalid snapshot */
102 return apr_time_exp_gmt(xt, t);
105 return apr_time_exp_lt(xt, t);
110 memcpy(xt, &(cache_element_snapshot.xt),
111 sizeof(apr_time_exp_t));
117 r = apr_time_exp_gmt(xt, t);
120 r = apr_time_exp_lt(xt, t);
122 if (r != APR_SUCCESS) {
125 cache_element->t = seconds;
126 memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
127 cache_element->t_validate = seconds;
129 xt->tm_usec = (int)apr_time_usec(t);
134 AP_DECLARE(apr_status_t) ap_explode_recent_localtime(apr_time_exp_t * tm,
137 return cached_explode(tm, t, exploded_cache_localtime, 0);
140 AP_DECLARE(apr_status_t) ap_explode_recent_gmt(apr_time_exp_t * tm,
143 return cached_explode(tm, t, exploded_cache_gmt, 1);
146 AP_DECLARE(apr_status_t) ap_recent_ctime(char *date_str, apr_time_t t)
148 /* ### This code is a clone of apr_ctime(), except that it
149 * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
155 /* example: "Wed Jun 30 21:49:08 1993" */
156 /* 123456789012345678901234 */
158 ap_explode_recent_localtime(&xt, t);
159 s = &apr_day_snames[xt.tm_wday][0];
164 s = &apr_month_snames[xt.tm_mon][0];
169 *date_str++ = xt.tm_mday / 10 + '0';
170 *date_str++ = xt.tm_mday % 10 + '0';
172 *date_str++ = xt.tm_hour / 10 + '0';
173 *date_str++ = xt.tm_hour % 10 + '0';
175 *date_str++ = xt.tm_min / 10 + '0';
176 *date_str++ = xt.tm_min % 10 + '0';
178 *date_str++ = xt.tm_sec / 10 + '0';
179 *date_str++ = xt.tm_sec % 10 + '0';
181 real_year = 1900 + xt.tm_year;
182 *date_str++ = real_year / 1000 + '0';
183 *date_str++ = real_year % 1000 / 100 + '0';
184 *date_str++ = real_year % 100 / 10 + '0';
185 *date_str++ = real_year % 10 + '0';
191 AP_DECLARE(apr_status_t) ap_recent_rfc822_date(char *date_str, apr_time_t t)
193 /* ### This code is a clone of apr_rfc822_date(), except that it
194 * uses ap_explode_recent_gmt() instead of apr_time_exp_gmt().
200 ap_explode_recent_gmt(&xt, t);
202 /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
203 /* 12345678901234567890123456789 */
205 s = &apr_day_snames[xt.tm_wday][0];
211 *date_str++ = xt.tm_mday / 10 + '0';
212 *date_str++ = xt.tm_mday % 10 + '0';
214 s = &apr_month_snames[xt.tm_mon][0];
219 real_year = 1900 + xt.tm_year;
220 /* This routine isn't y10k ready. */
221 *date_str++ = real_year / 1000 + '0';
222 *date_str++ = real_year % 1000 / 100 + '0';
223 *date_str++ = real_year % 100 / 10 + '0';
224 *date_str++ = real_year % 10 + '0';
226 *date_str++ = xt.tm_hour / 10 + '0';
227 *date_str++ = xt.tm_hour % 10 + '0';
229 *date_str++ = xt.tm_min / 10 + '0';
230 *date_str++ = xt.tm_min % 10 + '0';
232 *date_str++ = xt.tm_sec / 10 + '0';
233 *date_str++ = xt.tm_sec % 10 + '0';