Mercurial > sdl-ios-xcode
annotate src/thread/linux/SDL_systhread.c @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | c9b51268668f |
children | 604d73db6802 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1300
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1300
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1300
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1300
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1300
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1300
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1300
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 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 /* Linux thread management routines for SDL */ | |
24 | |
25 #include "SDL_error.h" | |
26 #include "SDL_thread.h" | |
27 #include "SDL_systhread.h" | |
28 | |
29 #ifdef FORK_HACK | |
30 | |
31 #include <unistd.h> | |
32 | |
33 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
34 { | |
35 SDL_SetError("Threads are not supported on this platform"); | |
36 return(-1); | |
37 } | |
38 void SDL_SYS_SetupThread(void) | |
39 { | |
40 return; | |
41 } | |
42 Uint32 SDL_ThreadID(void) | |
43 { | |
44 return((Uint32)getpid()); | |
45 } | |
46 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
47 { | |
48 return; | |
49 } | |
50 void SDL_SYS_KillThread(SDL_Thread *thread) | |
51 { | |
52 return; | |
53 } | |
54 | |
55 #else | |
56 | |
57 #include <signal.h> | |
58 | |
59 /* List of signals to mask in the subthreads */ | |
60 static int sig_list[] = { | |
61 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH, | |
62 SIGVTALRM, SIGPROF, 0 | |
63 }; | |
64 | |
65 #ifdef SDL_USE_PTHREADS | |
66 | |
67 #include <pthread.h> | |
68 | |
69 | |
70 static void *RunThread(void *data) | |
71 { | |
72 SDL_RunThread(data); | |
73 pthread_exit((void*)0); | |
74 return((void *)0); /* Prevent compiler warning */ | |
75 } | |
76 | |
77 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
78 { | |
79 pthread_attr_t type; | |
80 | |
81 /* Set the thread attributes */ | |
82 if ( pthread_attr_init(&type) != 0 ) { | |
83 SDL_SetError("Couldn't initialize pthread attributes"); | |
84 return(-1); | |
85 } | |
86 pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE); | |
87 | |
88 /* Create the thread and go! */ | |
89 if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) { | |
90 SDL_SetError("Not enough resources to create thread"); | |
91 return(-1); | |
92 } | |
93 return(0); | |
94 } | |
95 | |
96 void SDL_SYS_SetupThread(void) | |
97 { | |
98 int i; | |
99 sigset_t mask; | |
100 | |
101 /* Mask asynchronous signals for this thread */ | |
102 sigemptyset(&mask); | |
103 for ( i=0; sig_list[i]; ++i ) { | |
104 sigaddset(&mask, sig_list[i]); | |
105 } | |
106 pthread_sigmask(SIG_BLOCK, &mask, 0); | |
107 | |
108 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS | |
109 /* Allow ourselves to be asynchronously cancelled */ | |
110 { int oldstate; | |
111 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); | |
112 } | |
113 #endif | |
114 } | |
115 | |
116 /* WARNING: This may not work for systems with 64-bit pid_t */ | |
117 Uint32 SDL_ThreadID(void) | |
118 { | |
119 return((Uint32)pthread_self()); | |
120 } | |
121 | |
122 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
123 { | |
124 pthread_join(thread->handle, 0); | |
125 } | |
126 | |
127 void SDL_SYS_KillThread(SDL_Thread *thread) | |
128 { | |
129 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS | |
130 pthread_cancel(thread->handle); | |
131 #else | |
132 #ifdef __FreeBSD__ | |
133 #warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2 | |
134 #endif | |
135 pthread_kill(thread->handle, SIGKILL); | |
136 #endif | |
137 } | |
138 | |
139 #else /* Linux-specific clone() based implementation */ | |
140 | |
141 #include <stdlib.h> | |
142 #include <errno.h> | |
143 #include <unistd.h> | |
144 #include <sys/wait.h> | |
145 | |
146 | |
147 /* Stack size for child thread */ | |
148 #define STACKSIZE 16384*4 /* 16384 is too small */ | |
149 | |
150 #ifdef __GLIBC__ | |
151 #include <sched.h> | |
152 #else | |
153 /* From <linux/sched.h> */ | |
154 #define CLONE_VM 0x00000100 /* set if VM shared */ | |
155 #define CLONE_FS 0x00000200 /* set if fs info shared */ | |
156 #define CLONE_FILES 0x00000400 /* set if open files shared */ | |
157 #define CLONE_SIGHAND 0x00000800 /* set if signal handlers shared */ | |
158 #define CLONE_PID 0x00001000 /* set if pid shared */ | |
159 | |
160 /* The infamous "start_thread" function, courtesy Linus Torvalds */ | |
161 extern int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); | |
162 #endif | |
163 | |
164 static int RunThread(void *data) | |
165 { | |
166 SDL_RunThread(data); | |
167 return(0); | |
168 } | |
169 | |
170 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
171 { | |
172 void *stack; | |
173 | |
174 /* Allocate memory for thread stack */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
175 stack = SDL_malloc(STACKSIZE); |
0 | 176 if ( stack == (void *)0 ) { |
177 SDL_OutOfMemory(); | |
178 return(-1); | |
179 } | |
180 thread->data = stack; | |
181 | |
182 /* Adjust the stack since it actually grows down */ | |
183 stack = (void *) ((char *)stack + STACKSIZE); | |
184 | |
185 /* Create the thread and go! */ | |
186 thread->handle = clone(RunThread, stack, | |
187 (CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND), args); | |
188 if ( thread->handle < 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
189 SDL_free(thread->data); |
0 | 190 SDL_SetError("Not enough resources to create thread"); |
191 return(-1); | |
192 } | |
193 return(0); | |
194 } | |
195 | |
196 void SDL_SYS_SetupThread(void) | |
197 { | |
198 int i; | |
199 sigset_t mask; | |
200 | |
201 /* Mask asynchronous signals for this thread */ | |
202 sigemptyset(&mask); | |
203 for ( i=0; sig_list[i]; ++i ) { | |
204 sigaddset(&mask, sig_list[i]); | |
205 } | |
206 sigprocmask(SIG_BLOCK, &mask, 0); | |
207 } | |
208 | |
209 /* WARNING: This may not work for systems with 64-bit pid_t */ | |
210 Uint32 SDL_ThreadID(void) | |
211 { | |
212 return((Uint32)getpid()); | |
213 } | |
214 | |
215 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
216 { | |
217 #ifdef __WCLONE | |
218 errno = 0; | |
219 while ( errno != ECHILD ) { | |
220 waitpid(thread->handle, 0, __WCLONE); | |
221 } | |
222 #else | |
223 /* Ack, ugly ugly hack -- | |
224 wait() doesn't work, waitpid() doesn't work, and ignoring SIG_CHLD | |
225 doesn't work .. and the child thread is still a zombie, so kill() | |
226 doesn't work. | |
227 */ | |
228 char command[1024]; | |
229 | |
230 sprintf(command, | |
231 "ps ax|fgrep -v fgrep|fgrep -v '<zombie>'|fgrep %d >/dev/null", | |
232 thread->handle); | |
233 while ( system(command) == 0 ) | |
234 sleep(1); | |
235 #endif | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
236 SDL_free(thread->data); |
0 | 237 } |
238 | |
239 void SDL_SYS_KillThread(SDL_Thread *thread) | |
240 { | |
241 kill(thread->handle, SIGKILL); | |
242 } | |
243 | |
244 #endif /* SDL_USE_PTHREADS */ | |
245 | |
246 #endif /* FORK_HACK */ |