test/torturethread.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 09 May 2006 07:26:58 +0000
changeset 1790 828a17e05192
parent 1769 290b5baf2fca
child 1662 782fd950bd46
child 1895 c121d94672cb
permissions -rw-r--r--
Date: Mon, 8 May 2006 14:19:30 -0700
From: Bob Ippolito
Subject: SDL trunk (r2346) and Mac OS X

The current state of the trunk doesn't quite compile on Mac OS X,
I've attached a series of patches that gets it to compile and kills a
few warnings.

sdl-trunk-r2346-dlcompat-warnings.diff:
The dlcompat thing is just loaded with incorrect type signatures ..
some of them have changed since 10.2 and others are just flat wrong.
This puts it in sync with the latest headers. People on 10.2 will get
the warnings instead of people with new kits.
     1 
     2 /* Simple test of the SDL threading code */
     3 
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 #include <signal.h>
     7 #include <string.h>
     8 
     9 #include "SDL.h"
    10 #include "SDL_thread.h"
    11 
    12 #define NUMTHREADS 10
    13 
    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 }
    22 
    23 int SDLCALL SubThreadFunc(void *data) {
    24 	while(! *(int volatile *)data) {
    25 		; /*SDL_Delay(10);*/  /* do nothing */
    26 	}
    27 	return 0;
    28 }
    29 
    30 int SDLCALL ThreadFunc(void *data) {
    31 	SDL_Thread *sub_threads[NUMTHREADS];
    32 	int flags[NUMTHREADS];
    33 	int i;
    34 	int tid = (int)(uintptr_t)data;
    35 
    36 	fprintf(stderr, "Creating Thread %d\n", tid);
    37 
    38 	for(i = 0; i < NUMTHREADS; i++) {
    39 		flags[i] = 0;
    40 		sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
    41 	}
    42 
    43 	printf("Thread '%d' waiting for signal\n", tid);
    44 	while(time_for_threads_to_die[tid] != 1) {
    45 		; /* do nothing */
    46 	}
    47 
    48 	printf("Thread '%d' sending signals to subthreads\n", tid);
    49 	for(i = 0; i <  NUMTHREADS; i++) {
    50 		flags[i] = 1;
    51 		SDL_WaitThread(sub_threads[i], NULL);
    52 	}
    53 
    54 	printf("Thread '%d' exiting!\n", tid);
    55 
    56 	return 0;
    57 }
    58 
    59 int main(int argc, char *argv[])
    60 {
    61 	SDL_Thread *threads[NUMTHREADS];
    62 	int i;
    63 
    64 	/* Load the SDL library */
    65 	if ( SDL_Init(0) < 0 ) {
    66 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    67 		return(1);
    68 	}
    69 
    70 	signal(SIGSEGV, SIG_DFL);
    71 	for(i = 0; i < NUMTHREADS; i++) {
    72 		time_for_threads_to_die[i] = 0;
    73 		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 
    82 	for(i = 0; i < NUMTHREADS; i++) {
    83 		time_for_threads_to_die[i] = 1;
    84 	}
    85 
    86 	for(i = 0; i < NUMTHREADS; i++) {
    87 		SDL_WaitThread(threads[i], NULL);
    88 	}
    89 	SDL_Quit();
    90 	return(0);
    91 }