This is an archive of the discontinued LLVM Phabricator instance.

Use pthreads to store current thread id on darwin
ClosedPublic

Authored by fjricci on Feb 15 2017, 9:16 AM.

Details

Summary

__thread is not supported by all darwin versions and architectures,
use pthreads instead to allow for building darwin lsan on iossim.

Diff Detail

Repository
rL LLVM

Event Timeline

fjricci created this revision.Feb 15 2017, 9:16 AM
kubamracek added inline comments.Feb 15 2017, 1:33 PM
lib/lsan/lsan_common_linux.cc
37 ↗(On Diff #88553)

Please use some constant. Move the definition of kInvalidTid if you need to.

lib/lsan/lsan_common_mac.cc
27 ↗(On Diff #88553)

same here

28 ↗(On Diff #88553)

Can we come up with a better name for the struct? Perhaps something containing "thread_local_data"...

35 ↗(On Diff #88553)

We're removing the "allocate" argument. Wasn't that necessary for something? Why did we have it in the first place?

fjricci added inline comments.Feb 16 2017, 11:38 AM
lib/lsan/lsan_common_mac.cc
35 ↗(On Diff #88553)

We had this so that we could return false for DisabledInThisThread without allocating any thread-local data if DisableInThisThread had never been called. It's possibly a small performance optimization, but I don't think it's significant, and now that we're using the pthread key for more data, I think it's now more complex than it's worth.

I believe the idea of doing it that way came about was a remnant from trying to use __thread, which would fail if you accessed the value too early in the initialization process.

fjricci updated this revision to Diff 88783.Feb 16 2017, 2:05 PM

Address comments

kubamracek added inline comments.Feb 16 2017, 3:51 PM
lib/lsan/lsan_common.h
58 ↗(On Diff #88783)

Why does this have to fit into 24 bits?

kubamracek added inline comments.Feb 16 2017, 5:44 PM
lib/lsan/lsan_common.h
58 ↗(On Diff #88783)

lsan_thread.cc already declared kInvalidThread as -1. Can we use this declaration and just move it here?

fjricci updated this revision to Diff 88838.Feb 16 2017, 6:46 PM

Move kInvalidTid from lsan_thread to lsan_common

kubamracek accepted this revision.Feb 16 2017, 7:05 PM

LGTM with a nit.

lib/lsan/lsan_common_mac.cc
25 ↗(On Diff #88838)

Remove "thread_info", we already have "thread_local_data_t" below. (Or change this to a C++-ish declaration, up to you.)

This revision is now accepted and ready to land.Feb 16 2017, 7:05 PM
kubamracek added inline comments.Feb 16 2017, 7:07 PM
lib/lsan/lsan_common_mac.cc
40 ↗(On Diff #88838)

Oh, wait, we need a memset(0) + current_thread_id = kInvalidTid here or something like that.

Still LGTM, assuming the struct is initialized properly after the allocation.

fjricci updated this revision to Diff 88839.Feb 16 2017, 7:20 PM

Initialize thread-local struct

Not sure if this is the best way to do the initialization, in terms of style.
My main concern is with the initialization of the AllocatorCache, I wasn't
sure how to do that outside of the way I did it here.

(The AllocatorCache doesn't show up until the next patch, but might as well account for it now.)

kubamracek added inline comments.Feb 16 2017, 7:23 PM
lib/lsan/lsan_common_mac.cc
42 ↗(On Diff #88839)

Hm, that's weird. Either use placement new, new(ptr) thread_local_data_t(), or just initialize this field-by-field (and remove the default values).

fjricci updated this revision to Diff 88840.Feb 16 2017, 7:31 PM

Initialize thread_local members individual

This revision was automatically updated to reflect the committed changes.