Mercurial > sdl-ios-xcode
comparison test/testhread.c @ 1151:be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
if SDL is built with a non-cdecl calling convention, and it's just generally
bad practice anyhow.
Now programs explicitly call SDL_Quit() where appropriate, wrap SDL_Quit() in
a cdecl function where it can't be avoided, and rely on the parachute where
a crash might have hit the atexit() before (these ARE test programs, after
all!).
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 28 Sep 2005 11:36:20 +0000 |
parents | 74212992fb08 |
children | 14717b52abc0 |
comparison
equal
deleted
inserted
replaced
1150:7d8e1925f35b | 1151:be9c9c8f6d53 |
---|---|
7 | 7 |
8 #include "SDL.h" | 8 #include "SDL.h" |
9 #include "SDL_thread.h" | 9 #include "SDL_thread.h" |
10 | 10 |
11 static int alive = 0; | 11 static int alive = 0; |
12 | |
13 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | |
14 static void quit(int rc) | |
15 { | |
16 SDL_Quit(); | |
17 exit(rc); | |
18 } | |
12 | 19 |
13 int ThreadFunc(void *data) | 20 int ThreadFunc(void *data) |
14 { | 21 { |
15 printf("Started thread %s: My thread id is %u\n", | 22 printf("Started thread %s: My thread id is %u\n", |
16 (char *)data, SDL_ThreadID()); | 23 (char *)data, SDL_ThreadID()); |
25 static void killed(int sig) | 32 static void killed(int sig) |
26 { | 33 { |
27 printf("Killed with SIGTERM, waiting 5 seconds to exit\n"); | 34 printf("Killed with SIGTERM, waiting 5 seconds to exit\n"); |
28 SDL_Delay(5*1000); | 35 SDL_Delay(5*1000); |
29 alive = 0; | 36 alive = 0; |
30 exit(0); | 37 quit(0); |
31 } | 38 } |
32 | 39 |
33 int main(int argc, char *argv[]) | 40 int main(int argc, char *argv[]) |
34 { | 41 { |
35 SDL_Thread *thread; | 42 SDL_Thread *thread; |
36 | 43 |
37 /* Load the SDL library */ | 44 /* Load the SDL library */ |
38 if ( SDL_Init(0) < 0 ) { | 45 if ( SDL_Init(0) < 0 ) { |
39 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | 46 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); |
40 exit(1); | 47 return(1); |
41 } | 48 } |
42 atexit(SDL_Quit); | |
43 | 49 |
44 alive = 1; | 50 alive = 1; |
45 thread = SDL_CreateThread(ThreadFunc, "#1"); | 51 thread = SDL_CreateThread(ThreadFunc, "#1"); |
46 if ( thread == NULL ) { | 52 if ( thread == NULL ) { |
47 fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); | 53 fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); |
48 exit(1); | 54 quit(1); |
49 } | 55 } |
50 SDL_Delay(5*1000); | 56 SDL_Delay(5*1000); |
51 printf("Waiting for thread #1\n"); | 57 printf("Waiting for thread #1\n"); |
52 alive = 0; | 58 alive = 0; |
53 SDL_WaitThread(thread, NULL); | 59 SDL_WaitThread(thread, NULL); |
54 | 60 |
55 alive = 1; | 61 alive = 1; |
56 thread = SDL_CreateThread(ThreadFunc, "#2"); | 62 thread = SDL_CreateThread(ThreadFunc, "#2"); |
57 if ( thread == NULL ) { | 63 if ( thread == NULL ) { |
58 fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); | 64 fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); |
59 exit(1); | 65 quit(1); |
60 } | 66 } |
61 SDL_Delay(5*1000); | 67 SDL_Delay(5*1000); |
62 printf("Killing thread #2\n"); | 68 printf("Killing thread #2\n"); |
63 SDL_KillThread(thread); | 69 SDL_KillThread(thread); |
64 | 70 |
65 alive = 1; | 71 alive = 1; |
66 signal(SIGTERM, killed); | 72 signal(SIGTERM, killed); |
67 thread = SDL_CreateThread(ThreadFunc, "#3"); | 73 thread = SDL_CreateThread(ThreadFunc, "#3"); |
68 if ( thread == NULL ) { | 74 if ( thread == NULL ) { |
69 fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); | 75 fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); |
70 exit(1); | 76 quit(1); |
71 } | 77 } |
72 raise(SIGTERM); | 78 raise(SIGTERM); |
73 | 79 |
80 SDL_Quit(); /* Never reached */ | |
74 return(0); /* Never reached */ | 81 return(0); /* Never reached */ |
75 } | 82 } |