src/thread/SDL_thread.c
author Sam Lantinga
Mon, 01 Apr 2002 15:35:28 +0000
changeset 329 1d74ddc90cb2
parent 297 f6ffac90895c
child 340 b9f1ce709960
permissions -rw-r--r--
Patrice's fixes for GNU Pthread support
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     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
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* System independent thread management routines for SDL */
    29 
    30 #include <stdio.h>
    31 #include <stdlib.h>
    32 #include <string.h>
    33 
    34 #include "SDL_error.h"
    35 #include "SDL_mutex.h"
    36 #include "SDL_thread.h"
    37 #include "SDL_thread_c.h"
    38 #include "SDL_systhread.h"
    39 
    40 #define ARRAY_CHUNKSIZE	32
    41 /* The array of threads currently active in the application
    42    (except the main thread)
    43    The manipulation of an array here is safer than using a linked list.
    44 */
    45 static int SDL_maxthreads = 0;
    46 static int SDL_numthreads = 0;
    47 static SDL_Thread **SDL_Threads = NULL;
    48 static SDL_mutex *thread_lock = NULL;
    49 int _creating_thread_lock = 0;
    50 
    51 int SDL_ThreadsInit(void)
    52 {
    53 	int retval;
    54 
    55 #ifdef ENABLE_PTH
    56 	if (!pth_init()) {
    57 		return -1;
    58 	}
    59 #endif
    60 
    61 	retval = 0;
    62 	/* Set the thread lock creation flag so that we can reuse an
    63 	   existing lock on the system - since this mutex never gets
    64 	   destroyed (see SDL_ThreadsQuit()), we want to reuse it.
    65 	*/
    66 	_creating_thread_lock = 1;
    67 	thread_lock = SDL_CreateMutex();
    68 	_creating_thread_lock = 0;
    69 	if ( thread_lock == NULL ) {
    70 		retval = -1;
    71 	}
    72 	return(retval);
    73 }
    74 
    75 /* This should never be called...
    76    If this is called by SDL_Quit(), we don't know whether or not we should
    77    clean up threads here.  If any threads are still running after this call,
    78    they will no longer have access to any per-thread data.
    79  */
    80 void SDL_ThreadsQuit()
    81 {
    82 	SDL_mutex *mutex;
    83 
    84 	mutex = thread_lock;
    85 	thread_lock = NULL;
    86 	if ( mutex != NULL ) {
    87 		SDL_DestroyMutex(mutex);
    88 	}
    89 
    90 #ifdef ENABLE_PTH
    91 	pth_kill();
    92 #endif
    93 }
    94 
    95 /* Routines for manipulating the thread list */
    96 static void SDL_AddThread(SDL_Thread *thread)
    97 {
    98 	SDL_Thread **threads;
    99 
   100 	/* WARNING:
   101 	   If the very first threads are created simultaneously, then
   102 	   there could be a race condition causing memory corruption.
   103 	   In practice, this isn't a problem because by definition there
   104 	   is only one thread running the first time this is called.
   105 	*/
   106 	if ( thread_lock == NULL ) {
   107 		if ( SDL_ThreadsInit() < 0 ) {
   108 			return;
   109 		}
   110 	}
   111 	SDL_mutexP(thread_lock);
   112 
   113 	/* Expand the list of threads, if necessary */
   114 #ifdef DEBUG_THREADS
   115 	printf("Adding thread (%d already - %d max)\n",
   116 			SDL_numthreads, SDL_maxthreads);
   117 #endif
   118 	if ( SDL_numthreads == SDL_maxthreads ) {
   119 		threads=(SDL_Thread **)malloc((SDL_maxthreads+ARRAY_CHUNKSIZE)*
   120 		                              (sizeof *threads));
   121 		if ( threads == NULL ) {
   122 			SDL_OutOfMemory();
   123 			goto done;
   124 		}
   125 		memcpy(threads, SDL_Threads, SDL_numthreads*(sizeof *threads));
   126 		SDL_maxthreads += ARRAY_CHUNKSIZE;
   127 		if ( SDL_Threads ) {
   128 			free(SDL_Threads);
   129 		}
   130 		SDL_Threads = threads;
   131 	}
   132 	SDL_Threads[SDL_numthreads++] = thread;
   133 done:
   134 	SDL_mutexV(thread_lock);
   135 }
   136 
   137 static void SDL_DelThread(SDL_Thread *thread)
   138 {
   139 	int i;
   140 
   141 	if ( thread_lock ) {
   142 		SDL_mutexP(thread_lock);
   143 		for ( i=0; i<SDL_numthreads; ++i ) {
   144 			if ( thread == SDL_Threads[i] ) {
   145 				break;
   146 			}
   147 		}
   148 		if ( i < SDL_numthreads ) {
   149 			--SDL_numthreads;
   150 			while ( i < SDL_numthreads ) {
   151 				SDL_Threads[i] = SDL_Threads[i+1];
   152 				++i;
   153 			}
   154 #ifdef DEBUG_THREADS
   155 			printf("Deleting thread (%d left - %d max)\n",
   156 					SDL_numthreads, SDL_maxthreads);
   157 #endif
   158 		}
   159 		SDL_mutexV(thread_lock);
   160 	}
   161 }
   162 
   163 /* The default (non-thread-safe) global error variable */
   164 static SDL_error SDL_global_error;
   165 
   166 /* Routine to get the thread-specific error variable */
   167 SDL_error *SDL_GetErrBuf(void)
   168 {
   169 	SDL_error *errbuf;
   170 
   171 	errbuf = &SDL_global_error;
   172 	if ( SDL_Threads ) {
   173 		int i;
   174 		Uint32 this_thread;
   175 
   176 		this_thread = SDL_ThreadID();
   177 		SDL_mutexP(thread_lock);
   178 		for ( i=0; i<SDL_numthreads; ++i ) {
   179 			if ( this_thread == SDL_Threads[i]->threadid ) {
   180 				errbuf = &SDL_Threads[i]->errbuf;
   181 				break;
   182 			}
   183 		}
   184 		SDL_mutexV(thread_lock);
   185 	}
   186 	return(errbuf);
   187 }
   188 
   189 
   190 /* Arguments and callback to setup and run the user thread function */
   191 typedef struct {
   192 	int (*func)(void *);
   193 	void *data;
   194 	SDL_Thread *info;
   195 	SDL_sem *wait;
   196 } thread_args;
   197 
   198 void SDL_RunThread(void *data)
   199 {
   200 	thread_args *args;
   201 	int (*userfunc)(void *);
   202 	void *userdata;
   203 	int *statusloc;
   204 
   205 	/* Perform any system-dependent setup
   206 	   - this function cannot fail, and cannot use SDL_SetError()
   207 	 */
   208 	SDL_SYS_SetupThread();
   209 
   210 	/* Get the thread id */
   211 	args = (thread_args *)data;
   212 	args->info->threadid = SDL_ThreadID();
   213 
   214 	/* Figure out what function to run */
   215 	userfunc = args->func;
   216 	userdata = args->data;
   217 	statusloc = &args->info->status;
   218 
   219 	/* Wake up the parent thread */
   220 	SDL_SemPost(args->wait);
   221 
   222 	/* Run the function */
   223 	*statusloc = userfunc(userdata);
   224 }
   225 
   226 SDL_Thread *SDL_CreateThread(int (*fn)(void *), void *data)
   227 {
   228 	SDL_Thread *thread;
   229 	thread_args *args;
   230 	int ret;
   231 
   232 	/* Allocate memory for the thread info structure */
   233 	thread = (SDL_Thread *)malloc(sizeof(*thread));
   234 	if ( thread == NULL ) {
   235 		SDL_OutOfMemory();
   236 		return(NULL);
   237 	}
   238 	memset(thread, 0, (sizeof *thread));
   239 	thread->status = -1;
   240 
   241 	/* Set up the arguments for the thread */
   242 	args = (thread_args *)malloc(sizeof(*args));
   243 	if ( args == NULL ) {
   244 		SDL_OutOfMemory();
   245 		free(thread);
   246 		return(NULL);
   247 	}
   248 	args->func = fn;
   249 	args->data = data;
   250 	args->info = thread;
   251 	args->wait = SDL_CreateSemaphore(0);
   252 	if ( args->wait == NULL ) {
   253 		free(thread);
   254 		free(args);
   255 		return(NULL);
   256 	}
   257 
   258 	/* Add the thread to the list of available threads */
   259 	SDL_AddThread(thread);
   260 
   261 	/* Create the thread and go! */
   262 	ret = SDL_SYS_CreateThread(thread, args);
   263 	if ( ret >= 0 ) {
   264 		/* Wait for the thread function to use arguments */
   265 		SDL_SemWait(args->wait);
   266 	} else {
   267 		/* Oops, failed.  Gotta free everything */
   268 		SDL_DelThread(thread);
   269 		free(thread);
   270 		thread = NULL;
   271 	}
   272 	SDL_DestroySemaphore(args->wait);
   273 	free(args);
   274 
   275 	/* Everything is running now */
   276 	return(thread);
   277 }
   278 
   279 void SDL_WaitThread(SDL_Thread *thread, int *status)
   280 {
   281 	if ( thread ) {
   282 		SDL_SYS_WaitThread(thread);
   283 		if ( status ) {
   284 			*status = thread->status;
   285 		}
   286 		SDL_DelThread(thread);
   287 		free(thread);
   288 	}
   289 }
   290 
   291 Uint32 SDL_GetThreadID(SDL_Thread *thread)
   292 {
   293 	Uint32 id;
   294 
   295 	if ( thread ) {
   296 		id = thread->threadid;
   297 	} else {
   298 		id = SDL_ThreadID();
   299 	}
   300 	return(id);
   301 }
   302 
   303 void SDL_KillThread(SDL_Thread *thread)
   304 {
   305 	if ( thread ) {
   306 		SDL_SYS_KillThread(thread);
   307 		SDL_WaitThread(thread, NULL);
   308 	}
   309 }
   310