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