test/testlock.c
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testlock.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,89 @@
     1.4 +
     1.5 +/* Test the thread and mutex locking functions 
     1.6 +   Also exercises the system's signal/thread interaction
     1.7 +*/
     1.8 +
     1.9 +#include <signal.h>
    1.10 +#include <stdio.h>
    1.11 +#include <stdlib.h>
    1.12 +
    1.13 +#include "SDL.h"
    1.14 +#include "SDL_mutex.h"
    1.15 +#include "SDL_thread.h"
    1.16 +
    1.17 +static SDL_mutex *mutex = NULL;
    1.18 +static Uint32 mainthread;
    1.19 +static SDL_Thread *threads[6];
    1.20 +
    1.21 +void printid(void)
    1.22 +{
    1.23 +	printf("Process %u:  exiting\n", SDL_ThreadID());
    1.24 +}
    1.25 +	
    1.26 +void terminate(int sig)
    1.27 +{
    1.28 +	printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
    1.29 +	raise(SIGTERM);
    1.30 +}
    1.31 +void closemutex(int sig)
    1.32 +{
    1.33 +	Uint32 id = SDL_ThreadID();
    1.34 +	int i;
    1.35 +	printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
    1.36 +	for ( i=0; i<6; ++i )
    1.37 +		SDL_KillThread(threads[i]);
    1.38 +	SDL_DestroyMutex(mutex);
    1.39 +	exit(sig);
    1.40 +}
    1.41 +int Run(void *data)
    1.42 +{
    1.43 +	if ( SDL_ThreadID() == mainthread )
    1.44 +		signal(SIGTERM, closemutex);
    1.45 +	while ( 1 ) {
    1.46 +		printf("Process %u ready to work\n", SDL_ThreadID());
    1.47 +		if ( SDL_mutexP(mutex) < 0 ) {
    1.48 +			fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
    1.49 +			exit(1);
    1.50 +		}
    1.51 +		printf("Process %u, working!\n", SDL_ThreadID());
    1.52 +		SDL_Delay(1*1000);
    1.53 +		printf("Process %u, done!\n", SDL_ThreadID());
    1.54 +		if ( SDL_mutexV(mutex) < 0 ) {
    1.55 +			fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
    1.56 +			exit(1);
    1.57 +		}
    1.58 +		/* If this sleep isn't done, then threads may starve */
    1.59 +		SDL_Delay(10);
    1.60 +	}
    1.61 +	return(0);
    1.62 +}
    1.63 +
    1.64 +int main(int argc, char *argv[])
    1.65 +{
    1.66 +	int i;
    1.67 +	int maxproc = 6;
    1.68 +
    1.69 +	/* Load the SDL library */
    1.70 +	if ( SDL_Init(0) < 0 ) {
    1.71 +		fprintf(stderr, "%s\n", SDL_GetError());
    1.72 +		exit(1);
    1.73 +	}
    1.74 +	atexit(SDL_Quit);
    1.75 +
    1.76 +	if ( (mutex=SDL_CreateMutex()) == NULL ) {
    1.77 +		fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
    1.78 +		exit(1);
    1.79 +	}
    1.80 +
    1.81 +	mainthread = SDL_ThreadID();
    1.82 +	printf("Main thread: %u\n", mainthread);
    1.83 +	atexit(printid);
    1.84 +	for ( i=0; i<maxproc; ++i ) {
    1.85 +		if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL )
    1.86 +			fprintf(stderr, "Couldn't create thread!\n");
    1.87 +	}
    1.88 +	signal(SIGINT, terminate);
    1.89 +	Run(NULL);
    1.90 +
    1.91 +	return(0);	/* Never reached */
    1.92 +}