test/testlock.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
permissions -rw-r--r--
Initial revision
slouken@0
     1
slouken@0
     2
/* Test the thread and mutex locking functions 
slouken@0
     3
   Also exercises the system's signal/thread interaction
slouken@0
     4
*/
slouken@0
     5
slouken@0
     6
#include <signal.h>
slouken@0
     7
#include <stdio.h>
slouken@0
     8
#include <stdlib.h>
slouken@0
     9
slouken@0
    10
#include "SDL.h"
slouken@0
    11
#include "SDL_mutex.h"
slouken@0
    12
#include "SDL_thread.h"
slouken@0
    13
slouken@0
    14
static SDL_mutex *mutex = NULL;
slouken@0
    15
static Uint32 mainthread;
slouken@0
    16
static SDL_Thread *threads[6];
slouken@0
    17
slouken@0
    18
void printid(void)
slouken@0
    19
{
slouken@0
    20
	printf("Process %u:  exiting\n", SDL_ThreadID());
slouken@0
    21
}
slouken@0
    22
	
slouken@0
    23
void terminate(int sig)
slouken@0
    24
{
slouken@0
    25
	printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
slouken@0
    26
	raise(SIGTERM);
slouken@0
    27
}
slouken@0
    28
void closemutex(int sig)
slouken@0
    29
{
slouken@0
    30
	Uint32 id = SDL_ThreadID();
slouken@0
    31
	int i;
slouken@0
    32
	printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
slouken@0
    33
	for ( i=0; i<6; ++i )
slouken@0
    34
		SDL_KillThread(threads[i]);
slouken@0
    35
	SDL_DestroyMutex(mutex);
slouken@0
    36
	exit(sig);
slouken@0
    37
}
slouken@0
    38
int Run(void *data)
slouken@0
    39
{
slouken@0
    40
	if ( SDL_ThreadID() == mainthread )
slouken@0
    41
		signal(SIGTERM, closemutex);
slouken@0
    42
	while ( 1 ) {
slouken@0
    43
		printf("Process %u ready to work\n", SDL_ThreadID());
slouken@0
    44
		if ( SDL_mutexP(mutex) < 0 ) {
slouken@0
    45
			fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
slouken@0
    46
			exit(1);
slouken@0
    47
		}
slouken@0
    48
		printf("Process %u, working!\n", SDL_ThreadID());
slouken@0
    49
		SDL_Delay(1*1000);
slouken@0
    50
		printf("Process %u, done!\n", SDL_ThreadID());
slouken@0
    51
		if ( SDL_mutexV(mutex) < 0 ) {
slouken@0
    52
			fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
slouken@0
    53
			exit(1);
slouken@0
    54
		}
slouken@0
    55
		/* If this sleep isn't done, then threads may starve */
slouken@0
    56
		SDL_Delay(10);
slouken@0
    57
	}
slouken@0
    58
	return(0);
slouken@0
    59
}
slouken@0
    60
slouken@0
    61
int main(int argc, char *argv[])
slouken@0
    62
{
slouken@0
    63
	int i;
slouken@0
    64
	int maxproc = 6;
slouken@0
    65
slouken@0
    66
	/* Load the SDL library */
slouken@0
    67
	if ( SDL_Init(0) < 0 ) {
slouken@0
    68
		fprintf(stderr, "%s\n", SDL_GetError());
slouken@0
    69
		exit(1);
slouken@0
    70
	}
slouken@0
    71
	atexit(SDL_Quit);
slouken@0
    72
slouken@0
    73
	if ( (mutex=SDL_CreateMutex()) == NULL ) {
slouken@0
    74
		fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
slouken@0
    75
		exit(1);
slouken@0
    76
	}
slouken@0
    77
slouken@0
    78
	mainthread = SDL_ThreadID();
slouken@0
    79
	printf("Main thread: %u\n", mainthread);
slouken@0
    80
	atexit(printid);
slouken@0
    81
	for ( i=0; i<maxproc; ++i ) {
slouken@0
    82
		if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL )
slouken@0
    83
			fprintf(stderr, "Couldn't create thread!\n");
slouken@0
    84
	}
slouken@0
    85
	signal(SIGINT, terminate);
slouken@0
    86
	Run(NULL);
slouken@0
    87
slouken@0
    88
	return(0);	/* Never reached */
slouken@0
    89
}