src/thread/pth/SDL_systhread.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@185
     1
/*
slouken@185
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@185
     4
slouken@185
     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@185
     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@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@1312
    13
    Lesser General Public License for more details.
slouken@185
    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@185
    18
slouken@185
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@185
    21
*/
slouken@185
    22
slouken@329
    23
/*
slouken@329
    24
 *	GNU pth threads
slouken@329
    25
 *
slouken@329
    26
 *	Patrice Mandin
slouken@329
    27
 */
slouken@185
    28
slouken@185
    29
#include "SDL_thread.h"
slouken@1361
    30
#include "../SDL_systhread.h"
slouken@185
    31
slouken@1361
    32
#include <pth.h>
slouken@185
    33
#include <signal.h>
slouken@185
    34
slouken@185
    35
/* List of signals to mask in the subthreads */
slouken@185
    36
static int sig_list[] = {
slouken@185
    37
	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@185
    38
	SIGVTALRM, SIGPROF, 0
slouken@185
    39
};
slouken@185
    40
slouken@185
    41
static void *RunThread(void *data)
slouken@185
    42
{
slouken@185
    43
	SDL_RunThread(data);
slouken@185
    44
	pth_exit((void*)0);
slouken@185
    45
	return((void *)0);		/* Prevent compiler warning */
slouken@185
    46
}
slouken@185
    47
slouken@185
    48
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
slouken@185
    49
{
slouken@185
    50
	pth_attr_t type;
slouken@185
    51
slouken@329
    52
	/* Create a new attribute */
slouken@321
    53
	type = pth_attr_new();
slouken@329
    54
	if ( type == NULL ) {
slouken@185
    55
		SDL_SetError("Couldn't initialize pth attributes");
slouken@185
    56
		return(-1);
slouken@185
    57
	}
slouken@321
    58
	pth_attr_set(type, PTH_ATTR_JOINABLE, TRUE);
slouken@185
    59
slouken@185
    60
	/* Create the thread and go! */
slouken@397
    61
	thread->handle = pth_spawn(type, RunThread, args);
slouken@397
    62
	if ( thread->handle == NULL ) {
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@329
    73
	int oldstate;
slouken@185
    74
slouken@185
    75
	/* Mask asynchronous signals for this thread */
slouken@185
    76
	sigemptyset(&mask);
slouken@185
    77
	for ( i=0; sig_list[i]; ++i ) {
slouken@185
    78
		sigaddset(&mask, sig_list[i]);
slouken@185
    79
	}
slouken@185
    80
	pth_sigmask(SIG_BLOCK, &mask, 0);
slouken@185
    81
slouken@185
    82
	/* Allow ourselves to be asynchronously cancelled */
slouken@329
    83
	pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@185
    84
}
slouken@185
    85
slouken@185
    86
/* WARNING:  This may not work for systems with 64-bit pid_t */
slouken@185
    87
Uint32 SDL_ThreadID(void)
slouken@185
    88
{
slouken@185
    89
	return((Uint32)pth_self());
slouken@185
    90
}
slouken@185
    91
slouken@185
    92
void SDL_SYS_WaitThread(SDL_Thread *thread)
slouken@185
    93
{
slouken@1361
    94
	pth_join(thread->handle, NULL);
slouken@185
    95
}
slouken@185
    96
slouken@185
    97
void SDL_SYS_KillThread(SDL_Thread *thread)
slouken@185
    98
{
slouken@185
    99
	pth_cancel(thread->handle);
slouken@185
   100
	pth_join(thread->handle, NULL);
slouken@185
   101
}