• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
25  *	GNU pth threads
26  *
27  *	Patrice Mandin
28  */
29 
30 #include <pth.h>
31 #include <signal.h>
32 
33 #include "SDL_thread.h"
34 #include "../SDL_thread_c.h"
35 #include "../SDL_systhread.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 
RunThread(void * data)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 
SDL_SYS_CreateThread(SDL_Thread * thread,void * args)50 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
51 {
52 	pth_attr_t type;
53 
54 	/* Create a new attribute */
55 	type = pth_attr_new();
56 	if ( type == NULL ) {
57 		SDL_SetError("Couldn't initialize pth attributes");
58 		return(-1);
59 	}
60 	pth_attr_set(type, PTH_ATTR_JOINABLE, TRUE);
61 
62 	/* Create the thread and go! */
63 	thread->handle = pth_spawn(type, RunThread, args);
64 	if ( thread->handle == NULL ) {
65 		SDL_SetError("Not enough resources to create thread");
66 		return(-1);
67 	}
68 	return(0);
69 }
70 
SDL_SYS_SetupThread(void)71 void SDL_SYS_SetupThread(void)
72 {
73 	int i;
74 	sigset_t mask;
75 	int oldstate;
76 
77 	/* Mask asynchronous signals for this thread */
78 	sigemptyset(&mask);
79 	for ( i=0; sig_list[i]; ++i ) {
80 		sigaddset(&mask, sig_list[i]);
81 	}
82 	pth_sigmask(SIG_BLOCK, &mask, 0);
83 
84 	/* Allow ourselves to be asynchronously cancelled */
85 	pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
86 }
87 
88 /* WARNING:  This may not work for systems with 64-bit pid_t */
SDL_ThreadID(void)89 Uint32 SDL_ThreadID(void)
90 {
91 	return((Uint32)pth_self());
92 }
93 
SDL_SYS_WaitThread(SDL_Thread * thread)94 void SDL_SYS_WaitThread(SDL_Thread *thread)
95 {
96 	pth_join(thread->handle, NULL);
97 }
98 
SDL_SYS_KillThread(SDL_Thread * thread)99 void SDL_SYS_KillThread(SDL_Thread *thread)
100 {
101 	pth_cancel(thread->handle);
102 	pth_join(thread->handle, NULL);
103 }
104