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