19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Simple test of the SDL threading code and error handling */ 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h> 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdlib.h> 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h> 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h" 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h" 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int alive = 0; 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void quit(int rc) 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Quit(); 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall exit(rc); 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDLCALL ThreadFunc(void *data) 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the child thread error string */ 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Thread %s (%d) had a problem: %s", 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (char *)data, SDL_ThreadID(), "nevermind"); 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( alive ) { 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Thread '%s' is alive!\n", (char *)data); 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Delay(1*1000); 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Child thread error string: %s\n", SDL_GetError()); 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main(int argc, char *argv[]) 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Thread *thread; 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Load the SDL library */ 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_Init(0) < 0 ) { 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(1); 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the error value for the main thread */ 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("No worries"); 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall alive = 1; 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall thread = SDL_CreateThread(ThreadFunc, "#1"); 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( thread == NULL ) { 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall quit(1); 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Delay(5*1000); 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Waiting for thread #1\n"); 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall alive = 0; 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_WaitThread(thread, NULL); 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Main thread error string: %s\n", SDL_GetError()); 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Quit(); 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 62