src/thread/pthread/SDL_systhread.c
changeset 1361 19418e4422cb
child 1402 d910939febfa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/thread/pthread/SDL_systhread.c	Thu Feb 16 10:11:48 2006 +0000
     1.3 @@ -0,0 +1,104 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#include <pthread.h>
    1.27 +#include <signal.h>
    1.28 +
    1.29 +#include "SDL_thread.h"
    1.30 +#include "../SDL_thread_c.h"
    1.31 +#include "../SDL_systhread.h"
    1.32 +
    1.33 +/* List of signals to mask in the subthreads */
    1.34 +static int sig_list[] = {
    1.35 +	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    1.36 +	SIGVTALRM, SIGPROF, 0
    1.37 +};
    1.38 +
    1.39 +
    1.40 +static void *RunThread(void *data)
    1.41 +{
    1.42 +	SDL_RunThread(data);
    1.43 +	pthread_exit((void*)0);
    1.44 +	return((void *)0);		/* Prevent compiler warning */
    1.45 +}
    1.46 +
    1.47 +int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
    1.48 +{
    1.49 +	pthread_attr_t type;
    1.50 +
    1.51 +	/* Set the thread attributes */
    1.52 +	if ( pthread_attr_init(&type) != 0 ) {
    1.53 +		SDL_SetError("Couldn't initialize pthread attributes");
    1.54 +		return(-1);
    1.55 +	}
    1.56 +	pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
    1.57 +
    1.58 +	/* Create the thread and go! */
    1.59 +	if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) {
    1.60 +		SDL_SetError("Not enough resources to create thread");
    1.61 +		return(-1);
    1.62 +	}
    1.63 +	return(0);
    1.64 +}
    1.65 +
    1.66 +void SDL_SYS_SetupThread(void)
    1.67 +{
    1.68 +	int i;
    1.69 +	sigset_t mask;
    1.70 +
    1.71 +	/* Mask asynchronous signals for this thread */
    1.72 +	sigemptyset(&mask);
    1.73 +	for ( i=0; sig_list[i]; ++i ) {
    1.74 +		sigaddset(&mask, sig_list[i]);
    1.75 +	}
    1.76 +	pthread_sigmask(SIG_BLOCK, &mask, 0);
    1.77 +
    1.78 +#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
    1.79 +	/* Allow ourselves to be asynchronously cancelled */
    1.80 +	{ int oldstate;
    1.81 +		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
    1.82 +	}
    1.83 +#endif
    1.84 +}
    1.85 +
    1.86 +/* WARNING:  This may not work for systems with 64-bit pid_t */
    1.87 +Uint32 SDL_ThreadID(void)
    1.88 +{
    1.89 +	return((Uint32)pthread_self());
    1.90 +}
    1.91 +
    1.92 +void SDL_SYS_WaitThread(SDL_Thread *thread)
    1.93 +{
    1.94 +	pthread_join(thread->handle, 0);
    1.95 +}
    1.96 +
    1.97 +void SDL_SYS_KillThread(SDL_Thread *thread)
    1.98 +{
    1.99 +#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   1.100 +	pthread_cancel(thread->handle);
   1.101 +#else
   1.102 +#ifdef __FreeBSD__
   1.103 +#warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2
   1.104 +#endif
   1.105 +	pthread_kill(thread->handle, SIGKILL);
   1.106 +#endif
   1.107 +}