X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fegl%2Fmain%2Feglcurrent.c;h=54fc4f742e507bf5c6285f8591fbfb60e2c35a8c;hb=f2001df508fda599a18b3586d2775e970a3db13a;hp=96152db19fb87ee9e3452659e17db01da921e65e;hpb=cd63e35603568c168e8aebbc47ca6ea308f2ccfa;p=mesa.git diff --git a/src/egl/main/eglcurrent.c b/src/egl/main/eglcurrent.c index 96152db19fb..54fc4f742e5 100644 --- a/src/egl/main/eglcurrent.c +++ b/src/egl/main/eglcurrent.c @@ -1,63 +1,142 @@ +/************************************************************************** + * + * Copyright 2009-2010 Chia-I Wu + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + + #include #include -#include "eglcurrent.h" -#include "eglcontext.h" #include "egllog.h" +#include "eglmutex.h" +#include "eglcurrent.h" +#include "eglglobals.h" + +/* This should be kept in sync with _eglInitThreadInfo() */ +#define _EGL_THREAD_INFO_INITIALIZER \ + { EGL_SUCCESS, { NULL }, 0 } /* a fallback thread info to guarantee that every thread always has one */ -static _EGLThreadInfo dummy_thread; +static _EGLThreadInfo dummy_thread = _EGL_THREAD_INFO_INITIALIZER; +#if PTHREADS +#include + +static _EGL_DECLARE_MUTEX(_egl_TSDMutex); +static EGLBoolean _egl_TSDInitialized; +static pthread_key_t _egl_TSD; +static void (*_egl_FreeTSD)(_EGLThreadInfo *); + #ifdef GLX_USE_TLS -static __thread const _EGLThreadInfo *_egl_TSD; +static __thread const _EGLThreadInfo *_egl_TLS __attribute__ ((tls_model("initial-exec"))); +#endif -static INLINE EGLBoolean _eglInitTSD(void) { return EGL_TRUE; } -static INLINE void _eglFiniTSD(void) { } -static INLINE void _eglSetTSD(const _EGLThreadInfo *t) { _egl_TSD = t; } - -static INLINE _EGLThreadInfo *_eglGetTSD(void) +static INLINE void _eglSetTSD(const _EGLThreadInfo *t) { - return (_EGLThreadInfo *) _egl_TSD; + pthread_setspecific(_egl_TSD, (const void *) t); +#ifdef GLX_USE_TLS + _egl_TLS = t; +#endif } -#elif PTHREADS -#include - -static pthread_key_t _egl_TSD; - -static INLINE EGLBoolean _eglInitTSD(void) +static INLINE _EGLThreadInfo *_eglGetTSD(void) { - return (pthread_key_create(&_egl_TSD, NULL) == 0); +#ifdef GLX_USE_TLS + return (_EGLThreadInfo *) _egl_TLS; +#else + return (_EGLThreadInfo *) pthread_getspecific(_egl_TSD); +#endif } static INLINE void _eglFiniTSD(void) { - pthread_key_delete(_egl_TSD); + _eglLockMutex(&_egl_TSDMutex); + if (_egl_TSDInitialized) { + _EGLThreadInfo *t = _eglGetTSD(); + + _egl_TSDInitialized = EGL_FALSE; + if (t && _egl_FreeTSD) + _egl_FreeTSD((void *) t); + pthread_key_delete(_egl_TSD); + } + _eglUnlockMutex(&_egl_TSDMutex); } -static INLINE void _eglSetTSD(const _EGLThreadInfo *t) +static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *)) { - pthread_setspecific(_egl_TSD, (const void *) t); -} + if (!_egl_TSDInitialized) { + _eglLockMutex(&_egl_TSDMutex); + + /* check again after acquiring lock */ + if (!_egl_TSDInitialized) { + if (pthread_key_create(&_egl_TSD, (void (*)(void *)) dtor) != 0) { + _eglUnlockMutex(&_egl_TSDMutex); + return EGL_FALSE; + } + _egl_FreeTSD = dtor; + _eglAddAtExitCall(_eglFiniTSD); + _egl_TSDInitialized = EGL_TRUE; + } -static INLINE _EGLThreadInfo *_eglGetTSD(void) -{ - return (_EGLThreadInfo *) pthread_getspecific(_egl_TSD); + _eglUnlockMutex(&_egl_TSDMutex); + } + + return EGL_TRUE; } #else /* PTHREADS */ static const _EGLThreadInfo *_egl_TSD; +static void (*_egl_FreeTSD)(_EGLThreadInfo *); -static INLINE EGLBoolean _eglInitTSD(void) { return EGL_TRUE; } -static INLINE void _eglFiniTSD(void) { } -static INLINE void _eglSetTSD(const _EGLThreadInfo *t) { _egl_TSD = t; } +static INLINE void _eglSetTSD(const _EGLThreadInfo *t) +{ + _egl_TSD = t; +} static INLINE _EGLThreadInfo *_eglGetTSD(void) { return (_EGLThreadInfo *) _egl_TSD; } + +static INLINE void _eglFiniTSD(void) +{ + if (_egl_FreeTSD && _egl_TSD) + _egl_FreeTSD((_EGLThreadInfo *) _egl_TSD); +} + +static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *)) +{ + if (!_egl_FreeTSD && dtor) { + _egl_FreeTSD = dtor; + _eglAddAtExitCall(_eglFiniTSD); + } + return EGL_TRUE; +} + #endif /* !PTHREADS */ @@ -98,24 +177,17 @@ _eglDestroyThreadInfo(_EGLThreadInfo *t) /** - * Initialize "current thread" management. + * Make sure TSD is initialized and return current value. */ -EGLBoolean -_eglInitCurrent(void) +static INLINE _EGLThreadInfo * +_eglCheckedGetTSD(void) { - _eglInitThreadInfo(&dummy_thread); - return _eglInitTSD(); -} - + if (_eglInitTSD(&_eglDestroyThreadInfo) != EGL_TRUE) { + _eglLog(_EGL_FATAL, "failed to initialize \"current\" system"); + return NULL; + } -/** - * Finish "current thread" management. - */ -void -_eglFiniCurrent(void) -{ - /* TODO trace and release all threads... */ - _eglFiniTSD(); + return _eglGetTSD(); } @@ -129,7 +201,7 @@ _eglFiniCurrent(void) _EGLThreadInfo * _eglGetCurrentThread(void) { - _EGLThreadInfo *t = _eglGetTSD(); + _EGLThreadInfo *t = _eglCheckedGetTSD(); if (!t) { t = _eglCreateThreadInfo(); _eglSetTSD(t); @@ -145,7 +217,7 @@ _eglGetCurrentThread(void) void _eglDestroyCurrentThread(void) { - _EGLThreadInfo *t = _eglGetTSD(); + _EGLThreadInfo *t = _eglCheckedGetTSD(); if (t) { _eglDestroyThreadInfo(t); _eglSetTSD(NULL); @@ -162,73 +234,48 @@ _eglDestroyCurrentThread(void) EGLBoolean _eglIsCurrentThreadDummy(void) { - _EGLThreadInfo *t = _eglGetTSD(); + _EGLThreadInfo *t = _eglCheckedGetTSD(); return (!t || t == &dummy_thread); } /** - * Return the currently bound context, or NULL. - */ -_EGLContext * -_eglGetCurrentContext(void) -{ - _EGLThreadInfo *t = _eglGetCurrentThread(); - return t->CurrentContexts[t->CurrentAPIIndex]; -} - - -/** - * Return the display of the currently bound context, or NULL. + * Return the currently bound context of the given API, or NULL. */ -_EGLDisplay * -_eglGetCurrentDisplay(void) +PUBLIC _EGLContext * +_eglGetAPIContext(EGLenum api) { _EGLThreadInfo *t = _eglGetCurrentThread(); - _EGLContext *ctx = t->CurrentContexts[t->CurrentAPIIndex]; - if (ctx) - return ctx->Display; - else - return NULL; + return t->CurrentContexts[_eglConvertApiToIndex(api)]; } /** - * Return the read or write surface of the currently bound context, or NULL. + * Return the currently bound context of the current API, or NULL. */ -_EGLSurface * -_eglGetCurrentSurface(EGLint readdraw) +_EGLContext * +_eglGetCurrentContext(void) { _EGLThreadInfo *t = _eglGetCurrentThread(); - _EGLContext *ctx = t->CurrentContexts[t->CurrentAPIIndex]; - if (ctx) { - switch (readdraw) { - case EGL_DRAW: - return ctx->DrawSurface; - case EGL_READ: - return ctx->ReadSurface; - default: - return NULL; - } - } - return NULL; + return t->CurrentContexts[t->CurrentAPIIndex]; } /** - * Record EGL error code. + * Record EGL error code and return EGL_FALSE. */ EGLBoolean _eglError(EGLint errCode, const char *msg) { _EGLThreadInfo *t = _eglGetCurrentThread(); - const char *s; if (t == &dummy_thread) return EGL_FALSE; - if (t->LastError == EGL_SUCCESS) { - t->LastError = errCode; + t->LastError = errCode; + + if (errCode != EGL_SUCCESS) { + const char *s; switch (errCode) { case EGL_BAD_ACCESS: @@ -267,14 +314,19 @@ _eglError(EGLint errCode, const char *msg) case EGL_BAD_SURFACE: s = "EGL_BAD_SURFACE"; break; + case EGL_NOT_INITIALIZED: + s = "EGL_NOT_INITIALIZED"; + break; +#ifdef EGL_MESA_screen_surface case EGL_BAD_SCREEN_MESA: s = "EGL_BAD_SCREEN_MESA"; break; case EGL_BAD_MODE_MESA: s = "EGL_BAD_MODE_MESA"; break; +#endif default: - s = "other"; + s = "other EGL error"; } _eglLog(_EGL_DEBUG, "EGL user error 0x%x (%s) in %s\n", errCode, s, msg); }