test/testlock.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 28 Sep 2005 11:36:20 +0000
changeset 1151 be9c9c8f6d53
parent 0 74212992fb08
child 1659 14717b52abc0
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!).
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
icculus@1151
    18
/*
icculus@1151
    19
 * SDL_Quit() shouldn't be used with atexit() directly because
icculus@1151
    20
 *  calling conventions may differ...
icculus@1151
    21
 */
icculus@1151
    22
static void SDL_Quit_Wrapper(void)
icculus@1151
    23
{
icculus@1151
    24
	SDL_Quit();
icculus@1151
    25
}
icculus@1151
    26
slouken@0
    27
void printid(void)
slouken@0
    28
{
slouken@0
    29
	printf("Process %u:  exiting\n", SDL_ThreadID());
slouken@0
    30
}
slouken@0
    31
	
slouken@0
    32
void terminate(int sig)
slouken@0
    33
{
slouken@0
    34
	printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
slouken@0
    35
	raise(SIGTERM);
slouken@0
    36
}
slouken@0
    37
void closemutex(int sig)
slouken@0
    38
{
slouken@0
    39
	Uint32 id = SDL_ThreadID();
slouken@0
    40
	int i;
slouken@0
    41
	printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
slouken@0
    42
	for ( i=0; i<6; ++i )
slouken@0
    43
		SDL_KillThread(threads[i]);
slouken@0
    44
	SDL_DestroyMutex(mutex);
slouken@0
    45
	exit(sig);
slouken@0
    46
}
slouken@0
    47
int Run(void *data)
slouken@0
    48
{
slouken@0
    49
	if ( SDL_ThreadID() == mainthread )
slouken@0
    50
		signal(SIGTERM, closemutex);
slouken@0
    51
	while ( 1 ) {
slouken@0
    52
		printf("Process %u ready to work\n", SDL_ThreadID());
slouken@0
    53
		if ( SDL_mutexP(mutex) < 0 ) {
slouken@0
    54
			fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
slouken@0
    55
			exit(1);
slouken@0
    56
		}
slouken@0
    57
		printf("Process %u, working!\n", SDL_ThreadID());
slouken@0
    58
		SDL_Delay(1*1000);
slouken@0
    59
		printf("Process %u, done!\n", SDL_ThreadID());
slouken@0
    60
		if ( SDL_mutexV(mutex) < 0 ) {
slouken@0
    61
			fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
slouken@0
    62
			exit(1);
slouken@0
    63
		}
slouken@0
    64
		/* If this sleep isn't done, then threads may starve */
slouken@0
    65
		SDL_Delay(10);
slouken@0
    66
	}
slouken@0
    67
	return(0);
slouken@0
    68
}
slouken@0
    69
slouken@0
    70
int main(int argc, char *argv[])
slouken@0
    71
{
slouken@0
    72
	int i;
slouken@0
    73
	int maxproc = 6;
slouken@0
    74
slouken@0
    75
	/* Load the SDL library */
slouken@0
    76
	if ( SDL_Init(0) < 0 ) {
slouken@0
    77
		fprintf(stderr, "%s\n", SDL_GetError());
slouken@0
    78
		exit(1);
slouken@0
    79
	}
icculus@1151
    80
	atexit(SDL_Quit_Wrapper);
slouken@0
    81
slouken@0
    82
	if ( (mutex=SDL_CreateMutex()) == NULL ) {
slouken@0
    83
		fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
slouken@0
    84
		exit(1);
slouken@0
    85
	}
slouken@0
    86
slouken@0
    87
	mainthread = SDL_ThreadID();
slouken@0
    88
	printf("Main thread: %u\n", mainthread);
slouken@0
    89
	atexit(printid);
slouken@0
    90
	for ( i=0; i<maxproc; ++i ) {
slouken@0
    91
		if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL )
slouken@0
    92
			fprintf(stderr, "Couldn't create thread!\n");
slouken@0
    93
	}
slouken@0
    94
	signal(SIGINT, terminate);
slouken@0
    95
	Run(NULL);
slouken@0
    96
slouken@0
    97
	return(0);	/* Never reached */
slouken@0
    98
}