summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2009-12-21 19:43:23 +0000
committerjhb <jhb@FreeBSD.org>2009-12-21 19:43:23 +0000
commit257e93c9a0eba2f74e73a06f18fea91d461f6e15 (patch)
treee6e72d337600509aba60b26e417e79c3ee48a49f
parentf04a989f2dbe7ffdf8dc178715cc5fe4c658f813 (diff)
downloadFreeBSD-src-257e93c9a0eba2f74e73a06f18fea91d461f6e15.zip
FreeBSD-src-257e93c9a0eba2f74e73a06f18fea91d461f6e15.tar.gz
Use _once() to initialize the pthread key for thread-local storage to hold
the results of localtime() instead of using a pthread mutex directly. MFC after: 1 week
-rw-r--r--lib/libc/stdtime/localtime.c28
1 files changed, 14 insertions, 14 deletions
diff --git a/lib/libc/stdtime/localtime.c b/lib/libc/stdtime/localtime.c
index d391e3e..640078d 100644
--- a/lib/libc/stdtime/localtime.c
+++ b/lib/libc/stdtime/localtime.c
@@ -237,6 +237,9 @@ static char lcl_TZname[TZ_STRLEN_MAX + 1];
static int lcl_is_set;
static pthread_once_t gmt_once = PTHREAD_ONCE_INIT;
static pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
+static pthread_once_t localtime_once = PTHREAD_ONCE_INIT;
+static pthread_key_t localtime_key;
+static int localtime_key_error;
char * tzname[2] = {
wildabbr,
@@ -1406,27 +1409,24 @@ 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) {
- if (localtime_key < 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_mutex_unlock(&localtime_mutex);
+ _once(&localtime_once, localtime_key_init);
+ if (localtime_key_error != 0) {
+ errno = localtime_key_error;
+ return(NULL);
}
p_tm = _pthread_getspecific(localtime_key);
if (p_tm == NULL) {
OpenPOWER on IntegriCloud