Mercurial > sdl-ios-xcode
annotate src/thread/amigaos/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 | d910939febfa |
children | 782fd950bd46 c121d94672cb |
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:
769
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:
769
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:
769
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:
769
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:
769
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:
769
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:
769
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:
21
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:
1361
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" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
28 #include "../SDL_thread_c.h" |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
29 #include "../SDL_systhread.h" |
0 | 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 struct SignalSemaphore thread_lock; | |
40 int thread_lock_created = 0; | |
41 | |
42 int SDL_ThreadsInit(void) | |
43 { | |
44 InitSemaphore(&thread_lock); | |
45 thread_lock_created=1; | |
46 return 0; | |
47 } | |
48 | |
49 /* This should never be called... | |
50 If this is called by SDL_Quit(), we don't know whether or not we should | |
51 clean up threads here. If any threads are still running after this call, | |
52 they will no longer have access to any per-thread data. | |
53 */ | |
54 void SDL_ThreadsQuit() | |
55 { | |
56 thread_lock_created=0; | |
57 } | |
58 | |
59 /* Routines for manipulating the thread list */ | |
60 static void SDL_AddThread(SDL_Thread *thread) | |
61 { | |
62 SDL_Thread **threads; | |
63 | |
64 /* WARNING: | |
65 If the very first threads are created simultaneously, then | |
66 there could be a race condition causing memory corruption. | |
67 In practice, this isn't a problem because by definition there | |
68 is only one thread running the first time this is called. | |
69 */ | |
70 if ( !thread_lock_created ) { | |
71 if ( SDL_ThreadsInit() < 0 ) { | |
72 return; | |
73 } | |
74 } | |
75 ObtainSemaphore(&thread_lock); | |
76 | |
77 /* Expand the list of threads, if necessary */ | |
78 #ifdef DEBUG_THREADS | |
79 printf("Adding thread (%d already - %d max)\n", | |
80 SDL_numthreads, SDL_maxthreads); | |
81 #endif | |
82 if ( SDL_numthreads == SDL_maxthreads ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
83 threads=(SDL_Thread **)SDL_malloc((SDL_maxthreads+ARRAY_CHUNKSIZE)* |
0 | 84 (sizeof *threads)); |
85 if ( threads == NULL ) { | |
86 SDL_OutOfMemory(); | |
87 goto done; | |
88 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
89 SDL_memcpy(threads, SDL_Threads, SDL_numthreads*(sizeof *threads)); |
0 | 90 SDL_maxthreads += ARRAY_CHUNKSIZE; |
91 if ( SDL_Threads ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
92 SDL_free(SDL_Threads); |
0 | 93 } |
94 SDL_Threads = threads; | |
95 } | |
96 SDL_Threads[SDL_numthreads++] = thread; | |
97 done: | |
98 ReleaseSemaphore(&thread_lock); | |
99 } | |
100 | |
101 static void SDL_DelThread(SDL_Thread *thread) | |
102 { | |
103 int i; | |
104 | |
105 if ( thread_lock_created ) { | |
106 ObtainSemaphore(&thread_lock); | |
107 for ( i=0; i<SDL_numthreads; ++i ) { | |
108 if ( thread == SDL_Threads[i] ) { | |
109 break; | |
110 } | |
111 } | |
112 if ( i < SDL_numthreads ) { | |
113 --SDL_numthreads; | |
114 while ( i < SDL_numthreads ) { | |
115 SDL_Threads[i] = SDL_Threads[i+1]; | |
116 ++i; | |
117 } | |
118 #ifdef DEBUG_THREADS | |
119 printf("Deleting thread (%d left - %d max)\n", | |
120 SDL_numthreads, SDL_maxthreads); | |
121 #endif | |
122 } | |
123 ReleaseSemaphore(&thread_lock); | |
124 } | |
125 } | |
126 | |
127 /* The default (non-thread-safe) global error variable */ | |
128 static SDL_error SDL_global_error; | |
129 | |
130 /* Routine to get the thread-specific error variable */ | |
131 SDL_error *SDL_GetErrBuf(void) | |
132 { | |
133 SDL_error *errbuf; | |
134 | |
135 errbuf = &SDL_global_error; | |
136 if ( SDL_Threads ) { | |
137 int i; | |
138 Uint32 this_thread; | |
139 | |
140 this_thread = SDL_ThreadID(); | |
141 ObtainSemaphore(&thread_lock); | |
142 for ( i=0; i<SDL_numthreads; ++i ) { | |
143 if ( this_thread == SDL_Threads[i]->threadid ) { | |
144 errbuf = &SDL_Threads[i]->errbuf; | |
145 break; | |
146 } | |
147 } | |
148 ReleaseSemaphore(&thread_lock); | |
149 } | |
150 return(errbuf); | |
151 } | |
152 | |
153 | |
154 /* Arguments and callback to setup and run the user thread function */ | |
155 typedef struct { | |
156 int (*func)(void *); | |
157 void *data; | |
158 SDL_Thread *info; | |
159 struct Task *wait; | |
160 } thread_args; | |
161 | |
162 void SDL_RunThread(void *data) | |
163 { | |
164 thread_args *args; | |
165 int (*userfunc)(void *); | |
166 void *userdata; | |
167 int *statusloc; | |
168 | |
169 /* Perform any system-dependent setup | |
170 - this function cannot fail, and cannot use SDL_SetError() | |
171 */ | |
172 SDL_SYS_SetupThread(); | |
173 | |
174 /* Get the thread id */ | |
175 args = (thread_args *)data; | |
176 args->info->threadid = SDL_ThreadID(); | |
177 | |
178 /* Figure out what function to run */ | |
179 userfunc = args->func; | |
180 userdata = args->data; | |
181 statusloc = &args->info->status; | |
182 | |
183 /* Wake up the parent thread */ | |
184 Signal(args->wait,SIGBREAKF_CTRL_E); | |
185 | |
186 /* Run the function */ | |
187 *statusloc = userfunc(userdata); | |
188 } | |
189 | |
190 SDL_Thread *SDL_CreateThread(int (*fn)(void *), void *data) | |
191 { | |
192 SDL_Thread *thread; | |
193 thread_args *args; | |
194 int ret; | |
195 | |
196 /* Allocate memory for the thread info structure */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
197 thread = (SDL_Thread *)SDL_malloc(sizeof(*thread)); |
0 | 198 if ( thread == NULL ) { |
199 SDL_OutOfMemory(); | |
200 return(NULL); | |
201 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
202 SDL_memset(thread, 0, (sizeof *thread)); |
0 | 203 thread->status = -1; |
204 | |
205 /* Set up the arguments for the thread */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
206 args = (thread_args *)SDL_malloc(sizeof(*args)); |
0 | 207 if ( args == NULL ) { |
208 SDL_OutOfMemory(); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
209 SDL_free(thread); |
0 | 210 return(NULL); |
211 } | |
212 args->func = fn; | |
213 args->data = data; | |
214 args->info = thread; | |
215 args->wait = FindTask(NULL); | |
216 if ( args->wait == NULL ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
217 SDL_free(thread); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
218 SDL_free(args); |
0 | 219 SDL_OutOfMemory(); |
220 return(NULL); | |
221 } | |
222 | |
223 /* Add the thread to the list of available threads */ | |
224 SDL_AddThread(thread); | |
225 | |
226 D(bug("Starting thread...\n")); | |
227 | |
228 /* Create the thread and go! */ | |
229 ret = SDL_SYS_CreateThread(thread, args); | |
230 if ( ret >= 0 ) { | |
231 D(bug("Waiting for thread CTRL_E...\n")); | |
232 /* Wait for the thread function to use arguments */ | |
233 Wait(SIGBREAKF_CTRL_E); | |
234 D(bug(" Arrived.")); | |
235 } else { | |
236 /* Oops, failed. Gotta free everything */ | |
237 SDL_DelThread(thread); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
238 SDL_free(thread); |
0 | 239 thread = NULL; |
240 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
241 SDL_free(args); |
0 | 242 |
243 /* Everything is running now */ | |
244 return(thread); | |
245 } | |
246 | |
247 void SDL_WaitThread(SDL_Thread *thread, int *status) | |
248 { | |
249 if ( thread ) { | |
250 SDL_SYS_WaitThread(thread); | |
251 if ( status ) { | |
252 *status = thread->status; | |
253 } | |
254 SDL_DelThread(thread); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
255 SDL_free(thread); |
0 | 256 } |
257 } | |
258 | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
259 Uint32 SDL_GetThreadID(SDL_Thread *thread) |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
260 { |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
261 Uint32 id; |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
262 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
263 if ( thread ) { |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
264 id = thread->threadid; |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
265 } else { |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
266 id = SDL_ThreadID(); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
267 } |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
268 return(id); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
269 } |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
270 |
0 | 271 void SDL_KillThread(SDL_Thread *thread) |
272 { | |
273 if ( thread ) { | |
274 SDL_SYS_KillThread(thread); | |
275 SDL_WaitThread(thread, NULL); | |
276 } | |
277 } | |
278 |