Mercurial > sdl-ios-xcode
annotate src/thread/linux/SDL_systhread.c @ 1340:58b114ef50e7
Faster fades when changing to/from/between fullscreen modes on Mac OS X. Also,
it doesn't show the desktop between modes anymore.
Fixes Bugzilla #100.
--ryan.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 07 Feb 2006 11:18:21 +0000 |
parents | 604d73db6802 |
children | c71e05b4dc2e |
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 | |
1338
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
25 #include "SDL_stdlib.h" |
0 | 26 #include "SDL_error.h" |
27 #include "SDL_thread.h" | |
28 #include "SDL_systhread.h" | |
29 | |
30 #ifdef FORK_HACK | |
31 | |
32 #include <unistd.h> | |
33 | |
34 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
35 { | |
36 SDL_SetError("Threads are not supported on this platform"); | |
37 return(-1); | |
38 } | |
39 void SDL_SYS_SetupThread(void) | |
40 { | |
41 return; | |
42 } | |
43 Uint32 SDL_ThreadID(void) | |
44 { | |
45 return((Uint32)getpid()); | |
46 } | |
47 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
48 { | |
49 return; | |
50 } | |
51 void SDL_SYS_KillThread(SDL_Thread *thread) | |
52 { | |
53 return; | |
54 } | |
55 | |
56 #else | |
57 | |
58 #include <signal.h> | |
59 | |
60 /* List of signals to mask in the subthreads */ | |
61 static int sig_list[] = { | |
62 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH, | |
63 SIGVTALRM, SIGPROF, 0 | |
64 }; | |
65 | |
66 #ifdef SDL_USE_PTHREADS | |
67 | |
68 #include <pthread.h> | |
69 | |
70 | |
71 static void *RunThread(void *data) | |
72 { | |
73 SDL_RunThread(data); | |
74 pthread_exit((void*)0); | |
75 return((void *)0); /* Prevent compiler warning */ | |
76 } | |
77 | |
78 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) | |
79 { | |
80 pthread_attr_t type; | |
81 | |
82 /* Set the thread attributes */ | |
83 if ( pthread_attr_init(&type) != 0 ) { | |
84 SDL_SetError("Couldn't initialize pthread attributes"); | |
85 return(-1); | |
86 } | |
87 pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE); | |
88 | |
89 /* Create the thread and go! */ | |
90 if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) { | |
91 SDL_SetError("Not enough resources to create thread"); | |
92 return(-1); | |
93 } | |
94 return(0); | |
95 } | |
96 | |
97 void SDL_SYS_SetupThread(void) | |
98 { | |
99 int i; | |
100 sigset_t mask; | |
101 | |
102 /* Mask asynchronous signals for this thread */ | |
103 sigemptyset(&mask); | |
104 for ( i=0; sig_list[i]; ++i ) { | |
105 sigaddset(&mask, sig_list[i]); | |
106 } | |
107 pthread_sigmask(SIG_BLOCK, &mask, 0); | |
108 | |
109 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS | |
110 /* Allow ourselves to be asynchronously cancelled */ | |
111 { int oldstate; | |
112 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); | |
113 } | |
114 #endif | |
115 } | |
116 | |
117 /* WARNING: This may not work for systems with 64-bit pid_t */ | |
118 Uint32 SDL_ThreadID(void) | |
119 { | |
120 return((Uint32)pthread_self()); | |
121 } | |
122 | |
123 void SDL_SYS_WaitThread(SDL_Thread *thread) | |
124 { | |
125 pthread_join(thread->handle, 0); | |
126 } | |
127 | |
128 void SDL_SYS_KillThread(SDL_Thread *thread) | |
129 { | |
130 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS | |
131 pthread_cancel(thread->handle); | |
132 #else | |
133 #ifdef __FreeBSD__ | |
134 #warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2 | |
135 #endif | |
136 pthread_kill(thread->handle, SIGKILL); | |
137 #endif | |
138 } | |
139 | |
140 #else /* Linux-specific clone() based implementation */ | |
141 | |
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 | |
1338
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
230 SDL_snprintf(command, SDL_arraysize(command), |
0 | 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 */ |