2 * Guest-side implementation of GL/GLX API. Replacement of standard libGL.so
4 * Copyright (c) 2006,2007 Even Rouault
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #ifdef ENABLE_THREAD_SAFETY
30 extern GLState* new_gl_state();
32 extern pthread_mutex_t global_mutex;
33 extern pthread_key_t key_current_gl_state;
35 extern pthread_t last_current_thread;
36 extern GLState* _mono_threaded_current_gl_state;
37 extern GLState* default_gl_state;
41 /* The concepts used here are coming directly from http://www.mesa3d.org/dispatch.html */
43 #ifdef TEST_IF_LOCK_USE_IS_CORRECT_INTO_MONO_THREADED_CASE
44 static int lock_count = 0;
45 #define LOCK(func_number) int __lock__ = 0; assert(lock_count == 0); lock_count++; pthread_mutex_lock( &global_mutex );
46 #define UNLOCK(func_number) assert(lock_count + __lock__ == 1); lock_count--; pthread_mutex_unlock( &global_mutex );
48 static int _is_mt = 2;
50 static int _is_mt = 0;
51 static inline int is_mt()
54 pthread_t current_thread = pthread_self();
55 if (last_current_thread == 0)
56 last_current_thread = current_thread;
57 if (current_thread != last_current_thread)
60 log_gl("-------- Two threads at least are doing OpenGL ---------\n");
61 pthread_key_create(&key_current_gl_state, NULL);
65 #define IS_MT() is_mt()
67 /* The idea here is that the first GL/GLX call made in each thread is necessary a GLX call */
68 /* So in the case where it's a GLX call we always take the lock and check if we're in MT case */
69 /* otherwise (regular GL call), we have to take the lock only in the MT case */
70 #define LOCK(func_number) do { if (IS_GLX_CALL(func_number)) { pthread_mutex_lock( &global_mutex ); IS_MT(); } else if (_is_mt) { pthread_mutex_lock( &global_mutex ); } } while(0)
71 #define UNLOCK(func_number) do { if (IS_GLX_CALL(func_number) || _is_mt) pthread_mutex_unlock( &global_mutex ); } while(0)
74 static void set_current_state(GLState* current_gl_state)
77 pthread_setspecific(key_current_gl_state, current_gl_state);
79 _mono_threaded_current_gl_state = current_gl_state;
82 static inline GLState* get_current_state()
84 GLState* current_gl_state;
86 last_current_thread == pthread_self())
89 set_current_state(_mono_threaded_current_gl_state);
90 _mono_threaded_current_gl_state = NULL;
92 current_gl_state = (_is_mt) ? pthread_getspecific(key_current_gl_state) : _mono_threaded_current_gl_state;
93 if (current_gl_state == NULL)
95 if (default_gl_state == NULL)
97 default_gl_state = new_gl_state();
99 current_gl_state = default_gl_state;
100 set_current_state(current_gl_state);
102 return current_gl_state;
104 #define SET_CURRENT_STATE(_x) set_current_state(_x)
106 /* No support for threading */
108 #define LOCK(func_number)
109 #define UNLOCK(func_number)
110 #define GET_CURRENT_THREAD() 0
112 static GLState* current_gl_state = NULL;
113 static inline GLState* get_current_state()
115 if (current_gl_state == NULL)
117 if (default_gl_state == NULL)
119 default_gl_state = new_gl_state();
121 return default_gl_state;
123 return current_gl_state;
125 #define SET_CURRENT_STATE(_x) current_gl_state = _x
128 #define GET_CURRENT_STATE() GLState* state = get_current_state()