TLS for Android

Joakim joakim at airpost.net
Fri Mar 7 16:55:04 PST 2014


So I've been looking into implementing TLS for Android/x86, 
rummaging through old TLS git commits for dmd and ldc to see what 
to do.  It appears that Walter implemented TLS on OS X more than 
four years ago by packing thread-local variables into special 
segments and then unpacking them in druntime, which uses 
pthread_(get|set)specific on OS X nowadays:

http://www.drdobbs.com/architecture-and-design/implementing-thread-local-storage-on-os/228701185
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_osx.d#L106

Since Android also provides these pthread functions for TLS, 
seems like a similar approach is called for.

I notice that ldc never used this approach, depending on llvm's 
built-in TLS support instead:

https://github.com/ldc-developers/ldc/commit/4d7a6eda234bc8d12703cc577c09c2ca50ac6bda#diff-19

It seems that this also meant that TLS wasn't garbage-collected 
on OSX, until David added it a little more than a year ago:

https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/osx_tls.c

I can copy what dmd is doing on OS X Mach-O with ELF, but it's 
not going to be easily transferable to ldc, which will be 
necessary for Android/ARM.

Do you have any advice on how to pull this off with ldc?  Should 
I be going the dmd route and packing the TLS myself?  Does llvm 
provide good support for this?

Or is there some other llvm TLS shortcut I can use?  I tried to 
see if llvm just has some thread-local implementation that 
automatically uses pthread_setspecific, but didn't find anything.


More information about the digitalmars-d-ldc mailing list