2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* System independent thread management routines for SDL */
26 #include "SDL_mutex.h"
27 #include "SDL_thread.h"
28 #include "SDL_thread_c.h"
29 #include "SDL_systhread.h"
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.
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;
41 int SDL_ThreadsInit(void)
46 thread_lock = SDL_CreateMutex();
47 if ( thread_lock == NULL ) {
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.
58 void SDL_ThreadsQuit(void)
64 if ( mutex != NULL ) {
65 SDL_DestroyMutex(mutex);
69 /* Routines for manipulating the thread list */
70 static void SDL_AddThread(SDL_Thread *thread)
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.
79 if ( SDL_ThreadsInit() < 0 ) {
83 SDL_mutexP(thread_lock);
85 /* Expand the list of threads, if necessary */
87 printf("Adding thread (%d already - %d max)\n",
88 SDL_numthreads, SDL_maxthreads);
90 if ( SDL_numthreads == SDL_maxthreads ) {
92 threads = (SDL_Thread **)SDL_realloc(SDL_Threads,
93 (SDL_maxthreads+ARRAY_CHUNKSIZE)*(sizeof *threads));
94 if ( threads == NULL ) {
98 SDL_maxthreads += ARRAY_CHUNKSIZE;
99 SDL_Threads = threads;
101 SDL_Threads[SDL_numthreads++] = thread;
103 SDL_mutexV(thread_lock);
106 static void SDL_DelThread(SDL_Thread *thread)
110 if ( !thread_lock ) {
113 SDL_mutexP(thread_lock);
114 for ( i=0; i<SDL_numthreads; ++i ) {
115 if ( thread == SDL_Threads[i] ) {
119 if ( i < SDL_numthreads ) {
120 if ( --SDL_numthreads > 0 ) {
121 while ( i < SDL_numthreads ) {
122 SDL_Threads[i] = SDL_Threads[i+1];
127 SDL_free(SDL_Threads);
131 printf("Deleting thread (%d left - %d max)\n",
132 SDL_numthreads, SDL_maxthreads);
135 SDL_mutexV(thread_lock);
137 #if 0 /* There could be memory corruption if another thread is starting */
138 if ( SDL_Threads == NULL ) {
144 /* The default (non-thread-safe) global error variable */
145 static SDL_error SDL_global_error;
147 /* Routine to get the thread-specific error variable */
148 SDL_error *SDL_GetErrBuf(void)
152 errbuf = &SDL_global_error;
157 this_thread = SDL_ThreadID();
158 SDL_mutexP(thread_lock);
159 for ( i=0; i<SDL_numthreads; ++i ) {
160 if ( this_thread == SDL_Threads[i]->threadid ) {
161 errbuf = &SDL_Threads[i]->errbuf;
165 SDL_mutexV(thread_lock);
171 /* Arguments and callback to setup and run the user thread function */
173 int (SDLCALL *func)(void *);
179 void SDL_RunThread(void *data)
182 int (SDLCALL *userfunc)(void *);
186 /* Perform any system-dependent setup
187 - this function cannot fail, and cannot use SDL_SetError()
189 SDL_SYS_SetupThread();
191 /* Get the thread id */
192 args = (thread_args *)data;
193 args->info->threadid = SDL_ThreadID();
195 /* Figure out what function to run */
196 userfunc = args->func;
197 userdata = args->data;
198 statusloc = &args->info->status;
200 /* Wake up the parent thread */
201 SDL_SemPost(args->wait);
203 /* Run the function */
204 *statusloc = userfunc(userdata);
207 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
208 #undef SDL_CreateThread
209 DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
211 DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data)
218 /* Allocate memory for the thread info structure */
219 thread = (SDL_Thread *)SDL_malloc(sizeof(*thread));
220 if ( thread == NULL ) {
224 SDL_memset(thread, 0, (sizeof *thread));
227 /* Set up the arguments for the thread */
228 args = (thread_args *)SDL_malloc(sizeof(*args));
229 if ( args == NULL ) {
237 args->wait = SDL_CreateSemaphore(0);
238 if ( args->wait == NULL ) {
244 /* Add the thread to the list of available threads */
245 SDL_AddThread(thread);
247 /* Create the thread and go! */
248 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
249 ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
251 ret = SDL_SYS_CreateThread(thread, args);
254 /* Wait for the thread function to use arguments */
255 SDL_SemWait(args->wait);
257 /* Oops, failed. Gotta free everything */
258 SDL_DelThread(thread);
262 SDL_DestroySemaphore(args->wait);
265 /* Everything is running now */
269 void SDL_WaitThread(SDL_Thread *thread, int *status)
272 SDL_SYS_WaitThread(thread);
274 *status = thread->status;
276 SDL_DelThread(thread);
281 Uint32 SDL_GetThreadID(SDL_Thread *thread)
286 id = thread->threadid;
293 void SDL_KillThread(SDL_Thread *thread)
296 SDL_SYS_KillThread(thread);
297 SDL_WaitThread(thread, NULL);