src/thread/pth/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 769 b8d311d90021
child 1358 c71e05b4dc2e
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
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_error.h"
slouken@185
    30
#include "SDL_thread.h"
slouken@185
    31
#include "SDL_systhread.h"
slouken@185
    32
slouken@185
    33
#include <signal.h>
slouken@185
    34
#include <pth.h>
slouken@185
    35
slouken@185
    36
/* List of signals to mask in the subthreads */
slouken@185
    37
static int sig_list[] = {
slouken@185
    38
	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@185
    39
	SIGVTALRM, SIGPROF, 0
slouken@185
    40
};
slouken@185
    41
slouken@185
    42
static void *RunThread(void *data)
slouken@185
    43
{
slouken@185
    44
	SDL_RunThread(data);
slouken@185
    45
	pth_exit((void*)0);
slouken@185
    46
	return((void *)0);		/* Prevent compiler warning */
slouken@185
    47
}
slouken@185
    48
slouken@185
    49
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
slouken@185
    50
{
slouken@185
    51
	pth_attr_t type;
slouken@185
    52
slouken@329
    53
	/* Create a new attribute */
slouken@321
    54
	type = pth_attr_new();
slouken@329
    55
	if ( type == NULL ) {
slouken@185
    56
		SDL_SetError("Couldn't initialize pth attributes");
slouken@185
    57
		return(-1);
slouken@185
    58
	}
slouken@321
    59
	pth_attr_set(type, PTH_ATTR_JOINABLE, TRUE);
slouken@185
    60
slouken@185
    61
	/* Create the thread and go! */
slouken@397
    62
	thread->handle = pth_spawn(type, RunThread, args);
slouken@397
    63
	if ( thread->handle == NULL ) {
slouken@185
    64
		SDL_SetError("Not enough resources to create thread");
slouken@185
    65
		return(-1);
slouken@185
    66
	}
slouken@185
    67
	return(0);
slouken@185
    68
}
slouken@185
    69
slouken@185
    70
void SDL_SYS_SetupThread(void)
slouken@185
    71
{
slouken@185
    72
	int i;
slouken@185
    73
	sigset_t mask;
slouken@329
    74
	int oldstate;
slouken@185
    75
slouken@185
    76
	/* Mask asynchronous signals for this thread */
slouken@185
    77
	sigemptyset(&mask);
slouken@185
    78
	for ( i=0; sig_list[i]; ++i ) {
slouken@185
    79
		sigaddset(&mask, sig_list[i]);
slouken@185
    80
	}
slouken@185
    81
	pth_sigmask(SIG_BLOCK, &mask, 0);
slouken@185
    82
slouken@185
    83
	/* Allow ourselves to be asynchronously cancelled */
slouken@329
    84
	pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
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
}