Files
semi-libc/src/time/tzset.c
T
Rich Felker 4750cf4202 ditch the priority inheritance locks; use malloc's version of lock
i did some testing trying to switch malloc to use the new internal
lock with priority inheritance, and my malloc contention test got
20-100 times slower. if priority inheritance futexes are this slow,
it's simply too high a price to pay for avoiding priority inversion.
maybe we can consider them somewhere down the road once the kernel
folks get their act together on this (and perferably don't link it to
glibc's inefficient lock API)...

as such, i've switch __lock to use malloc's implementation of
lightweight locks, and updated all the users of the code to use an
array with a waiter count for their locks. this should give optimal
performance in the vast majority of cases, and it's simple.

malloc is still using its own internal copy of the lock code because
it seems to yield measurably better performance with -O3 when it's
inlined (20% or more difference in the contention stress test).
2012-04-24 16:32:23 -04:00

173 lines
3.7 KiB
C

#include <time.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "libc.h"
#include "__time.h"
long __timezone = 0;
int __daylight = 0;
char *__tzname[2] = { 0, 0 };
int __dst_offset = 0;
weak_alias(__timezone, timezone);
weak_alias(__daylight, daylight);
weak_alias(__tzname, tzname);
static char std_name[TZNAME_MAX+1];
static char dst_name[TZNAME_MAX+1];
/* all elements are zero-based */
static struct rule {
signed char month;
signed char week;
short day;
int time;
} __dst_start, __dst_end;
static void zname(char *d, char **s)
{
int i;
for (i=0; i<TZNAME_MAX && isalpha(d[i]=**s); i++, (*s)++);
d[i] = 0;
}
static int hhmmss(char **s)
{
int ofs = strtol(*s, s, 10)*3600;
if (ofs >= 0) {
if (**s == ':') ofs += strtol(*s+1, s, 10)*60;
if (**s == ':') ofs += strtol(*s+1, s, 10);
} else {
if (**s == ':') ofs -= strtol(*s+1, s, 10)*60;
if (**s == ':') ofs -= strtol(*s+1, s, 10);
}
return ofs;
}
static int dstrule(struct rule *rule, char **s)
{
if (**s != ',') return -1;
switch (*++*s) {
case 'J':
rule->month = 'J';
rule->day = strtol(*s+1, s, 10)-1;
break;
case 'M':
rule->month = strtol(*s+1, s, 10)-1;
if (**s != '.' || rule->month < 0 || rule->month > 11)
return -1;
rule->week = strtol(*s+1, s, 10)-1;
if (**s != '.' || rule->week < 0 || rule->week > 4)
return -1;
rule->day = strtol(*s+1, s, 10);
if (rule->day < 0 || rule->day > 6)
return -1;
break;
default:
rule->month = 'L';
rule->day = strtol(*s+1, s, 10);
break;
}
if (**s == '/') {
(*s)++;
rule->time = hhmmss(s);
} else rule->time = 7200;
return 0;
}
void tzset(void)
{
char *z, *a;
strcpy(std_name, "GMT");
strcpy(dst_name, "GMT");
__tzname[0] = std_name;
__tzname[1] = dst_name;
__timezone = 0;
__daylight = 0;
if (!(z = getenv("TZ")) || !isalpha(*z)) return;
zname(std_name, &z);
__timezone = hhmmss(&z);
zname(dst_name, &z);
if (dst_name[0]) __daylight=1;
a = z;
__dst_offset = hhmmss(&z) - __timezone;
if (z==a) __dst_offset = -3600;
if (dstrule(&__dst_start, &z) || dstrule(&__dst_end, &z))
__daylight = 0;
}
void __tzset(void)
{
static int lock[2], init;
if (init) return;
LOCK(lock);
if (!init) tzset();
init=1;
UNLOCK(lock);
}
static int is_leap(int year)
{
year -= 100;
return !(year&3) && ((year%100) || !(year%400));
}
static int cutoff_yday(struct tm *tm, struct rule *rule)
{
static const char days_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
static const int first_day[] = {0,31,59,90,120,151,181,212,243,273,304,335};
int yday, mday, leap;
switch (rule->month) {
case 'J':
return rule->day + (tm->tm_mon > 1 && is_leap(tm->tm_year));
case 'L':
return rule->day;
default:
yday = first_day[rule->month];
leap = is_leap(tm->tm_year);
if (rule->month > 1 && leap) yday++;
mday = (rule->day - (yday + tm->tm_wday - tm->tm_yday) + 1400)%7 + 7*rule->week;
if (mday >= days_in_month[rule->month] + (leap && rule->month == 1))
mday -= 7;
return mday + yday;
}
}
struct tm *__dst_adjust(struct tm *tm)
{
time_t t;
int start, end, secs;
int after_start, before_end;
if (tm->tm_isdst >= 0) return tm;
if (!__daylight) {
tm->tm_isdst = 0;
return tm;
}
secs = tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec;
start = cutoff_yday(tm, &__dst_start);
end = cutoff_yday(tm, &__dst_end);
after_start = (tm->tm_yday > start || (tm->tm_yday == start && secs >= __dst_start.time));
before_end = (tm->tm_yday < end || (tm->tm_yday == end && secs < __dst_end.time));
if ((after_start && before_end) || ((end < start) && (after_start || before_end))) {
tm->tm_sec -= __dst_offset;
tm->tm_isdst = 1;
t = __tm_to_time(tm);
return __time_to_tm(t, tm);
} else tm->tm_isdst = 0;
return tm;
}