TLS for Android (and iOS)

Dan Olson at
Tue Apr 1 07:46:12 PDT 2014

Dan Olson < at> writes:

> "David Nadlinger" <code at> writes:
>> On 31 Mar 2014, at 8:25, Jacob Carlborg wrote:
>>> You'll just need to add a call to druntime in one of the functions
>>> in the dyld TLS code. Have a look at:
>> More specifically, for the DMD TLS emulation implementation, this is
>> done in the initTLSRanges() function, which forwards to
>> getTLSBlock(). IIRC, initTLSRanges() is only called for new
>> threads. For the main thread, the TLS ranges is included in the GC
>> ranges detected in initSections().
>> For LDC on OS X, which makes use of the 10.7+ system-level TLS
>> implementation, the place where this is handled is
>> _d_dyld_getTLSRange
>> uses an undocumented dyld API function (dyld_enumerate_tlv_storage) to
>> get the actual TLS  memory range on the current thread:
>> David
> I had disabled initTLSRanges for iOS since dyld_enumerate_tlv_storage is
> a stub for x86 (see

I meant it is a stub for ARM.

> Now that I have tweaked threadLocalVariables.c,
> dyld_enumerate_tlv_storage should now work on iOS. I will have to
> reenble initTLSRanges and see what happens.

I did reenable and it works.  I can tell because the std.datetime
unittest uses enough memory that it causes a GC.  When I first rebuild
everything with TLS enabled and plugged in support from
threadLocalVariables.c (but without initTLSRanges enabled), the
std.datetime unittest started crashing.  The datetime unittest tests
have a fair number of thread locals.  Then I reenabled David's
initTLSRanges() for iOS, and std.datetime unittest went back to passing.

More information about the digitalmars-d-ldc mailing list