Date: Wed, 13 Jan 2010 18:12:37 +0000 (UTC) From: John Baldwin <jhb@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-7@freebsd.org Subject: svn commit: r202202 - stable/7/lib/libc/stdtime Message-ID: <201001131812.o0DICbJi083490@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: jhb Date: Wed Jan 13 18:12:37 2010 New Revision: 202202 URL: http://svn.freebsd.org/changeset/base/202202 Log: MFC 199607, 200797, 201270, 201669: Use pthread_once() to initialize the thread-local storage for localtime() and gmtime() and _once() to initialize gmt state rather than home-rolled versions using pthread mutex locks. Modified: stable/7/lib/libc/stdtime/localtime.c Directory Properties: stable/7/lib/libc/ (props changed) stable/7/lib/libc/stdtime/ (props changed) Modified: stable/7/lib/libc/stdtime/localtime.c ============================================================================== --- stable/7/lib/libc/stdtime/localtime.c Wed Jan 13 18:12:21 2010 (r202201) +++ stable/7/lib/libc/stdtime/localtime.c Wed Jan 13 18:12:37 2010 (r202202) @@ -235,9 +235,14 @@ static struct state gmtmem; static char lcl_TZname[TZ_STRLEN_MAX + 1]; static int lcl_is_set; -static int gmt_is_set; +static pthread_once_t gmt_once = PTHREAD_ONCE_INIT; static pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER; -static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_once_t gmtime_once = PTHREAD_ONCE_INIT; +static pthread_key_t gmtime_key; +static int gmtime_key_error; +static pthread_once_t localtime_once = PTHREAD_ONCE_INIT; +static pthread_key_t localtime_key; +static int localtime_key_error; char * tzname[2] = { wildabbr, @@ -1407,26 +1412,25 @@ struct tm * const tmp; return result; } +static void +localtime_key_init(void) +{ + + localtime_key_error = _pthread_key_create(&localtime_key, free); +} + struct tm * localtime(timep) const time_t * const timep; { - static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER; - static pthread_key_t localtime_key = -1; struct tm *p_tm; - int r; if (__isthreaded != 0) { - _pthread_mutex_lock(&localtime_mutex); - if (localtime_key < 0) { - if ((r = _pthread_key_create(&localtime_key, free)) - != 0) { - _pthread_mutex_unlock(&localtime_mutex); - errno = r; - return(NULL); - } + _pthread_once(&localtime_once, localtime_key_init); + if (localtime_key_error != 0) { + errno = localtime_key_error; + return(NULL); } - _pthread_mutex_unlock(&localtime_mutex); p_tm = _pthread_getspecific(localtime_key); if (p_tm == NULL) { if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) @@ -1462,6 +1466,17 @@ struct tm * tmp; return tmp; } +static void +gmt_init(void) +{ + +#ifdef ALL_STATE + gmtptr = (struct state *) malloc(sizeof *gmtptr); + if (gmtptr != NULL) +#endif /* defined ALL_STATE */ + gmtload(gmtptr); +} + /* ** gmtsub is to gmtime as localsub is to localtime. */ @@ -1474,16 +1489,7 @@ struct tm * const tmp; { register struct tm * result; - _MUTEX_LOCK(&gmt_mutex); - if (!gmt_is_set) { - gmt_is_set = TRUE; -#ifdef ALL_STATE - gmtptr = (struct state *) malloc(sizeof *gmtptr); - if (gmtptr != NULL) -#endif /* defined ALL_STATE */ - gmtload(gmtptr); - } - _MUTEX_UNLOCK(&gmt_mutex); + _once(&gmt_once, gmt_init); result = timesub(timep, offset, gmtptr, tmp); #ifdef TM_ZONE /* @@ -1507,25 +1513,25 @@ struct tm * const tmp; return result; } +static void +gmtime_key_init(void) +{ + + gmtime_key_error = _pthread_key_create(&gmtime_key, free); +} + struct tm * gmtime(timep) const time_t * const timep; { - static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER; - static pthread_key_t gmtime_key = -1; struct tm *p_tm; - int r; if (__isthreaded != 0) { - _pthread_mutex_lock(&gmtime_mutex); - if (gmtime_key < 0) { - if ((r = _pthread_key_create(&gmtime_key, free)) != 0) { - _pthread_mutex_unlock(&gmtime_mutex); - errno = r; - return(NULL); - } + _pthread_once(&gmtime_once, gmtime_key_init); + if (gmtime_key_error != 0) { + errno = gmtime_key_error; + return(NULL); } - _pthread_mutex_unlock(&gmtime_mutex); /* * Changed to follow POSIX.1 threads standard, which * is what BSD currently has.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201001131812.o0DICbJi083490>