comparison src/thread/amigaos/SDL_thread.c @ 1668:4da1ee79c9af SDL-1.3

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