comparison test/torturethread.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 d93862a3d821
comparison
equal deleted inserted replaced
1150:7d8e1925f35b 1151:be9c9c8f6d53
10 #include "SDL_thread.h" 10 #include "SDL_thread.h"
11 11
12 #define NUMTHREADS 10 12 #define NUMTHREADS 10
13 13
14 static char volatile time_for_threads_to_die[NUMTHREADS]; 14 static char volatile time_for_threads_to_die[NUMTHREADS];
15
16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
17 static void quit(int rc)
18 {
19 SDL_Quit();
20 exit(rc);
21 }
15 22
16 int SubThreadFunc(void *data) { 23 int SubThreadFunc(void *data) {
17 while(! *(int volatile *)data) { 24 while(! *(int volatile *)data) {
18 ; /*SDL_Delay(10); /* do nothing */ 25 ; /*SDL_Delay(10); /* do nothing */
19 } 26 }
55 int i; 62 int i;
56 63
57 /* Load the SDL library */ 64 /* Load the SDL library */
58 if ( SDL_Init(0) < 0 ) { 65 if ( SDL_Init(0) < 0 ) {
59 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 66 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
60 exit(1); 67 return(1);
61 } 68 }
62 atexit(SDL_Quit);
63
64 69
65 signal(SIGSEGV, SIG_DFL); 70 signal(SIGSEGV, SIG_DFL);
66 for(i = 0; i < NUMTHREADS; i++) { 71 for(i = 0; i < NUMTHREADS; i++) {
67 time_for_threads_to_die[i] = 0; 72 time_for_threads_to_die[i] = 0;
68 threads[i] = SDL_CreateThread(ThreadFunc, (void *) i); 73 threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
69 74
70 if ( threads[i] == NULL ) { 75 if ( threads[i] == NULL ) {
71 fprintf(stderr, 76 fprintf(stderr,
72 "Couldn't create thread: %s\n", SDL_GetError()); 77 "Couldn't create thread: %s\n", SDL_GetError());
73 exit(1); 78 quit(1);
74 } 79 }
75 } 80 }
76 81
77 for(i = 0; i < NUMTHREADS; i++) { 82 for(i = 0; i < NUMTHREADS; i++) {
78 time_for_threads_to_die[i] = 1; 83 time_for_threads_to_die[i] = 1;
79 } 84 }
80 85
81 for(i = NUMTHREADS-1; i >= 0; --i) { 86 for(i = NUMTHREADS-1; i >= 0; --i) {
82 SDL_WaitThread(threads[i], NULL); 87 SDL_WaitThread(threads[i], NULL);
83 } 88 }
84 return(0); /* Never reached */ 89 SDL_Quit();
90 return(0);
85 } 91 }