src/thread/pth/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 252 e8157fcb3114
child 321 af42e7c4c860
permissions -rw-r--r--
Updated copyright information for 2002
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* Pth thread management routines for SDL */
    29 
    30 #include "SDL_error.h"
    31 #include "SDL_thread.h"
    32 #include "SDL_systhread.h"
    33 
    34 #include <signal.h>
    35 #include <pth.h>
    36 
    37 /* List of signals to mask in the subthreads */
    38 static int sig_list[] = {
    39 	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    40 	SIGVTALRM, SIGPROF, 0
    41 };
    42 
    43 static void *RunThread(void *data)
    44 {
    45 	SDL_RunThread(data);
    46 	pth_exit((void*)0);
    47 	return((void *)0);		/* Prevent compiler warning */
    48 }
    49 
    50 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
    51 {
    52 	pth_attr_t type;
    53 
    54 	/* Set the thread attributes */
    55 	if ( pth_attr_init(&type) != 0 ) {
    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 	if ( pth_spawn( &type, RunThread, args) != 0 ) {
    63 		SDL_SetError("Not enough resources to create thread");
    64 		return(-1);
    65 	}
    66 	return(0);
    67 }
    68 
    69 void SDL_SYS_SetupThread(void)
    70 {
    71 	int i;
    72 	sigset_t mask;
    73 
    74 	/* Mask asynchronous signals for this thread */
    75 	sigemptyset(&mask);
    76 	for ( i=0; sig_list[i]; ++i ) {
    77 		sigaddset(&mask, sig_list[i]);
    78 	}
    79 	pth_sigmask(SIG_BLOCK, &mask, 0);
    80 
    81 	/* Allow ourselves to be asynchronously cancelled */
    82 	{ int oldstate;
    83 		pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
    84 	}
    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 }