TLS for Android (and iOS)

Dan Olson at
Mon Mar 31 08:23:03 PDT 2014

Jacob Carlborg <doob at> writes:

> Regardless of the license, you can still give a try to see if the
> technical parts work.

I have tried and success.

I added threadLocalHelpers.s and threadLocalVariables.c, modified to
enable for arm, then had to sprinkle in some missing types from
dl_priv.h.  Then put in a call to tlv_initializer().

The proof is that thread locals get proper initial values when accessed
through tlv_get_addr().  For example, a thread local double is being
initialized to nan.

Still having LLVM emit a __tls_get_addr, so to try this out, I changed
my test __tls_get_addr() implementation to forward to tlv_get_addr() in

    extern (C)
    void* __tls_get_addr(TLVDescriptor* tlvd)
        __gshared static ubyte data[512];

        printf("__tls_get_addr %p \n", tlvd);
        printf("thunk %p, key %u, offset %u\n",
               tlvd.thunk, tlvd.key, tlvd.offset);

        // tlv_initializer() will change thunk to tlv_get_addr
        if (tlvd.thunk is &tlv_get_addr) {
            puts("calling real tlv_get_addr instead");
            return tlv_get_addr(tlvd);

        // tlv not initialized yet, return my fake thread local data.
        return data.tlvd + tlvd.offset;

More information about the digitalmars-d-ldc mailing list