[cxx-abi-dev] thread_local CONstructors

Jakub Jelinek jakub at redhat.com
Thu Sep 27 13:22:50 UTC 2012


On Thu, Sep 27, 2012 at 08:54:04AM -0400, Jason Merrill wrote:
> On 09/26/2012 05:03 PM, Jason Merrill wrote:
> >On 09/26/2012 10:59 AM, Richard Henderson wrote:
> >>Which means that one call to __tls_get_addr can be shared for the
> >>lookup of I and I_DONE.
> >
> >I suppose tweaking the wrapper to
> >
> >extern int& i_init() __attribute__ ((weak));
> >inline int& i_wrapper()
> >{
> >   if (i_init)
> >     return i_init();
> >   else
> >     return i;
> >}
> >
> >would avoid looking up the TLS address on both sides of the call to i_init.
> 
> On further consideration, I guess this wouldn't really be a win; it
> would prevent making i_init an alias to the whole-TU init function,
> and then you'd need to look up i in both i_init and the TU init fn.

BTW, there is another problem with the initialization of whole TU TLS.
If some of the TLS vars are exported from a shared library, they might be
overriden by some other definition in another shared library.  At that point
we could initialize one TLS var twice.  Or is that an ODR violation we just
don't care about?
a.h:
struct S { S (); ~S (); int s; };
extern thread_local S s1, s2, s3;
liba.C:
#include "a.h"
thread_local S s1, s2;
libb.C:
#include "a.h"
thread_local S s2, s3;
main.C:
#include "a.h"
int
main ()
{
  s1.s++;
  s2.s++;
  s3.s++;
}
g++ -shared -fpic -o liba.so liba.C
g++ -shared -fpic -o libb.so libb.C
g++ -o main main.C ./liba.so ./libb.so
s2 symbol will resolve to liba.so's copy, not libb.so's copy...

	Jakub


More information about the cxx-abi-dev mailing list