src/thread/pthread/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
     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 #include <pthread.h>
    24 #include <signal.h>
    25 
    26 #include "SDL_thread.h"
    27 #include "../SDL_thread_c.h"
    28 #include "../SDL_systhread.h"
    29 
    30 /* List of signals to mask in the subthreads */
    31 static int sig_list[] = {
    32 	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    33 	SIGVTALRM, SIGPROF, 0
    34 };
    35 
    36 
    37 static void *RunThread(void *data)
    38 {
    39 	SDL_RunThread(data);
    40 	pthread_exit((void*)0);
    41 	return((void *)0);		/* Prevent compiler warning */
    42 }
    43 
    44 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
    45 {
    46 	pthread_attr_t type;
    47 
    48 	/* Set the thread attributes */
    49 	if ( pthread_attr_init(&type) != 0 ) {
    50 		SDL_SetError("Couldn't initialize pthread attributes");
    51 		return(-1);
    52 	}
    53 	pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
    54 
    55 	/* Create the thread and go! */
    56 	if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) {
    57 		SDL_SetError("Not enough resources to create thread");
    58 		return(-1);
    59 	}
    60 	return(0);
    61 }
    62 
    63 void SDL_SYS_SetupThread(void)
    64 {
    65 	int i;
    66 	sigset_t mask;
    67 
    68 	/* Mask asynchronous signals for this thread */
    69 	sigemptyset(&mask);
    70 	for ( i=0; sig_list[i]; ++i ) {
    71 		sigaddset(&mask, sig_list[i]);
    72 	}
    73 	pthread_sigmask(SIG_BLOCK, &mask, 0);
    74 
    75 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
    76 	/* Allow ourselves to be asynchronously cancelled */
    77 	{ int oldstate;
    78 		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
    79 	}
    80 #endif
    81 }
    82 
    83 /* WARNING:  This may not work for systems with 64-bit pid_t */
    84 Uint32 SDL_ThreadID(void)
    85 {
    86 	return((Uint32)pthread_self());
    87 }
    88 
    89 void SDL_SYS_WaitThread(SDL_Thread *thread)
    90 {
    91 	pthread_join(thread->handle, 0);
    92 }
    93 
    94 void SDL_SYS_KillThread(SDL_Thread *thread)
    95 {
    96 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
    97 	pthread_cancel(thread->handle);
    98 #else
    99 #ifdef __FreeBSD__
   100 #warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2
   101 #endif
   102 	pthread_kill(thread->handle, SIGKILL);
   103 #endif
   104 }