src/thread/pth/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Sep 2001 04:34:15 +0000
changeset 185 34d316d5e744
child 252 e8157fcb3114
permissions -rw-r--r--
Added support for the GNU Pth thread lib (thanks Mandin!)
slouken@185
     1
/*
slouken@185
     2
    SDL - Simple DirectMedia Layer
slouken@185
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@185
     4
slouken@185
     5
    This library is free software; you can redistribute it and/or
slouken@185
     6
    modify it under the terms of the GNU Library General Public
slouken@185
     7
    License as published by the Free Software Foundation; either
slouken@185
     8
    version 2 of the License, or (at your option) any later version.
slouken@185
     9
slouken@185
    10
    This library is distributed in the hope that it will be useful,
slouken@185
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@185
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@185
    13
    Library General Public License for more details.
slouken@185
    14
slouken@185
    15
    You should have received a copy of the GNU Library General Public
slouken@185
    16
    License along with this library; if not, write to the Free
slouken@185
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@185
    18
slouken@185
    19
    Sam Lantinga
slouken@185
    20
    slouken@devolution.com
slouken@185
    21
*/
slouken@185
    22
slouken@185
    23
#ifdef SAVE_RCSID
slouken@185
    24
static char rcsid =
slouken@185
    25
 "@(#) $Id$";
slouken@185
    26
#endif
slouken@185
    27
slouken@185
    28
/* Pth thread management routines for SDL */
slouken@185
    29
slouken@185
    30
#include "SDL_error.h"
slouken@185
    31
#include "SDL_thread.h"
slouken@185
    32
#include "SDL_systhread.h"
slouken@185
    33
slouken@185
    34
#include <signal.h>
slouken@185
    35
#include <pth.h>
slouken@185
    36
slouken@185
    37
/* List of signals to mask in the subthreads */
slouken@185
    38
static int sig_list[] = {
slouken@185
    39
	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@185
    40
	SIGVTALRM, SIGPROF, 0
slouken@185
    41
};
slouken@185
    42
slouken@185
    43
static void *RunThread(void *data)
slouken@185
    44
{
slouken@185
    45
	SDL_RunThread(data);
slouken@185
    46
	pth_exit((void*)0);
slouken@185
    47
	return((void *)0);		/* Prevent compiler warning */
slouken@185
    48
}
slouken@185
    49
slouken@185
    50
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
slouken@185
    51
{
slouken@185
    52
	pth_attr_t type;
slouken@185
    53
slouken@185
    54
	/* Set the thread attributes */
slouken@185
    55
	if ( pth_attr_init(&type) != 0 ) {
slouken@185
    56
		SDL_SetError("Couldn't initialize pth attributes");
slouken@185
    57
		return(-1);
slouken@185
    58
	}
slouken@185
    59
	pth_attr_set(&type, PTH_ATTR_JOINABLE, TRUE);
slouken@185
    60
slouken@185
    61
	/* Create the thread and go! */
slouken@185
    62
	if ( pth_spawn( &type, RunThread, args) != 0 ) {
slouken@185
    63
		SDL_SetError("Not enough resources to create thread");
slouken@185
    64
		return(-1);
slouken@185
    65
	}
slouken@185
    66
	return(0);
slouken@185
    67
}
slouken@185
    68
slouken@185
    69
void SDL_SYS_SetupThread(void)
slouken@185
    70
{
slouken@185
    71
	int i;
slouken@185
    72
	sigset_t mask;
slouken@185
    73
slouken@185
    74
	/* Mask asynchronous signals for this thread */
slouken@185
    75
	sigemptyset(&mask);
slouken@185
    76
	for ( i=0; sig_list[i]; ++i ) {
slouken@185
    77
		sigaddset(&mask, sig_list[i]);
slouken@185
    78
	}
slouken@185
    79
	pth_sigmask(SIG_BLOCK, &mask, 0);
slouken@185
    80
slouken@185
    81
	/* Allow ourselves to be asynchronously cancelled */
slouken@185
    82
	{ int oldstate;
slouken@185
    83
		pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@185
    84
	}
slouken@185
    85
}
slouken@185
    86
slouken@185
    87
/* WARNING:  This may not work for systems with 64-bit pid_t */
slouken@185
    88
Uint32 SDL_ThreadID(void)
slouken@185
    89
{
slouken@185
    90
	return((Uint32)pth_self());
slouken@185
    91
}
slouken@185
    92
slouken@185
    93
void SDL_SYS_WaitThread(SDL_Thread *thread)
slouken@185
    94
{
slouken@185
    95
	pth_join(thread->handle, 0);
slouken@185
    96
}
slouken@185
    97
slouken@185
    98
void SDL_SYS_KillThread(SDL_Thread *thread)
slouken@185
    99
{
slouken@185
   100
	pth_cancel(thread->handle);
slouken@185
   101
	pth_join(thread->handle, NULL);
slouken@185
   102
}