From cb6207858a9fcc2feaee22e626912fba281ac969 Mon Sep 17 00:00:00 2001 From: raf Date: Tue, 20 Mar 2007 17:29:57 -0700 Subject: PSARC 2007/129 thr_keycreate_once 6513516 double checked locking code needs a memory barrier --- usr/src/lib/libsocket/inet/ether_addr.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'usr/src/lib/libsocket') diff --git a/usr/src/lib/libsocket/inet/ether_addr.c b/usr/src/lib/libsocket/inet/ether_addr.c index 44f8979691..12c7a06f70 100644 --- a/usr/src/lib/libsocket/inet/ether_addr.c +++ b/usr/src/lib/libsocket/inet/ether_addr.c @@ -20,7 +20,7 @@ */ /* - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -240,27 +241,16 @@ typedef struct { static eabuf_t * ea_buf(void) { - static thread_key_t key; - static int key_once = 0; - static mutex_t tsd_lock = DEFAULTMUTEX; + static thread_key_t key = THR_ONCE_KEY; static eabuf_t ea_main; - eabuf_t *eabuf = NULL; + eabuf_t *eabuf; if (thr_main()) return (&ea_main); - if (key_once == 0) { - (void) mutex_lock(&tsd_lock); - if (key_once == 0) { - if (thr_keycreate(&key, free) != 0) { - (void) mutex_unlock(&tsd_lock); - return (NULL); - } - key_once = 1; - } - (void) mutex_unlock(&tsd_lock); - } - (void) thr_getspecific(key, (void **)&eabuf); + if (thr_keycreate_once(&key, free) != 0) + return (NULL); + eabuf = pthread_getspecific(key); if (eabuf == NULL) { eabuf = malloc(sizeof (eabuf_t)); (void) thr_setspecific(key, eabuf); -- cgit v1.2.3