Mercurial > sdl-ios-xcode
annotate src/thread/SDL_thread.c @ 1551:02e19471a694
Fixed bug #4
[Note: I'm applying this patch since it's a cleaner version of what's already implemented, and supports this controller on older kernels. I'll ask to make sure this doesn't break on the new kernels where it's no longer necessary]
Date: Mon, 21 Mar 2005 09:41:11 -0500
From: Chris Nelson
Subject: SDL Patch
Hey, Ryan.
I submitted the following patch about a year ago. It's just a simple
patch for the linux port, to make multiple joysticks each appear to SDL
as their own device, if they are on the same USB port (specifically,
these guys
<http://www.consoleplus.co.uk/product_info.php?pName=super-joybox-5-quad-joypad-converter>,
which allow 4 Playstation2 controllers to be accessed via a single USB
port). Without this patch, SDL pretty much drops the ball, and reports
that there are 4 joysticks available when less than that number are
plugged in.
My work built upon the work of another person with the same device. When
I submitted the patch to the list, he tested it, but it didn't work for
him, so the patch was never accepted. Maybe about 3 times in the past
year, I've tried to email the guy, to see if he couldn't run my new
version, complete with debug code to diagnose the problem he was having.
He never got back to me.
So, I'm attaching the patch. I wish I knew why it didn't work for him,
but I've been using it for the last year with no problems. Let me know
if you need any more information, or have any ideas as to how I could
test it. I'd like to see it in the tree, but I want to make sure it works.
-Chris
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 19 Mar 2006 06:31:34 +0000 |
parents | ad887c988713 |
children | 97d0966f4bf7 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* System independent thread management routines for SDL */ | |
25 | |
26 #include "SDL_mutex.h" | |
27 #include "SDL_thread.h" | |
28 #include "SDL_thread_c.h" | |
29 #include "SDL_systhread.h" | |
30 | |
31 #define ARRAY_CHUNKSIZE 32 | |
32 /* The array of threads currently active in the application | |
33 (except the main thread) | |
34 The manipulation of an array here is safer than using a linked list. | |
35 */ | |
36 static int SDL_maxthreads = 0; | |
37 static int SDL_numthreads = 0; | |
38 static SDL_Thread **SDL_Threads = NULL; | |
39 static SDL_mutex *thread_lock = NULL; | |
40 | |
41 int SDL_ThreadsInit(void) | |
42 { | |
43 int retval; | |
44 | |
45 retval = 0; | |
46 thread_lock = SDL_CreateMutex(); | |
47 if ( thread_lock == NULL ) { | |
48 retval = -1; | |
49 } | |
50 return(retval); | |
51 } | |
52 | |
53 /* This should never be called... | |
54 If this is called by SDL_Quit(), we don't know whether or not we should | |
55 clean up threads here. If any threads are still running after this call, | |
56 they will no longer have access to any per-thread data. | |
57 */ | |
58 void SDL_ThreadsQuit() | |
59 { | |
60 SDL_mutex *mutex; | |
61 | |
62 mutex = thread_lock; | |
63 thread_lock = NULL; | |
64 if ( mutex != NULL ) { | |
65 SDL_DestroyMutex(mutex); | |
66 } | |
67 } | |
68 | |
69 /* Routines for manipulating the thread list */ | |
70 static void SDL_AddThread(SDL_Thread *thread) | |
71 { | |
72 /* WARNING: | |
73 If the very first threads are created simultaneously, then | |
74 there could be a race condition causing memory corruption. | |
75 In practice, this isn't a problem because by definition there | |
76 is only one thread running the first time this is called. | |
77 */ | |
1499 | 78 if ( !thread_lock ) { |
0 | 79 if ( SDL_ThreadsInit() < 0 ) { |
80 return; | |
81 } | |
82 } | |
83 SDL_mutexP(thread_lock); | |
84 | |
85 /* Expand the list of threads, if necessary */ | |
86 #ifdef DEBUG_THREADS | |
87 printf("Adding thread (%d already - %d max)\n", | |
88 SDL_numthreads, SDL_maxthreads); | |
89 #endif | |
90 if ( SDL_numthreads == SDL_maxthreads ) { | |
1499 | 91 SDL_Thread **threads; |
92 threads = (SDL_Thread **)SDL_realloc(SDL_Threads, | |
93 (SDL_maxthreads+ARRAY_CHUNKSIZE)*(sizeof *threads)); | |
0 | 94 if ( threads == NULL ) { |
95 SDL_OutOfMemory(); | |
96 goto done; | |
97 } | |
98 SDL_maxthreads += ARRAY_CHUNKSIZE; | |
99 SDL_Threads = threads; | |
100 } | |
101 SDL_Threads[SDL_numthreads++] = thread; | |
102 done: | |
103 SDL_mutexV(thread_lock); | |
104 } | |
105 | |
106 static void SDL_DelThread(SDL_Thread *thread) | |
107 { | |
108 int i; | |
109 | |
1499 | 110 if ( !thread_lock ) { |
111 return; | |
112 } | |
113 SDL_mutexP(thread_lock); | |
114 for ( i=0; i<SDL_numthreads; ++i ) { | |
115 if ( thread == SDL_Threads[i] ) { | |
116 break; | |
0 | 117 } |
1499 | 118 } |
119 if ( i < SDL_numthreads ) { | |
120 if ( --SDL_numthreads > 0 ) { | |
121 while ( i < SDL_numthreads ) { | |
122 SDL_Threads[i] = SDL_Threads[i+1]; | |
123 ++i; | |
0 | 124 } |
1499 | 125 } else { |
126 SDL_maxthreads = 0; | |
127 SDL_free(SDL_Threads); | |
128 SDL_Threads = NULL; | |
129 } | |
0 | 130 #ifdef DEBUG_THREADS |
1499 | 131 printf("Deleting thread (%d left - %d max)\n", |
132 SDL_numthreads, SDL_maxthreads); | |
0 | 133 #endif |
1499 | 134 } |
135 SDL_mutexV(thread_lock); | |
136 | |
137 if ( SDL_Threads == NULL ) { | |
138 SDL_ThreadsQuit(); | |
0 | 139 } |
140 } | |
141 | |
142 /* The default (non-thread-safe) global error variable */ | |
143 static SDL_error SDL_global_error; | |
144 | |
145 /* Routine to get the thread-specific error variable */ | |
146 SDL_error *SDL_GetErrBuf(void) | |
147 { | |
148 SDL_error *errbuf; | |
149 | |
150 errbuf = &SDL_global_error; | |
151 if ( SDL_Threads ) { | |
152 int i; | |
153 Uint32 this_thread; | |
154 | |
155 this_thread = SDL_ThreadID(); | |
156 SDL_mutexP(thread_lock); | |
157 for ( i=0; i<SDL_numthreads; ++i ) { | |
158 if ( this_thread == SDL_Threads[i]->threadid ) { | |
159 errbuf = &SDL_Threads[i]->errbuf; | |
160 break; | |
161 } | |
162 } | |
163 SDL_mutexV(thread_lock); | |
164 } | |
165 return(errbuf); | |
166 } | |
167 | |
168 | |
169 /* Arguments and callback to setup and run the user thread function */ | |
170 typedef struct { | |
171 int (*func)(void *); | |
172 void *data; | |
173 SDL_Thread *info; | |
174 SDL_sem *wait; | |
175 } thread_args; | |
176 | |
177 void SDL_RunThread(void *data) | |
178 { | |
179 thread_args *args; | |
180 int (*userfunc)(void *); | |
181 void *userdata; | |
182 int *statusloc; | |
183 | |
184 /* Perform any system-dependent setup | |
185 - this function cannot fail, and cannot use SDL_SetError() | |
186 */ | |
187 SDL_SYS_SetupThread(); | |
188 | |
189 /* Get the thread id */ | |
190 args = (thread_args *)data; | |
191 args->info->threadid = SDL_ThreadID(); | |
192 | |
193 /* Figure out what function to run */ | |
194 userfunc = args->func; | |
195 userdata = args->data; | |
196 statusloc = &args->info->status; | |
197 | |
198 /* Wake up the parent thread */ | |
199 SDL_SemPost(args->wait); | |
200 | |
201 /* Run the function */ | |
202 *statusloc = userfunc(userdata); | |
203 } | |
204 | |
1471
9fb0eee04dd9
Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
205 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
206 #undef SDL_CreateThread |
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
207 DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (*fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread) |
1190 | 208 #else |
209 DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (*fn)(void *), void *data) | |
210 #endif | |
0 | 211 { |
212 SDL_Thread *thread; | |
213 thread_args *args; | |
214 int ret; | |
215 | |
216 /* Allocate memory for the thread info structure */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
217 thread = (SDL_Thread *)SDL_malloc(sizeof(*thread)); |
0 | 218 if ( thread == NULL ) { |
219 SDL_OutOfMemory(); | |
220 return(NULL); | |
221 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
222 SDL_memset(thread, 0, (sizeof *thread)); |
0 | 223 thread->status = -1; |
224 | |
225 /* Set up the arguments for the thread */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
226 args = (thread_args *)SDL_malloc(sizeof(*args)); |
0 | 227 if ( args == NULL ) { |
228 SDL_OutOfMemory(); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
229 SDL_free(thread); |
0 | 230 return(NULL); |
231 } | |
232 args->func = fn; | |
233 args->data = data; | |
234 args->info = thread; | |
235 args->wait = SDL_CreateSemaphore(0); | |
236 if ( args->wait == NULL ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
237 SDL_free(thread); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
238 SDL_free(args); |
0 | 239 return(NULL); |
240 } | |
241 | |
242 /* Add the thread to the list of available threads */ | |
243 SDL_AddThread(thread); | |
244 | |
245 /* Create the thread and go! */ | |
1471
9fb0eee04dd9
Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
246 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
247 ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread); |
1190 | 248 #else |
0 | 249 ret = SDL_SYS_CreateThread(thread, args); |
1190 | 250 #endif |
0 | 251 if ( ret >= 0 ) { |
252 /* Wait for the thread function to use arguments */ | |
253 SDL_SemWait(args->wait); | |
254 } else { | |
255 /* Oops, failed. Gotta free everything */ | |
256 SDL_DelThread(thread); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
257 SDL_free(thread); |
0 | 258 thread = NULL; |
259 } | |
260 SDL_DestroySemaphore(args->wait); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
261 SDL_free(args); |
0 | 262 |
263 /* Everything is running now */ | |
264 return(thread); | |
265 } | |
266 | |
267 void SDL_WaitThread(SDL_Thread *thread, int *status) | |
268 { | |
269 if ( thread ) { | |
270 SDL_SYS_WaitThread(thread); | |
271 if ( status ) { | |
272 *status = thread->status; | |
273 } | |
274 SDL_DelThread(thread); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
275 SDL_free(thread); |
0 | 276 } |
277 } | |
278 | |
279 Uint32 SDL_GetThreadID(SDL_Thread *thread) | |
280 { | |
281 Uint32 id; | |
282 | |
283 if ( thread ) { | |
284 id = thread->threadid; | |
285 } else { | |
286 id = SDL_ThreadID(); | |
287 } | |
288 return(id); | |
289 } | |
290 | |
291 void SDL_KillThread(SDL_Thread *thread) | |
292 { | |
293 if ( thread ) { | |
294 SDL_SYS_KillThread(thread); | |
295 SDL_WaitThread(thread, NULL); | |
296 } | |
297 } | |
298 |