19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* System independent thread management routines for SDL */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mutex.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_systhread.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ARRAY_CHUNKSIZE	32
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The array of threads currently active in the application
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   (except the main thread)
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The manipulation of an array here is safer than using a linked list.
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_maxthreads = 0;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_numthreads = 0;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Thread **SDL_Threads = NULL;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_mutex *thread_lock = NULL;
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_ThreadsInit(void)
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread_lock = SDL_CreateMutex();
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( thread_lock == NULL ) {
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This should never be called...
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   If this is called by SDL_Quit(), we don't know whether or not we should
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   clean up threads here.  If any threads are still running after this call,
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   they will no longer have access to any per-thread data.
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_ThreadsQuit(void)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutex *mutex;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mutex = thread_lock;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread_lock = NULL;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mutex != NULL ) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_DestroyMutex(mutex);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Routines for manipulating the thread list */
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_AddThread(SDL_Thread *thread)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* WARNING:
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   If the very first threads are created simultaneously, then
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   there could be a race condition causing memory corruption.
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   In practice, this isn't a problem because by definition there
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   is only one thread running the first time this is called.
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !thread_lock ) {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_ThreadsInit() < 0 ) {
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexP(thread_lock);
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Expand the list of threads, if necessary */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_THREADS
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Adding thread (%d already - %d max)\n",
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_numthreads, SDL_maxthreads);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_numthreads == SDL_maxthreads ) {
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Thread **threads;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		threads = (SDL_Thread **)SDL_realloc(SDL_Threads,
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(SDL_maxthreads+ARRAY_CHUNKSIZE)*(sizeof *threads));
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( threads == NULL ) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			goto done;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_maxthreads += ARRAY_CHUNKSIZE;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Threads = threads;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Threads[SDL_numthreads++] = thread;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldone:
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexV(thread_lock);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_DelThread(SDL_Thread *thread)
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !thread_lock ) {
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexP(thread_lock);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_numthreads; ++i ) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( thread == SDL_Threads[i] ) {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( i < SDL_numthreads ) {
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( --SDL_numthreads > 0 ) {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while ( i < SDL_numthreads ) {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Threads[i] = SDL_Threads[i+1];
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++i;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_maxthreads = 0;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(SDL_Threads);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Threads = NULL;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_THREADS
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		printf("Deleting thread (%d left - %d max)\n",
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_numthreads, SDL_maxthreads);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexV(thread_lock);
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0	/* There could be memory corruption if another thread is starting */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Threads == NULL ) {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_ThreadsQuit();
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The default (non-thread-safe) global error variable */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_error SDL_global_error;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Routine to get the thread-specific error variable */
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_error *SDL_GetErrBuf(void)
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_error *errbuf;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	errbuf = &SDL_global_error;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Threads ) {
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int i;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint32 this_thread;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this_thread = SDL_ThreadID();
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexP(thread_lock);
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<SDL_numthreads; ++i ) {
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( this_thread == SDL_Threads[i]->threadid ) {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				errbuf = &SDL_Threads[i]->errbuf;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexV(thread_lock);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(errbuf);
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Arguments and callback to setup and run the user thread function */
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct {
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int (SDLCALL *func)(void *);
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *data;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Thread *info;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_sem *wait;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} thread_args;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_RunThread(void *data)
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread_args *args;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int (SDLCALL *userfunc)(void *);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *userdata;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int *statusloc;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Perform any system-dependent setup
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   - this function cannot fail, and cannot use SDL_SetError()
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SYS_SetupThread();
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the thread id */
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	args = (thread_args *)data;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	args->info->threadid = SDL_ThreadID();
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Figure out what function to run */
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	userfunc = args->func;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	userdata = args->data;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	statusloc = &args->info->status;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wake up the parent thread */
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SemPost(args->wait);
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Run the function */
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*statusloc = userfunc(userdata);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#undef SDL_CreateThread
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data)
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Thread *thread;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread_args *args;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int ret;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate memory for the thread info structure */
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread = (SDL_Thread *)SDL_malloc(sizeof(*thread));
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( thread == NULL ) {
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(thread, 0, (sizeof *thread));
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread->status = -1;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the arguments for the thread */
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	args = (thread_args *)SDL_malloc(sizeof(*args));
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( args == NULL ) {
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(thread);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	args->func = fn;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	args->data = data;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	args->info = thread;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	args->wait = SDL_CreateSemaphore(0);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( args->wait == NULL ) {
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(thread);
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(args);
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Add the thread to the list of available threads */
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AddThread(thread);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the thread and go! */
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ret = SDL_SYS_CreateThread(thread, args);
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ret >= 0 ) {
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Wait for the thread function to use arguments */
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SemWait(args->wait);
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Oops, failed.  Gotta free everything */
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_DelThread(thread);
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(thread);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		thread = NULL;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DestroySemaphore(args->wait);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(args);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Everything is running now */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(thread);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_WaitThread(SDL_Thread *thread, int *status)
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( thread ) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SYS_WaitThread(thread);
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( status ) {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*status = thread->status;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_DelThread(thread);
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(thread);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_GetThreadID(SDL_Thread *thread)
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 id;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( thread ) {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		id = thread->threadid;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		id = SDL_ThreadID();
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(id);
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_KillThread(SDL_Thread *thread)
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( thread ) {
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SYS_KillThread(thread);
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WaitThread(thread, NULL);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
301