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