Initial commit
[profile/ivi/simulator-opengl.git] / libGL / lock.h
1 /*
2  *  Guest-side implementation of GL/GLX API. Replacement of standard libGL.so
3  *
4  *  Copyright (c) 2006,2007 Even Rouault
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 #include <pthread.h>
25 #include "common.h"
26 #include "log.h"
27
28 #ifdef ENABLE_THREAD_SAFETY
29
30 extern GLState* new_gl_state();
31
32 extern pthread_mutex_t global_mutex;
33 extern pthread_key_t   key_current_gl_state;
34
35 extern pthread_t last_current_thread;
36 extern GLState* _mono_threaded_current_gl_state;
37 extern GLState* default_gl_state;
38
39
40 /* Posix threading */
41 /* The concepts used here are coming directly from http://www.mesa3d.org/dispatch.html */
42
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 );
47   #define IS_MT() 2
48   static int _is_mt = 2;
49 #else
50   static int _is_mt = 0;
51   static inline int is_mt()
52   {
53     if (_is_mt) return 1;
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)
58     {
59       _is_mt = 1;
60       log_gl("-------- Two threads at least are doing OpenGL ---------\n");
61       pthread_key_create(&key_current_gl_state, NULL);
62     }
63     return _is_mt;
64   }
65   #define IS_MT() is_mt()
66
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)
72 #endif
73
74 static void set_current_state(GLState* current_gl_state)
75 {
76   if (_is_mt)
77     pthread_setspecific(key_current_gl_state, current_gl_state);
78   else
79     _mono_threaded_current_gl_state = current_gl_state;
80 }
81
82 static inline GLState* get_current_state()
83 {
84   GLState* current_gl_state;
85   if (_is_mt == 1 &&
86       last_current_thread == pthread_self())
87   {
88     _is_mt = 2;
89     set_current_state(_mono_threaded_current_gl_state);
90     _mono_threaded_current_gl_state = NULL;
91   }
92   current_gl_state = (_is_mt) ? pthread_getspecific(key_current_gl_state) : _mono_threaded_current_gl_state;
93   if (current_gl_state == NULL)
94   {
95     if (default_gl_state == NULL)
96     {
97       default_gl_state = new_gl_state();
98     }
99     current_gl_state = default_gl_state;
100     set_current_state(current_gl_state);
101   }
102   return current_gl_state;
103 }
104 #define SET_CURRENT_STATE(_x) set_current_state(_x)
105
106 /* No support for threading */
107 #else
108 #define LOCK(func_number)
109 #define UNLOCK(func_number)
110 #define GET_CURRENT_THREAD() 0
111 #define IS_MT() 0
112 static GLState* current_gl_state = NULL;
113 static inline GLState* get_current_state()
114 {
115   if (current_gl_state == NULL)
116   {
117     if (default_gl_state == NULL)
118     {
119       default_gl_state = new_gl_state();
120     }
121     return default_gl_state;
122   }
123   return current_gl_state;
124 }
125 #define SET_CURRENT_STATE(_x) current_gl_state = _x
126 #endif
127
128 #define GET_CURRENT_STATE()   GLState* state = get_current_state()