[PATCH] Simplify TSD without TLS

Mike Hommey mh+jemalloc at glandium.org
Tue Apr 17 03:01:04 PDT 2012


From: Mike Hommey <mh at glandium.org>

---
 include/jemalloc/internal/tsd.h |   85 +++++++++------------------------------
 1 file changed, 20 insertions(+), 65 deletions(-)

diff --git a/include/jemalloc/internal/tsd.h b/include/jemalloc/internal/tsd.h
index 35ae5e3..db85ebb 100644
--- a/include/jemalloc/internal/tsd.h
+++ b/include/jemalloc/internal/tsd.h
@@ -190,36 +190,15 @@ a_name##_tsd_set(a_type *val)						\
 #else
 #define	malloc_tsd_funcs(a_attr, a_name, a_type, a_initializer,		\
     a_cleanup)								\
-/* Data structure. */							\
-typedef struct {							\
-	bool	isstatic;						\
-	bool	initialized;						\
-	a_type	val;							\
-} a_name##_tsd_wrapper_t;						\
 /* Initialization/cleanup. */						\
 a_attr void								\
 a_name##_tsd_cleanup_wrapper(void *arg)					\
 {									\
-	a_name##_tsd_wrapper_t *wrapper = (a_name##_tsd_wrapper_t *)arg;\
+	a_type *value = (a_type *)arg;					\
 									\
-	if (a_cleanup != malloc_tsd_no_cleanup &&			\
-	    wrapper->initialized) {					\
-		wrapper->initialized = false;				\
-		a_cleanup(&wrapper->val);				\
-		if (wrapper->initialized) {				\
-			/* Trigger another cleanup round. */		\
-			if (pthread_setspecific(a_name##_tsd,		\
-			    (void *)wrapper)) {				\
-				malloc_write("<jemalloc>: Error"	\
-				    " setting TSD for "#a_name"\n");	\
-				if (opt_abort)				\
-					abort();			\
-			}						\
-			return;						\
-		}							\
-	}								\
-	if (wrapper->isstatic == false)					\
-		malloc_tsd_dalloc(wrapper);				\
+	if (a_cleanup != malloc_tsd_no_cleanup)				\
+		a_cleanup(value);					\
+	malloc_tsd_dalloc(value);					\
 }									\
 a_attr bool								\
 a_name##_tsd_boot(void)							\
@@ -232,58 +211,34 @@ a_name##_tsd_boot(void)							\
 	return (false);							\
 }									\
 /* Get/set. */								\
-a_attr a_name##_tsd_wrapper_t *						\
-a_name##_tsd_get_wrapper(void)						\
+a_attr a_type *								\
+a_name##_tsd_get(void)							\
 {									\
-	a_name##_tsd_wrapper_t *wrapper = (a_name##_tsd_wrapper_t *)	\
-	    pthread_getspecific(a_name##_tsd);				\
+	a_type *value;							\
 									\
-	if (wrapper == NULL) {						\
-		wrapper = (a_name##_tsd_wrapper_t *)			\
-		    malloc_tsd_malloc(sizeof(a_name##_tsd_wrapper_t));	\
-		if (wrapper == NULL) {					\
-			static a_name##_tsd_wrapper_t			\
-			    a_name##_tsd_static_data =			\
-			    {true, false, a_initializer};		\
+	assert(a_name##_booted);					\
+	value = (a_type *) pthread_getspecific(a_name##_tsd);		\
+	if (value == NULL) {						\
+		a_type tsd_init_data = a_initializer;			\
+		value = (a_type *) malloc_tsd_malloc(sizeof(a_type));	\
+		if (value == NULL) {					\
 			malloc_write("<jemalloc>: Error allocating"	\
 			    " TSD for "#a_name"\n");			\
-			if (opt_abort)					\
-				abort();				\
-			wrapper = &a_name##_tsd_static_data;		\
-		} else {						\
-			static a_type tsd_static_data = a_initializer;	\
-			wrapper->isstatic = false;			\
-			wrapper->val = tsd_static_data;			\
-		}							\
-		if (pthread_setspecific(a_name##_tsd,			\
-		    (void *)wrapper)) {					\
-			malloc_write("<jemalloc>: Error setting"	\
-			    " TSD for "#a_name"\n");			\
-			if (opt_abort)					\
-				abort();				\
+			abort();					\
 		}							\
+		*value = tsd_init_data;					\
+		pthread_setspecific(a_name##_tsd, (void *)value);	\
 	}								\
-	return (wrapper);						\
-}									\
-a_attr a_type *								\
-a_name##_tsd_get(void)							\
-{									\
-	a_name##_tsd_wrapper_t *wrapper;				\
-									\
-	assert(a_name##_booted);					\
-	wrapper = a_name##_tsd_get_wrapper();				\
-	return (&wrapper->val);						\
+	return (value);							\
 }									\
 a_attr void								\
 a_name##_tsd_set(a_type *val)						\
 {									\
-	a_name##_tsd_wrapper_t *wrapper;				\
+	a_type *value;							\
 									\
 	assert(a_name##_booted);					\
-	wrapper = a_name##_tsd_get_wrapper();				\
-	wrapper->val = *(val);						\
-	if (a_cleanup != malloc_tsd_no_cleanup)				\
-		wrapper->initialized = true;				\
+	value = a_name##_tsd_get();					\
+	*value = *(val);						\
 }
 #endif
 
-- 
1.7.10




More information about the jemalloc-discuss mailing list