NVIDIA and TLS

Gareth Hughes gareth at nvidia.com
Mon Jun 16 15:09:02 PDT 2003


On Mon, 16 Jun 2003, Julian Elischer wrote:
> 
> Unfortunatly you are making an assumption about the threads that you 
> are only able to make in Linux "by chance" as they did something else
> for their TLS. I gather that you don't keep a local variable
> pointed to your local drawing context, but just use the 'global'
> style of programming but using %gs as a 'roll-your-own' version of 
> 'local context'.  Can you just grab %gs on Microsoft? I thought they
> were going to be using it for their TLS too.

There is space reserved for OpenGL in the Windows thread environment block,
so you can access your current context pointer at a fixed offset from %fs.
I don't understand your point about local variables -- fundamentally, OpenGL
has a notion of the current context.  All rendering that occurs in a given
thread is based on the context that is currently bound to that thread.  Each
thread can have its own context, although no context can be bound to more
than one thread at a time.  Thus, you have two options:

1) Store your pointers in global variables, and be thread-unsafe.
2) Store your pointers in some thread-safe manner, i.e., thread-local
storage.

If you need us to explain why this is so in greater detail, please just say
the word.

> 
> BTW
> Have you looked at the speed of that? At one time it was a lot slower
> to dereference things through %gs than it was to simply have a normal
> register allocated to the task (e.g. %esi) as would be done normally.
> I don't know if that is true with any modern machines though.

Sure, you may (and I stress may) pay a few cycles of overhead for a segment
prefix, but it is nothing compared to a function call.

> HOWEVER..
> as far as we know we still have %fs unused....
> Maybe we could just switch registers :-)
> (that too is of course non portable too but.....)

Wine uses %fs.

> BTW what do you do for ia64/alpha/amd64 archtectures?

All these platforms now support __thread variables and ELF TLS on Linux.

-- 
Gareth Hughes (gareth at nvidia.com)
OpenGL Developer, NVIDIA Corporation


More information about the freebsd-threads mailing list