1 /**************************************************************************
3 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "eglcurrent.h"
34 #include "eglglobals.h"
37 /* This should be kept in sync with _eglInitThreadInfo() */
38 #define _EGL_THREAD_INFO_INITIALIZER \
39 { EGL_SUCCESS, { NULL }, 0 }
41 /* a fallback thread info to guarantee that every thread always has one */
42 static _EGLThreadInfo dummy_thread = _EGL_THREAD_INFO_INITIALIZER;
48 static _EGL_DECLARE_MUTEX(_egl_TSDMutex);
49 static EGLBoolean _egl_TSDInitialized;
50 static pthread_key_t _egl_TSD;
51 static void (*_egl_FreeTSD)(_EGLThreadInfo *);
54 static __thread const _EGLThreadInfo *_egl_TLS
55 __attribute__ ((tls_model("initial-exec")));
58 static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
60 pthread_setspecific(_egl_TSD, (const void *) t);
66 static INLINE _EGLThreadInfo *_eglGetTSD(void)
69 return (_EGLThreadInfo *) _egl_TLS;
71 return (_EGLThreadInfo *) pthread_getspecific(_egl_TSD);
75 static INLINE void _eglFiniTSD(void)
77 _eglLockMutex(&_egl_TSDMutex);
78 if (_egl_TSDInitialized) {
79 _EGLThreadInfo *t = _eglGetTSD();
81 _egl_TSDInitialized = EGL_FALSE;
82 if (t && _egl_FreeTSD)
83 _egl_FreeTSD((void *) t);
84 pthread_key_delete(_egl_TSD);
86 _eglUnlockMutex(&_egl_TSDMutex);
89 static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
91 if (!_egl_TSDInitialized) {
92 _eglLockMutex(&_egl_TSDMutex);
94 /* check again after acquiring lock */
95 if (!_egl_TSDInitialized) {
96 if (pthread_key_create(&_egl_TSD, (void (*)(void *)) dtor) != 0) {
97 _eglUnlockMutex(&_egl_TSDMutex);
101 _eglAddAtExitCall(_eglFiniTSD);
102 _egl_TSDInitialized = EGL_TRUE;
105 _eglUnlockMutex(&_egl_TSDMutex);
112 static const _EGLThreadInfo *_egl_TSD;
113 static void (*_egl_FreeTSD)(_EGLThreadInfo *);
115 static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
120 static INLINE _EGLThreadInfo *_eglGetTSD(void)
122 return (_EGLThreadInfo *) _egl_TSD;
125 static INLINE void _eglFiniTSD(void)
127 if (_egl_FreeTSD && _egl_TSD)
128 _egl_FreeTSD((_EGLThreadInfo *) _egl_TSD);
131 static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
133 if (!_egl_FreeTSD && dtor) {
135 _eglAddAtExitCall(_eglFiniTSD);
140 #endif /* !PTHREADS */
144 _eglInitThreadInfo(_EGLThreadInfo *t)
146 memset(t, 0, sizeof(*t));
147 t->LastError = EGL_SUCCESS;
148 /* default, per EGL spec */
149 t->CurrentAPIIndex = _eglConvertApiToIndex(EGL_OPENGL_ES_API);
154 * Allocate and init a new _EGLThreadInfo object.
156 static _EGLThreadInfo *
157 _eglCreateThreadInfo(void)
159 _EGLThreadInfo *t = (_EGLThreadInfo *) calloc(1, sizeof(_EGLThreadInfo));
161 _eglInitThreadInfo(t);
169 * Delete/free a _EGLThreadInfo object.
172 _eglDestroyThreadInfo(_EGLThreadInfo *t)
174 if (t != &dummy_thread)
180 * Make sure TSD is initialized and return current value.
182 static INLINE _EGLThreadInfo *
183 _eglCheckedGetTSD(void)
185 if (_eglInitTSD(&_eglDestroyThreadInfo) != EGL_TRUE) {
186 _eglLog(_EGL_FATAL, "failed to initialize \"current\" system");
195 * Return the calling thread's thread info.
196 * If the calling thread nevers calls this function before, or if its thread
197 * info was destroyed, a new one is created. This function never returns NULL.
198 * In the case allocation fails, a dummy one is returned. See also
199 * _eglIsCurrentThreadDummy.
202 _eglGetCurrentThread(void)
204 _EGLThreadInfo *t = _eglCheckedGetTSD();
206 t = _eglCreateThreadInfo();
215 * Destroy the calling thread's thread info.
218 _eglDestroyCurrentThread(void)
220 _EGLThreadInfo *t = _eglCheckedGetTSD();
222 _eglDestroyThreadInfo(t);
229 * Return true if the calling thread's thread info is dummy.
230 * A dummy thread info is shared by all threads and should not be modified.
231 * Functions like eglBindAPI or eglMakeCurrent should check for dummy-ness
232 * before updating the thread info.
235 _eglIsCurrentThreadDummy(void)
237 _EGLThreadInfo *t = _eglCheckedGetTSD();
238 return (!t || t == &dummy_thread);
243 * Return the currently bound context of the given API, or NULL.
246 _eglGetAPIContext(EGLenum api)
248 _EGLThreadInfo *t = _eglGetCurrentThread();
249 return t->CurrentContexts[_eglConvertApiToIndex(api)];
254 * Return the currently bound context of the current API, or NULL.
257 _eglGetCurrentContext(void)
259 _EGLThreadInfo *t = _eglGetCurrentThread();
260 return t->CurrentContexts[t->CurrentAPIIndex];
265 * Record EGL error code and return EGL_FALSE.
268 _eglError(EGLint errCode, const char *msg)
270 _EGLThreadInfo *t = _eglGetCurrentThread();
272 if (t == &dummy_thread)
275 t->LastError = errCode;
277 if (errCode != EGL_SUCCESS) {
282 s = "EGL_BAD_ACCESS";
287 case EGL_BAD_ATTRIBUTE:
288 s = "EGL_BAD_ATTRIBUTE";
291 s = "EGL_BAD_CONFIG";
293 case EGL_BAD_CONTEXT:
294 s = "EGL_BAD_CONTEXT";
296 case EGL_BAD_CURRENT_SURFACE:
297 s = "EGL_BAD_CURRENT_SURFACE";
299 case EGL_BAD_DISPLAY:
300 s = "EGL_BAD_DISPLAY";
305 case EGL_BAD_NATIVE_PIXMAP:
306 s = "EGL_BAD_NATIVE_PIXMAP";
308 case EGL_BAD_NATIVE_WINDOW:
309 s = "EGL_BAD_NATIVE_WINDOW";
311 case EGL_BAD_PARAMETER:
312 s = "EGL_BAD_PARAMETER";
314 case EGL_BAD_SURFACE:
315 s = "EGL_BAD_SURFACE";
317 case EGL_NOT_INITIALIZED:
318 s = "EGL_NOT_INITIALIZED";
320 #ifdef EGL_MESA_screen_surface
321 case EGL_BAD_SCREEN_MESA:
322 s = "EGL_BAD_SCREEN_MESA";
324 case EGL_BAD_MODE_MESA:
325 s = "EGL_BAD_MODE_MESA";
329 s = "other EGL error";
331 _eglLog(_EGL_DEBUG, "EGL user error 0x%x (%s) in %s\n", errCode, s, msg);