NVIDIA and TLS
Gareth Hughes
gareth at nvidia.com
Mon Jun 16 19:23:52 PDT 2003
On Mon, 16 Jun 2003 Alexander Kabaev wrote:
>
> You do not understand. Whether or not it is better only time will tell.
> ELF TLS standard is defined in a way, that only a _specific_ threads
> implementation can reap full benefits, most other are penalized by the
> segment access reloads on every thread context switch.
>
> [snip]
>
> So the choice is either to pay penalty implementing TLS through callable
> entry, or by making context switches more expensive than they have to
> be.
>
> I am obviously not thrilled with the standard, but I guess I'll have
> live with it since I have nothing better to offer on my own anyway.
To save you the trouble, I'll copy the relevant information out
of Drepper's document (sections 3.4.2, 4.1.2, 4.2.2).
C code:
extern __thread int x;
&x;
Assembly code, General Dynamic access model:
leal x at tlsgd(%ebx),%eax
call __tls_get_addr at plt
C code:
static __thread int x1;
static __thread int x2;
&x1;
&x2;
Assembly code, Local Dynamic access model:
leal x1 at tlsldm(%ebx),%eax
call __tls_get_addr at plt
...
leal x1 at dtpoff(%eax),%edx
...
leal x2 at dtpoff(%eax),%edx
I don't see any segment register use there, do you? That's the
implementation of the dynamic TLS access models on x86
(specifically, the GNU variants, not the Sun variants). You
implement __tls_get_addr() however you need to. Only the static
access models require the use of %gs. If you don't want to use
%gs, then go right ahead and only implement the dynamic access
models. __thread variable access will be slower on FreeBSD in
some cases (those that could potentially use the static access
models), but you're free to implement whatever kind of thread
library you want, including one that does not touch nor use the
%gs segment register.
--
Gareth Hughes (gareth at nvidia.com)
OpenGL Developer, NVIDIA Corporation
More information about the freebsd-threads
mailing list