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