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