dsimcha wrote:
Thanks, though I'm way ahead of you in that I already did this.  Works great,
except it's a little bit slow.

TLS is always going to be slow. Beating the old drum about how freakin' useful a tool obj2asm is and why doesn't anyone use it, here's what it looks like:

--------------------

__thread int foo;
void main()
{
    foo = 3;
}

---------------------
__Dmain comdat
        assume  CS:__Dmain
                mov     EAX,__tls_index
                mov     ECX,FS:__tls_array
                mov     EDX,[EAX*4][ECX]
                mov     dword ptr _D5test63fooi[EDX],3
                xor     EAX,EAX
                ret
__Dmain ends
---------------------

So you see, it takes 4 instructions to reference a TLS global vs 1 instruction for regular static data. The lesson is to minimize directly referencing such globals. Instead, take a pointer to them, or cache the value into a local.

As for whether __thread is completely implemented, yes it is completely implemented in the compiler. I obviously forgot about the gc, though, and I'm glad you found the problem so I can fix it. In the meantime, you can call the gc directly to register your __thread variable as a 'root', then the gc will recognize it properly.

If you want to read about how TLS works under Windows, see:

http://www.nynaeve.net/?p=180

It works an equivalent, but completely differently under the hood, way in Linux:

http://people.redhat.com/drepper/tls.pdf

Reply via email to