Index: src/mesa/x86/glapi_x86.S =================================================================== RCS file: /cvs/mesa/Mesa/src/mesa/x86/glapi_x86.S,v retrieving revision 1.50 diff -u -d -r1.50 glapi_x86.S --- src/mesa/x86/glapi_x86.S 8 Feb 2005 03:44:24 -0000 1.50 +++ src/mesa/x86/glapi_x86.S 6 Apr 2005 22:05:27 -0000 @@ -64,7 +64,17 @@ # define THREADS #endif -#if defined(PTHREADS) +#ifdef GLX_USE_TLS + +# define GL_STUB(fn,off,fn_alt) \ +ALIGNTEXT16; \ +GLOBL_FN(GL_PREFIX(fn, fn_alt)); \ +GL_PREFIX(fn, fn_alt): \ + CALL(get_dispatch) ; \ + NOP ; \ + JMP(GL_OFFSET(off)) + +#elif defined(PTHREADS) # define GL_STUB(fn,off,fn_alt) \ ALIGNTEXT16; \ GLOBL_FN(GL_PREFIX(fn, fn_alt)); \ @@ -106,7 +116,14 @@ SEG_TEXT -#ifdef PTHREADS +#ifdef GLX_USE_TLS + +ALIGNTEXT16 +GLNAME(get_dispatch): + movl %gs:_glapi_tls_Dispatch@NTPOFF, %eax + ret + +#elif defined(PTHREADS) EXTERN GLNAME(_glapi_Dispatch) EXTERN GLNAME(_gl_DispatchTSD) EXTERN GLNAME(pthread_getspecific) @@ -1120,4 +1137,17 @@ GL_STUB_ALIAS(PointParameterfSGIS, _gloffset_PointParameterfEXT, PointParameterfSGIS@8, PointParameterfEXT, PointParameterfEXT@8) GL_STUB_ALIAS(PointParameterfvSGIS, _gloffset_PointParameterfvEXT, PointParameterfvSGIS@8, PointParameterfvEXT, PointParameterfvEXT@8) +#if defined(GLX_USE_TLS) && defined(__linux__) + .section ".note.ABI-tag", "a" + .p2align 2 + .long 1f - 0f /* name length */ + .long 3f - 2f /* data length */ + .long 1 /* note length */ +0: .asciz "GNU" /* vendor name */ +1: .p2align 2 +2: .long 0 /* note data: the ABI tag */ + .long 2,4,20 /* Minimum kernel version w/TLS */ +3: .p2align 2 /* pad out section */ +#endif /* GLX_USE_TLS */ + #endif /* __WIN32__ */ Index: configs/linux-dri =================================================================== RCS file: /cvs/mesa/Mesa/configs/linux-dri,v retrieving revision 1.21 diff -u -d -r1.21 linux-dri --- configs/linux-dri 6 Apr 2005 21:34:34 -0000 1.21 +++ configs/linux-dri 6 Apr 2005 22:05:27 -0000 @@ -20,6 +20,7 @@ DEFINES = -D_POSIX_SOURCE -D_POSIX_C_SOURCE=199309L -D_SVID_SOURCE \ -D_BSD_SOURCE -D_GNU_SOURCE \ + -DGLX_USE_TLS \ -DDRI_NEW_INTERFACE_ONLY -DPTHREADS -DUSE_EXTERNAL_DXTN_LIB=1 X11_INCLUDES = -I/usr/X11R6/include -I/usr/X11R6/include/X11/extensions Index: src/glx/x11/dispatch.c =================================================================== RCS file: /cvs/mesa/Mesa/src/glx/x11/dispatch.c,v retrieving revision 1.2 diff -u -d -r1.2 dispatch.c --- src/glx/x11/dispatch.c 15 Dec 2004 17:18:06 -0000 1.2 +++ src/glx/x11/dispatch.c 6 Apr 2005 22:05:27 -0000 @@ -57,16 +57,12 @@ #define NAME(func) gl##func #define DISPATCH(func, args, msg) \ - const struct _glapi_table *dispatch; \ - dispatch = _glapi_Dispatch ? _glapi_Dispatch : _glapi_get_dispatch();\ - (dispatch->func) args + GL_CALL(func) args #define RETURN_DISPATCH(func, args, msg) \ - const struct _glapi_table *dispatch; \ - dispatch = _glapi_Dispatch ? _glapi_Dispatch : _glapi_get_dispatch();\ - return (dispatch->func) args - + return GL_CALL(func) args +#include "glthread.h" #include "glapitemp.h" #endif /* USE_X86_ASM */ Index: src/glx/x11/dri_glx.c =================================================================== RCS file: /cvs/mesa/Mesa/src/glx/x11/dri_glx.c,v retrieving revision 1.2 diff -u -d -r1.2 dri_glx.c --- src/glx/x11/dri_glx.c 1 Dec 2004 09:41:14 -0000 1.2 +++ src/glx/x11/dri_glx.c 6 Apr 2005 22:05:27 -0000 @@ -222,6 +222,15 @@ void *handle = NULL; + /* If TLS support is enabled, try to open the TLS version of the driver + * binary first. If that fails, try the non-TLS version. + */ +#ifdef GLX_USE_TLS + snprintf(realDriverName, 200, "%s/tls/%s_dri.so", libDir, driverName); + InfoMessageF("OpenDriver: trying %s\n", realDriverName); + handle = dlopen(realDriverName, RTLD_NOW | RTLD_GLOBAL); +#endif + if ( handle == NULL ) { snprintf(realDriverName, 200, "%s/%s_dri.so", libDir, driverName); InfoMessageF("OpenDriver: trying %s\n", realDriverName); Index: src/mesa/glapi/gl_x86_asm.py =================================================================== RCS file: /cvs/mesa/Mesa/src/mesa/glapi/gl_x86_asm.py,v retrieving revision 1.9 diff -u -d -r1.9 gl_x86_asm.py --- src/mesa/glapi/gl_x86_asm.py 7 Jan 2005 03:41:48 -0000 1.9 +++ src/mesa/glapi/gl_x86_asm.py 6 Apr 2005 22:05:28 -0000 @@ -94,7 +94,17 @@ print '# define THREADS' print '#endif' print '' - print '#if defined(PTHREADS)' + print '#ifdef GLX_USE_TLS' + print '' + print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\' + print 'ALIGNTEXT16;\t\t\t\t\t\t\\' + print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\' + print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\' + print '\tCALL(get_dispatch) ;\t\t\t\t\\' + print '\tNOP ;\t\t\t\t\t\t\\' + print '\tJMP(GL_OFFSET(off))' + print '' + print '#elif defined(PTHREADS)' print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\' print 'ALIGNTEXT16;\t\t\t\t\t\t\\' print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\' @@ -136,7 +146,14 @@ print '' print 'SEG_TEXT' print '' - print '#ifdef PTHREADS' + print '#ifdef GLX_USE_TLS' + print '' + print 'ALIGNTEXT16' + print 'GLNAME(get_dispatch):' + print '\tmovl\t%gs:_glapi_tls_Dispatch@NTPOFF, %eax' + print '\tret' + print '' + print '#elif defined(PTHREADS)' print 'EXTERN GLNAME(_glapi_Dispatch)' print 'EXTERN GLNAME(_gl_DispatchTSD)' print 'EXTERN GLNAME(pthread_getspecific)' @@ -159,6 +176,19 @@ def printRealFooter(self): print '' + print '#if defined(GLX_USE_TLS) && defined(__linux__)' + print ' .section ".note.ABI-tag", "a"' + print ' .p2align 2' + print ' .long 1f - 0f /* name length */' + print ' .long 3f - 2f /* data length */' + print ' .long 1 /* note length */' + print '0: .asciz "GNU" /* vendor name */' + print '1: .p2align 2' + print '2: .long 0 /* note data: the ABI tag */' + print ' .long 2,4,20 /* Minimum kernel version w/TLS */' + print '3: .p2align 2 /* pad out section */' + print '#endif /* GLX_USE_TLS */' + print '' print '#endif /* __WIN32__ */' return Index: src/mesa/glapi/glapi.c =================================================================== RCS file: /cvs/mesa/Mesa/src/mesa/glapi/glapi.c,v retrieving revision 1.88 diff -u -d -r1.88 glapi.c --- src/mesa/glapi/glapi.c 15 Dec 2004 23:14:29 -0000 1.88 +++ src/mesa/glapi/glapi.c 6 Apr 2005 22:05:28 -0000 @@ -133,6 +133,28 @@ #if defined(THREADS) +#if defined(GLX_USE_TLS) + +__thread struct _glapi_table * _glapi_tls_Dispatch + __attribute__((tls_model("initial-exec"))) + = (struct _glapi_table *) __glapi_noop_table; + +static __thread struct _glapi_table * _glapi_tls_RealDispatch + __attribute__((tls_model("initial-exec"))) + = (struct _glapi_table *) __glapi_noop_table; + +__thread void * _glapi_tls_Context + __attribute__((tls_model("initial-exec"))); + +/** + * Legacy per-thread dispatch pointer. This is only needed to support + * non-TLS DRI drivers. + */ + +_glthread_TSD _gl_DispatchTSD; + +#else + /** * \name Multi-threaded control support variables * @@ -166,6 +188,7 @@ static _glthread_TSD ContextTSD; /**< Per-thread context pointer */ /*@}*/ +#endif /* defined(GLX_USE_TLS) */ #define DISPATCH_TABLE_NAME __glapi_threadsafe_table #define UNUSED_TABLE_NAME __unused_threadsafe_functions @@ -184,6 +207,29 @@ /***** END THREAD-SAFE DISPATCH *****/ +#if defined(GLX_USE_TLS) + +/** + * \name Old dispatch pointers + * + * Very old DRI based drivers assume that \c _glapi_Dispatch will never be + * \c NULL. Becuase of that, special "thread-safe" dispatch functions are + * needed here. Slightly more recent drivers detect the multi-threaded case + * by \c _glapi_DispatchTSD being \c NULL. + * + * \deprecated + * + * \warning + * \c _glapi_RealDispatch does not exist in TLS builds. I don't think it was + * ever used outside libGL.so, so this should be safe. + */ +/*@{*/ +PUBLIC const struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table; +PUBLIC const struct _glapi_table *_glapi_DispatchTSD = NULL; +PUBLIC const void *_glapi_Context = NULL; +/*@}*/ + +#else PUBLIC struct _glapi_table *_glapi_Dispatch = (struct _glapi_table *) __glapi_noop_table; #if defined( THREADS ) @@ -191,10 +237,11 @@ #endif PUBLIC struct _glapi_table *_glapi_RealDispatch = (struct _glapi_table *) __glapi_noop_table; - /* Used when thread safety disabled */ PUBLIC void *_glapi_Context = NULL; +#endif /* defined(GLX_USE_TLS) */ + static GLboolean DispatchOverride = GL_FALSE; @@ -224,7 +271,7 @@ PUBLIC void _glapi_check_multithread(void) { -#if defined(THREADS) +#if defined(THREADS) && !defined(GLX_USE_TLS) if (!ThreadSafe) { static unsigned long knownID; static GLboolean firstCall = GL_TRUE; @@ -255,7 +302,9 @@ _glapi_set_context(void *context) { (void) __unused_noop_functions; /* silence a warning */ -#if defined(THREADS) +#if defined(GLX_USE_TLS) + _glapi_tls_Context = context; +#elif defined(THREADS) (void) __unused_threadsafe_functions; /* silence a warning */ _glthread_SetTSD(&ContextTSD, context); _glapi_Context = (ThreadSafe) ? NULL : context; @@ -274,7 +323,9 @@ PUBLIC void * _glapi_get_context(void) { -#if defined(THREADS) +#if defined(GLX_USE_TLS) + return _glapi_tls_Context; +#elif defined(THREADS) if (ThreadSafe) { return _glthread_GetTSD(&ContextTSD); } @@ -304,7 +355,15 @@ } #endif -#if defined(THREADS) +#if defined(GLX_USE_TLS) + if (DispatchOverride) { + _glapi_tls_RealDispatch = dispatch; + } + else { + _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch); + _glapi_tls_Dispatch = dispatch; + } +#elif defined(THREADS) if (DispatchOverride) { _glthread_SetTSD(&RealDispatchTSD, (void *) dispatch); if (ThreadSafe) @@ -342,7 +401,13 @@ PUBLIC struct _glapi_table * _glapi_get_dispatch(void) { -#if defined(THREADS) +#if defined(GLX_USE_TLS) + struct _glapi_table * api = (DispatchOverride) + ? _glapi_tls_RealDispatch : _glapi_tls_Dispatch; + + assert( api != NULL ); + return api; +#elif defined(THREADS) if (ThreadSafe) { if (DispatchOverride) { return (struct _glapi_table *) _glthread_GetTSD(&RealDispatchTSD); @@ -399,7 +464,10 @@ _glapi_set_dispatch(real); -#if defined(THREADS) +#if defined(GLX_USE_TLS) + _glthread_SetTSD(&_gl_DispatchTSD, (void *) override); + _glapi_tls_Dispatch = override; +#elif defined(THREADS) _glthread_SetTSD(&_gl_DispatchTSD, (void *) override); if ( ThreadSafe ) { _glapi_Dispatch = (struct _glapi_table *) __glapi_threadsafe_table; @@ -424,10 +492,14 @@ DispatchOverride = GL_FALSE; _glapi_set_dispatch(real); /* the rest of this isn't needed, just play it safe */ -#if defined(THREADS) +#if defined(GLX_USE_TLS) + _glapi_tls_RealDispatch = NULL; +#else +# if defined(THREADS) _glthread_SetTSD(&RealDispatchTSD, NULL); -#endif +# endif _glapi_RealDispatch = NULL; +#endif } @@ -439,7 +511,9 @@ } else { if (DispatchOverride) { -#if defined(THREADS) +#if defined(GLX_USE_TLS) + return (struct _glapi_table *) _glapi_tls_Dispatch; +#elif defined(THREADS) return (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD); #else return _glapi_Dispatch; @@ -500,7 +574,7 @@ #ifdef USE_X86_ASM extern const GLubyte gl_dispatch_functions_start[]; -# if defined(THREADS) +# if defined(THREADS) && !defined(GLX_USE_TLS) # define X86_DISPATCH_FUNCTION_SIZE 32 # else # define X86_DISPATCH_FUNCTION_SIZE 16 @@ -510,6 +584,10 @@ /** * Return dispatch function address the named static (built-in) function. * Return NULL if function not found. + * + * \todo + * A TLS version of this is needed. The code right now will work on x86 in + * TLS mode, but it will be inefficient. */ static const _glapi_proc get_static_proc_address(const char *funcName) Index: src/mesa/glapi/glapi.h =================================================================== RCS file: /cvs/mesa/Mesa/src/mesa/glapi/glapi.h,v retrieving revision 1.24 diff -u -d -r1.24 glapi.h --- src/mesa/glapi/glapi.h 8 Dec 2004 12:35:55 -0000 1.24 +++ src/mesa/glapi/glapi.h 6 Apr 2005 22:05:28 -0000 @@ -54,10 +54,17 @@ typedef void (*_glapi_proc)(void); /* generic function pointer */ -extern void *_glapi_Context; +#if defined (GLX_USE_TLS) + +const extern void *_glapi_Context; +const extern struct _glapi_table *_glapi_Dispatch; +#else + +extern void *_glapi_Context; extern struct _glapi_table *_glapi_Dispatch; +#endif /* defined (GLX_USE_TLS) */ extern void _glapi_noop_enable_warnings(GLboolean enable); Index: src/mesa/glapi/glthread.h =================================================================== RCS file: /cvs/mesa/Mesa/src/mesa/glapi/glthread.h,v retrieving revision 1.17 diff -u -d -r1.17 glthread.h --- src/mesa/glapi/glthread.h 22 Nov 2004 19:11:01 -0000 1.17 +++ src/mesa/glapi/glthread.h 6 Apr 2005 22:05:30 -0000 @@ -306,7 +306,14 @@ extern void _glthread_SetTSD(_glthread_TSD *, void *); -#ifndef GL_CALL +#if defined(GLX_USE_TLS) + +extern __thread struct _glapi_table * _glapi_tls_Dispatch + __attribute__((tls_model("initial-exec"))); + +# define GL_CALL(name) (*(_glapi_tls_Dispatch-> name)) + +#elif !defined(GL_CALL) # if defined(THREADS) extern struct _glapi_table * _glapi_DispatchTSD; # define GL_CALL(name) \