Mercurial > sdl-ios-xcode
annotate src/thread/linux/SDL_systhread.c @ 1300:fd068ab116ee
Removed some old Mac OS X cruft.
Fixes Bugzilla #64.
--ryan.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 30 Jan 2006 18:56:30 +0000 |
parents | b8d311d90021 |
children | c9b51268668f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 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 | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* Linux thread management routines for SDL */ | |
29 | |
30 #include "SDL_error.h" | |
31 #include "SDL_thread.h" | |
32 #include "SDL_systhread.h" | |
33 | |
34 #ifdef FORK_HACK | |
35 | |
36 #include <unistd.h> | |
37 | |
38 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
39 { | |
40 SDL_SetError("Threads are not supported on this platform"); | |
41 return(-1); | |
42 } | |
43 void SDL_SYS_SetupThread(void) | |
44 { | |
45 return; | |
46 } | |
47 Uint32 SDL_ThreadID(void) | |
48 { | |
49 return((Uint32)getpid()); | |
50 } | |
51 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
52 { | |
53 return; | |
54 } | |
55 void SDL_SYS_KillThread(SDL_Thread *thread) | |
56 { | |
57 return; | |
58 } | |
59 | |
60 #else | |
61 | |
62 #include <signal.h> | |
63 | |
64 /* List of signals to mask in the subthreads */ | |
65 static int sig_list[] = { | |
66 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH, | |
67 SIGVTALRM, SIGPROF, 0 | |
68 }; | |
69 | |
70 #ifdef SDL_USE_PTHREADS | |
71 | |
72 #include <pthread.h> | |
73 | |
74 | |
75 static void *RunThread(void *data) | |
76 { | |
77 SDL_RunThread(data); | |
78 pthread_exit((void*)0); | |
79 return((void *)0); /* Prevent compiler warning */ | |
80 } | |
81 | |
82 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
83 { | |
84 pthread_attr_t type; | |
85 | |
86 /* Set the thread attributes */ | |
87 if ( pthread_attr_init(&type) != 0 ) { | |
88 SDL_SetError("Couldn't initialize pthread attributes"); | |
89 return(-1); | |
90 } | |
91 pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE); | |
92 | |
93 /* Create the thread and go! */ | |
94 if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) { | |
95 SDL_SetError("Not enough resources to create thread"); | |
96 return(-1); | |
97 } | |
98 return(0); | |
99 } | |
100 | |
101 void SDL_SYS_SetupThread(void) | |
102 { | |
103 int i; | |
104 sigset_t mask; | |
105 | |
106 /* Mask asynchronous signals for this thread */ | |
107 sigemptyset(&mask); | |
108 for ( i=0; sig_list[i]; ++i ) { | |
109 sigaddset(&mask, sig_list[i]); | |
110 } | |
111 pthread_sigmask(SIG_BLOCK, &mask, 0); | |
112 | |
113 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS | |
114 /* Allow ourselves to be asynchronously cancelled */ | |
115 { int oldstate; | |
116 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); | |
117 } | |
118 #endif | |
119 } | |
120 | |
121 /* WARNING: This may not work for systems with 64-bit pid_t */ | |
122 Uint32 SDL_ThreadID(void) | |
123 { | |
124 return((Uint32)pthread_self()); | |
125 } | |
126 | |
127 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
128 { | |
129 pthread_join(thread->handle, 0); | |
130 } | |
131 | |
132 void SDL_SYS_KillThread(SDL_Thread *thread) | |
133 { | |
134 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS | |
135 pthread_cancel(thread->handle); | |
136 #else | |
137 #ifdef __FreeBSD__ | |
138 #warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2 | |
139 #endif | |
140 pthread_kill(thread->handle, SIGKILL); | |
141 #endif | |
142 } | |
143 | |
144 #else /* Linux-specific clone() based implementation */ | |
145 | |
146 #include <stdlib.h> | |
147 #include <errno.h> | |
148 #include <unistd.h> | |
149 #include <sys/wait.h> | |
150 | |
151 | |
152 /* Stack size for child thread */ | |
153 #define STACKSIZE 16384*4 /* 16384 is too small */ | |
154 | |
155 #ifdef __GLIBC__ | |
156 #include <sched.h> | |
157 #else | |
158 /* From <linux/sched.h> */ | |
159 #define CLONE_VM 0x00000100 /* set if VM shared */ | |
160 #define CLONE_FS 0x00000200 /* set if fs info shared */ | |
161 #define CLONE_FILES 0x00000400 /* set if open files shared */ | |
162 #define CLONE_SIGHAND 0x00000800 /* set if signal handlers shared */ | |
163 #define CLONE_PID 0x00001000 /* set if pid shared */ | |
164 | |
165 /* The infamous "start_thread" function, courtesy Linus Torvalds */ | |
166 extern int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); | |
167 #endif | |
168 | |
169 static int RunThread(void *data) | |
170 { | |
171 SDL_RunThread(data); | |
172 return(0); | |
173 } | |
174 | |
175 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
176 { | |
177 void *stack; | |
178 | |
179 /* Allocate memory for thread stack */ | |
180 stack = malloc(STACKSIZE); | |
181 if ( stack == (void *)0 ) { | |
182 SDL_OutOfMemory(); | |
183 return(-1); | |
184 } | |
185 thread->data = stack; | |
186 | |
187 /* Adjust the stack since it actually grows down */ | |
188 stack = (void *) ((char *)stack + STACKSIZE); | |
189 | |
190 /* Create the thread and go! */ | |
191 thread->handle = clone(RunThread, stack, | |
192 (CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND), args); | |
193 if ( thread->handle < 0 ) { | |
194 free(thread->data); | |
195 SDL_SetError("Not enough resources to create thread"); | |
196 return(-1); | |
197 } | |
198 return(0); | |
199 } | |
200 | |
201 void SDL_SYS_SetupThread(void) | |
202 { | |
203 int i; | |
204 sigset_t mask; | |
205 | |
206 /* Mask asynchronous signals for this thread */ | |
207 sigemptyset(&mask); | |
208 for ( i=0; sig_list[i]; ++i ) { | |
209 sigaddset(&mask, sig_list[i]); | |
210 } | |
211 sigprocmask(SIG_BLOCK, &mask, 0); | |
212 } | |
213 | |
214 /* WARNING: This may not work for systems with 64-bit pid_t */ | |
215 Uint32 SDL_ThreadID(void) | |
216 { | |
217 return((Uint32)getpid()); | |
218 } | |
219 | |
220 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
221 { | |
222 #ifdef __WCLONE | |
223 errno = 0; | |
224 while ( errno != ECHILD ) { | |
225 waitpid(thread->handle, 0, __WCLONE); | |
226 } | |
227 #else | |
228 /* Ack, ugly ugly hack -- | |
229 wait() doesn't work, waitpid() doesn't work, and ignoring SIG_CHLD | |
230 doesn't work .. and the child thread is still a zombie, so kill() | |
231 doesn't work. | |
232 */ | |
233 char command[1024]; | |
234 | |
235 sprintf(command, | |
236 "ps ax|fgrep -v fgrep|fgrep -v '<zombie>'|fgrep %d >/dev/null", | |
237 thread->handle); | |
238 while ( system(command) == 0 ) | |
239 sleep(1); | |
240 #endif | |
241 free(thread->data); | |
242 } | |
243 | |
244 void SDL_SYS_KillThread(SDL_Thread *thread) | |
245 { | |
246 kill(thread->handle, SIGKILL); | |
247 } | |
248 | |
249 #endif /* SDL_USE_PTHREADS */ | |
250 | |
251 #endif /* FORK_HACK */ |