test/testsem.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 28 Sep 2005 11:36:20 +0000
changeset 1151 be9c9c8f6d53
parent 0 74212992fb08
child 1439 4d3bb026cd16
permissions -rw-r--r--
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
if SDL is built with a non-cdecl calling convention, and it's just generally
bad practice anyhow.

Now programs explicitly call SDL_Quit() where appropriate, wrap SDL_Quit() in
a cdecl function where it can't be avoided, and rely on the parachute where
a crash might have hit the atexit() before (these ARE test programs, after
all!).
     1 
     2 /* Simple test of the SDL semaphore code */
     3 
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 #include <signal.h>
     7 
     8 #include "SDL.h"
     9 #include "SDL_thread.h"
    10 
    11 #define NUM_THREADS 10
    12 
    13 static SDL_sem *sem;
    14 int alive = 1;
    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 ThreadFunc(void *data)
    24 {
    25 	while ( alive ) {
    26 		SDL_SemWait(sem);
    27 		fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", (int)data, SDL_SemValue(sem));
    28 		SDL_Delay(200);
    29 		SDL_SemPost(sem);
    30 		fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", (int)data, SDL_SemValue(sem));
    31 		SDL_Delay(1); /* For the scheduler */
    32 	}
    33 	printf("Thread number %d exiting.\n", (int)data);
    34 	return 0;
    35 }
    36 
    37 static void killed(int sig)
    38 {
    39 	alive = 0;
    40 }
    41 
    42 int main(int argc, char **argv)
    43 {
    44 	SDL_Thread *threads[NUM_THREADS];
    45 	int i, init_sem;
    46 
    47 	if(argc < 2) {
    48 		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
    49 		return(1);
    50 	}
    51 
    52 	/* Load the SDL library */
    53 	if ( SDL_Init(0) < 0 ) {
    54 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    55 		return(1);
    56 	}
    57 	signal(SIGTERM, killed);
    58 	signal(SIGINT, killed);
    59 	
    60 	init_sem = atoi(argv[1]);
    61 	sem = SDL_CreateSemaphore(init_sem);
    62 	
    63 	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
    64 	/* Create all the threads */
    65 	for( i = 0; i < NUM_THREADS; ++i ) {
    66 		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
    67 	}
    68 
    69 	/* Wait 10 seconds */
    70 	SDL_Delay(10 * 1000);
    71 
    72 	/* Wait for all threads to finish */
    73 	printf("Waiting for threads to finish\n");
    74 	alive = 0;
    75 	for( i = 0; i < NUM_THREADS; ++i ) {
    76 		SDL_WaitThread(threads[i], NULL);
    77 	}
    78 	printf("Finished waiting for threads\n");
    79 
    80 	SDL_DestroySemaphore(sem);
    81 	SDL_Quit();
    82 	return(0);
    83 }