NVIDIA and TLS

Daniel Eischen eischen at vigrid.com
Tue Jun 17 09:19:02 PDT 2003


On Mon, 16 Jun 2003, Gareth Hughes wrote:
> Umm, you clearly don't understand what I've been talking about.
> Upon entry into libGL, i.e. when an OpenGL API entrypoint is
> called by the application, things like the current context or
> current dispatch table are fetched from thread-local storage.
> Internally, we pass these pointers around as required.
> 
> So, we might have something like this:
> 
> 	void glBegin(GLenum mode)
> 	{
> 		// Grab the current dispatch pointer from TLS
> 		__GLdispatch *dispatch = GET_CURRENT_DISPATCH();
> 
> 		// Call into the driver backend
> 		dispatch->Begin(mode)
> 	}
> 
> or, in x86 assembly:
> 
> 	glBegin:
> 		mov %gs:__gl_dispatch at ntpoff, %eax
> 		jmp *__begin_offset(%eax)
> 
> (note that Andy mentioned this example in his original email)
> 
> This would jump to a function inside the driver like this:
> 
> 	void __internal_Begin(GLenum mode)
> 	{
> 		__GLcontext *ctx = GET_CURRENT_CONTEXT();
> 
> 		do_something(ctx, ...);
> 		do_something_else(ctx, ...);
> 		// and so on
> 	}

But you have at least 2 levels of "get current thread" context:
one in the openGl entry point (GET_CURRENT_DISPATCH()) and
another in the driver (GET_CURRENT_CONTEXT()).  If you are
going to get some TLS in the OpenGL entry point, then make
it the only TLS thingy you'll ever need.  Hang all your stuff
off the one TLS context and pass it (or parts of it) around
to other consumers.  So the driver gets called as follows:

	void __internal_Begin(GLenum mode, __GLcontext *ctx)
	{
		do_something(ctx, ...);
		do_something_else(ctx, ...);
		// and so on
	}

Now when somebody comes up with thread-safe OpenGL and you
have glBegin_r(GLctx *ctx, GLenum mode), you are golden:

	glBegin_r(GLctx *ctx, GLenum mode)
	{
		__GLdispatch *dispatch = ctx->dispatch;
		__GLcontext *glctx = ctx->glctx;

		dispatch->Begin(glctx, mode);
	}

> Once we're inside the driver, we know what the current context
> or other thread-local variable's value is.  Two critical
> points:
> 
> 1) We have to fetch the value from TLS at least once per entry
>    into the driver.

Pass what you need to the driver.  You needn't have to fetch
any TLS in the driver because the only way to the driver
is from the OpenGL entry point -- unless I'm missing
something.

-- 
Dan Eischen



More information about the freebsd-threads mailing list