src/thread/SDL_thread.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 23 Nov 2005 07:29:56 +0000
changeset 1190 173c063d4f55
parent 769 b8d311d90021
child 1312 c9b51268668f
permissions -rw-r--r--
OS/2 port!

This was mostly, if not entirely, written by "Doodle" and "Caetano":
doodle@scenergy.dfmk.hu
daniel@caetano.eng.br

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