[PATCH] Simplify TSD without TLS

Mike Hommey mh+jemalloc at glandium.org
Wed Apr 18 09:25:29 PDT 2012

On Tue, Apr 17, 2012 at 12:07:15PM +0200, Mike Hommey wrote:
> On Tue, Apr 17, 2012 at 12:01:04PM +0200, Mike Hommey wrote:
> > From: Mike Hommey <mh at glandium.org>
> > 
> > ---
> >  include/jemalloc/internal/tsd.h |   85 +++++++++------------------------------
> >  1 file changed, 20 insertions(+), 65 deletions(-)
> Just a few notes: this implementation is 95% the same as the one I have
> for mingw. I'm even tempted to make them actually share the code,
> because it would make changes/fixes to one be used in the other.
> It doesn't use a static variable when malloc_tsd_malloc
> fails, but unconditionally aborts instead. Using a static variable is
> due to fail with multithreading anyways. The 'initialized' variable is
> not necessary either, as pthread_key's cleanup function won't be called
> unless a non-NULL value has been set with pthread_setspecific.

With a more complete tsd testcase than the one I sent yesterday, I can
see how the initialized variable is necessary. However, there's a bug,
because it's not set to false when creating the wrapper, and this makes
the cleanup function called even when tsd_set was never called.

There's also a corner case, with *something_tsd_get() = value. It
doesn't trigger a cleanup at thread shutdown, and I'm not sure if that's
a desired effect. Arguably, maybe this should be forbidden. Adding
const to tsd_get() ensures that, but as a matter of fact,
thread_allocated tsd does this kind of assignment. But it also doesn't
have a cleanup function, so it's not really a problem. We may want to
avoid such things from happening in the future, though.

I'm going to attach my patch queue modifying different TSD
implementation details, and for good measure (and because the ordering
of my last patch was not obvious), I'm also going to push the whole
queue again, with proper ordering.

Please note that patch 6/11 (Add a test for the TSD system) does *not*
build without --enable-debug because it's such impossible build
independent parts of jemalloc with optimized builds. You may however
want to run the test on the freebsd implementation of the TSD system,
because that's the only one I couldn't test. With patches 8 and 9, the
--disable-tls implementation passes the test.

This patch queue closes on Mingw support. Next will be MSVC.


More information about the jemalloc-discuss mailing list