19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Simple test of the SDL threading code */
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdlib.h>
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h>
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NUMTHREADS 10
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char volatile time_for_threads_to_die[NUMTHREADS];
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void quit(int rc)
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	exit(rc);
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDLCALL SubThreadFunc(void *data) {
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(! *(int volatile *)data) {
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		; /*SDL_Delay(10);*/  /* do nothing */
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDLCALL ThreadFunc(void *data) {
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Thread *sub_threads[NUMTHREADS];
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int flags[NUMTHREADS];
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int tid = (int)(uintptr_t)data;
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Creating Thread %d\n", tid);
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 0; i < NUMTHREADS; i++) {
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flags[i] = 0;
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Thread '%d' waiting for signal\n", tid);
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(time_for_threads_to_die[tid] != 1) {
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		; /* do nothing */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Thread '%d' sending signals to subthreads\n", tid);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 0; i <  NUMTHREADS; i++) {
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flags[i] = 1;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WaitThread(sub_threads[i], NULL);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Thread '%d' exiting!\n", tid);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main(int argc, char *argv[])
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Thread *threads[NUMTHREADS];
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Load the SDL library */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Init(0) < 0 ) {
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(1);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	signal(SIGSEGV, SIG_DFL);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 0; i < NUMTHREADS; i++) {
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		time_for_threads_to_die[i] = 0;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		threads[i] = SDL_CreateThread(ThreadFunc, (void *)(uintptr_t)i);
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( threads[i] == NULL ) {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			"Couldn't create thread: %s\n", SDL_GetError());
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			quit(1);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 0; i < NUMTHREADS; i++) {
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		time_for_threads_to_die[i] = 1;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 0; i < NUMTHREADS; i++) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WaitThread(threads[i], NULL);
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
92