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 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 printf("Started thread %s: My thread id is %u\n", 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (char *)data, SDL_ThreadID()); 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( alive ) { 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Thread '%s' is alive!\n", (char *)data); 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Delay(1*1000); 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Thread '%s' exiting!\n", (char *)data); 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void killed(int sig) 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Killed with SIGTERM, waiting 5 seconds to exit\n"); 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Delay(5*1000); 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall alive = 0; 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall quit(0); 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main(int argc, char *argv[]) 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Thread *thread; 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Load the SDL library */ 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_Init(0) < 0 ) { 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(1); 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall alive = 1; 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall thread = SDL_CreateThread(ThreadFunc, "#1"); 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( thread == NULL ) { 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall quit(1); 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Delay(5*1000); 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Waiting for thread #1\n"); 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall alive = 0; 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_WaitThread(thread, NULL); 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall alive = 1; 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall thread = SDL_CreateThread(ThreadFunc, "#2"); 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( thread == NULL ) { 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall quit(1); 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Delay(5*1000); 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall printf("Killing thread #2\n"); 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_KillThread(thread); 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall alive = 1; 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall signal(SIGTERM, killed); 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall thread = SDL_CreateThread(ThreadFunc, "#3"); 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( thread == NULL ) { 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall quit(1); 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall raise(SIGTERM); 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Quit(); /* Never reached */ 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); /* Never reached */ 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 83