19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pthread.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_thread_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_systhread.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* List of signals to mask in the subthreads */
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int sig_list[] = {
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SIGVTALRM, SIGPROF, 0
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __RISCOS__
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* RISC OS needs to know the main thread for
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * it's timer and event processing. */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint riscos_using_threads = 0;
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 riscos_main_thread = 0; /* Thread running events */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void *RunThread(void *data)
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RunThread(data);
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_exit((void*)0);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return((void *)0);		/* Prevent compiler warning */
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_attr_t type;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the thread attributes */
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_attr_init(&type) != 0 ) {
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't initialize pthread attributes");
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the thread and go! */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Not enough resources to create thread");
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __RISCOS__
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (riscos_using_threads == 0) {
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		riscos_using_threads = 1;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		riscos_main_thread = SDL_ThreadID();
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_SetupThread(void)
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sigset_t mask;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mask asynchronous signals for this thread */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sigemptyset(&mask);
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; sig_list[i]; ++i ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sigaddset(&mask, sig_list[i]);
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_sigmask(SIG_BLOCK, &mask, 0);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allow ourselves to be asynchronously cancelled */
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ int oldstate;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* WARNING:  This may not work for systems with 64-bit pid_t */
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_ThreadID(void)
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return((Uint32)((size_t)pthread_self()));
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_WaitThread(SDL_Thread *thread)
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_join(thread->handle, 0);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_KillThread(SDL_Thread *thread)
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_cancel(thread->handle);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __FREEBSD__
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pthread_kill(thread->handle, SIGKILL);
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
121