src/thread/pth/SDL_systhread.c
changeset 185 34d316d5e744
child 252 e8157fcb3114
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/thread/pth/SDL_systhread.c	Fri Sep 14 04:34:15 2001 +0000
     1.3 @@ -0,0 +1,102 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  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 Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 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 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Pth thread management routines for SDL */
    1.32 +
    1.33 +#include "SDL_error.h"
    1.34 +#include "SDL_thread.h"
    1.35 +#include "SDL_systhread.h"
    1.36 +
    1.37 +#include <signal.h>
    1.38 +#include <pth.h>
    1.39 +
    1.40 +/* List of signals to mask in the subthreads */
    1.41 +static int sig_list[] = {
    1.42 +	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    1.43 +	SIGVTALRM, SIGPROF, 0
    1.44 +};
    1.45 +
    1.46 +static void *RunThread(void *data)
    1.47 +{
    1.48 +	SDL_RunThread(data);
    1.49 +	pth_exit((void*)0);
    1.50 +	return((void *)0);		/* Prevent compiler warning */
    1.51 +}
    1.52 +
    1.53 +int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
    1.54 +{
    1.55 +	pth_attr_t type;
    1.56 +
    1.57 +	/* Set the thread attributes */
    1.58 +	if ( pth_attr_init(&type) != 0 ) {
    1.59 +		SDL_SetError("Couldn't initialize pth attributes");
    1.60 +		return(-1);
    1.61 +	}
    1.62 +	pth_attr_set(&type, PTH_ATTR_JOINABLE, TRUE);
    1.63 +
    1.64 +	/* Create the thread and go! */
    1.65 +	if ( pth_spawn( &type, RunThread, args) != 0 ) {
    1.66 +		SDL_SetError("Not enough resources to create thread");
    1.67 +		return(-1);
    1.68 +	}
    1.69 +	return(0);
    1.70 +}
    1.71 +
    1.72 +void SDL_SYS_SetupThread(void)
    1.73 +{
    1.74 +	int i;
    1.75 +	sigset_t mask;
    1.76 +
    1.77 +	/* Mask asynchronous signals for this thread */
    1.78 +	sigemptyset(&mask);
    1.79 +	for ( i=0; sig_list[i]; ++i ) {
    1.80 +		sigaddset(&mask, sig_list[i]);
    1.81 +	}
    1.82 +	pth_sigmask(SIG_BLOCK, &mask, 0);
    1.83 +
    1.84 +	/* Allow ourselves to be asynchronously cancelled */
    1.85 +	{ int oldstate;
    1.86 +		pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
    1.87 +	}
    1.88 +}
    1.89 +
    1.90 +/* WARNING:  This may not work for systems with 64-bit pid_t */
    1.91 +Uint32 SDL_ThreadID(void)
    1.92 +{
    1.93 +	return((Uint32)pth_self());
    1.94 +}
    1.95 +
    1.96 +void SDL_SYS_WaitThread(SDL_Thread *thread)
    1.97 +{
    1.98 +	pth_join(thread->handle, 0);
    1.99 +}
   1.100 +
   1.101 +void SDL_SYS_KillThread(SDL_Thread *thread)
   1.102 +{
   1.103 +	pth_cancel(thread->handle);
   1.104 +	pth_join(thread->handle, NULL);
   1.105 +}