test/torturethread.c
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 5535 96594ac5fd1a
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
    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 
    15 
    16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    17 static void quit(int rc)
    17 static void
       
    18 quit(int rc)
    18 {
    19 {
    19 	SDL_Quit();
    20     SDL_Quit();
    20 	exit(rc);
    21     exit(rc);
    21 }
    22 }
    22 
    23 
    23 int SDLCALL SubThreadFunc(void *data) {
    24 int SDLCALL
    24 	while(! *(int volatile *)data) {
    25 SubThreadFunc(void *data)
    25 		; /*SDL_Delay(10);*/  /* do nothing */
    26 {
    26 	}
    27     while (!*(int volatile *) data) {
    27 	return 0;
    28         ;                       /*SDL_Delay(10); *//* do nothing */
       
    29     }
       
    30     return 0;
    28 }
    31 }
    29 
    32 
    30 int SDLCALL ThreadFunc(void *data) {
    33 int SDLCALL
    31 	SDL_Thread *sub_threads[NUMTHREADS];
    34 ThreadFunc(void *data)
    32 	int flags[NUMTHREADS];
    35 {
    33 	int i;
    36     SDL_Thread *sub_threads[NUMTHREADS];
    34 	int tid = (int)(uintptr_t)data;
    37     int flags[NUMTHREADS];
       
    38     int i;
       
    39     int tid = (int) (uintptr_t) data;
    35 
    40 
    36 	fprintf(stderr, "Creating Thread %d\n", tid);
    41     fprintf(stderr, "Creating Thread %d\n", tid);
    37 
    42 
    38 	for(i = 0; i < NUMTHREADS; i++) {
    43     for (i = 0; i < NUMTHREADS; i++) {
    39 		flags[i] = 0;
    44         flags[i] = 0;
    40 		sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
    45         sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
    41 	}
    46     }
    42 
    47 
    43 	printf("Thread '%d' waiting for signal\n", tid);
    48     printf("Thread '%d' waiting for signal\n", tid);
    44 	while(time_for_threads_to_die[tid] != 1) {
    49     while (time_for_threads_to_die[tid] != 1) {
    45 		; /* do nothing */
    50         ;                       /* do nothing */
    46 	}
    51     }
    47 
    52 
    48 	printf("Thread '%d' sending signals to subthreads\n", tid);
    53     printf("Thread '%d' sending signals to subthreads\n", tid);
    49 	for(i = 0; i <  NUMTHREADS; i++) {
    54     for (i = 0; i < NUMTHREADS; i++) {
    50 		flags[i] = 1;
    55         flags[i] = 1;
    51 		SDL_WaitThread(sub_threads[i], NULL);
    56         SDL_WaitThread(sub_threads[i], NULL);
    52 	}
    57     }
    53 
    58 
    54 	printf("Thread '%d' exiting!\n", tid);
    59     printf("Thread '%d' exiting!\n", tid);
    55 
    60 
    56 	return 0;
    61     return 0;
    57 }
    62 }
    58 
    63 
    59 int main(int argc, char *argv[])
    64 int
       
    65 main(int argc, char *argv[])
    60 {
    66 {
    61 	SDL_Thread *threads[NUMTHREADS];
    67     SDL_Thread *threads[NUMTHREADS];
    62 	int i;
    68     int i;
    63 
    69 
    64 	/* Load the SDL library */
    70     /* Load the SDL library */
    65 	if ( SDL_Init(0) < 0 ) {
    71     if (SDL_Init(0) < 0) {
    66 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    72         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    67 		return(1);
    73         return (1);
    68 	}
    74     }
    69 
    75 
    70 	signal(SIGSEGV, SIG_DFL);
    76     signal(SIGSEGV, SIG_DFL);
    71 	for(i = 0; i < NUMTHREADS; i++) {
    77     for (i = 0; i < NUMTHREADS; i++) {
    72 		time_for_threads_to_die[i] = 0;
    78         time_for_threads_to_die[i] = 0;
    73 		threads[i] = SDL_CreateThread(ThreadFunc, (void *)(uintptr_t)i);
    79         threads[i] = SDL_CreateThread(ThreadFunc, (void *) (uintptr_t) i);
    74 	
       
    75 		if ( threads[i] == NULL ) {
       
    76 			fprintf(stderr,
       
    77 			"Couldn't create thread: %s\n", SDL_GetError());
       
    78 			quit(1);
       
    79 		}
       
    80 	}
       
    81 
    80 
    82 	for(i = 0; i < NUMTHREADS; i++) {
    81         if (threads[i] == NULL) {
    83 		time_for_threads_to_die[i] = 1;
    82             fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    84 	}
    83             quit(1);
       
    84         }
       
    85     }
    85 
    86 
    86 	for(i = 0; i < NUMTHREADS; i++) {
    87     for (i = 0; i < NUMTHREADS; i++) {
    87 		SDL_WaitThread(threads[i], NULL);
    88         time_for_threads_to_die[i] = 1;
    88 	}
    89     }
    89 	SDL_Quit();
    90 
    90 	return(0);
    91     for (i = 0; i < NUMTHREADS; i++) {
       
    92         SDL_WaitThread(threads[i], NULL);
       
    93     }
       
    94     SDL_Quit();
       
    95     return (0);
    91 }
    96 }