test/testlock.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 18 Feb 2019 00:26:53 -0500
changeset 69 a2c5f6a01a55
parent 0 a6cb692b8939
permissions -rw-r--r--
Fill in a default palette for 8-bit screen surfaces.
icculus@0
     1
icculus@0
     2
/* Test the thread and mutex locking functions 
icculus@0
     3
   Also exercises the system's signal/thread interaction
icculus@0
     4
*/
icculus@0
     5
icculus@0
     6
#include <signal.h>
icculus@0
     7
#include <stdio.h>
icculus@0
     8
icculus@0
     9
#include "SDL.h"
icculus@0
    10
#include "SDL_mutex.h"
icculus@0
    11
#include "SDL_thread.h"
icculus@0
    12
icculus@0
    13
static SDL_mutex *mutex = NULL;
icculus@0
    14
static Uint32 mainthread;
icculus@0
    15
static SDL_Thread *threads[6];
icculus@0
    16
static volatile int doterminate = 0;
icculus@0
    17
icculus@0
    18
/*
icculus@0
    19
 * SDL_Quit() shouldn't be used with atexit() directly because
icculus@0
    20
 *  calling conventions may differ...
icculus@0
    21
 */
icculus@0
    22
static void SDL_Quit_Wrapper(void)
icculus@0
    23
{
icculus@0
    24
	SDL_Quit();
icculus@0
    25
}
icculus@0
    26
icculus@0
    27
void printid(void)
icculus@0
    28
{
icculus@0
    29
	printf("Process %u:  exiting\n", SDL_ThreadID());
icculus@0
    30
}
icculus@0
    31
	
icculus@0
    32
void terminate(int sig)
icculus@0
    33
{
icculus@0
    34
	signal(SIGINT, terminate);
icculus@0
    35
	doterminate = 1;
icculus@0
    36
}
icculus@0
    37
void closemutex(int sig)
icculus@0
    38
{
icculus@0
    39
	Uint32 id = SDL_ThreadID();
icculus@0
    40
	int i;
icculus@0
    41
	printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
icculus@0
    42
	for ( i=0; i<6; ++i )
icculus@0
    43
		SDL_KillThread(threads[i]);
icculus@0
    44
	SDL_DestroyMutex(mutex);
icculus@0
    45
	exit(sig);
icculus@0
    46
}
icculus@0
    47
int SDLCALL Run(void *data)
icculus@0
    48
{
icculus@0
    49
	if ( SDL_ThreadID() == mainthread )
icculus@0
    50
		signal(SIGTERM, closemutex);
icculus@0
    51
	while ( 1 ) {
icculus@0
    52
		printf("Process %u ready to work\n", SDL_ThreadID());
icculus@0
    53
		if ( SDL_mutexP(mutex) < 0 ) {
icculus@0
    54
			fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
icculus@0
    55
			exit(1);
icculus@0
    56
		}
icculus@0
    57
		printf("Process %u, working!\n", SDL_ThreadID());
icculus@0
    58
		SDL_Delay(1*1000);
icculus@0
    59
		printf("Process %u, done!\n", SDL_ThreadID());
icculus@0
    60
		if ( SDL_mutexV(mutex) < 0 ) {
icculus@0
    61
			fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
icculus@0
    62
			exit(1);
icculus@0
    63
		}
icculus@0
    64
		/* If this sleep isn't done, then threads may starve */
icculus@0
    65
		SDL_Delay(10);
icculus@0
    66
		if (SDL_ThreadID() == mainthread && doterminate) {
icculus@0
    67
			printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
icculus@0
    68
			raise(SIGTERM);
icculus@0
    69
		}
icculus@0
    70
	}
icculus@0
    71
	return(0);
icculus@0
    72
}
icculus@0
    73
icculus@0
    74
int main(int argc, char *argv[])
icculus@0
    75
{
icculus@0
    76
	int i;
icculus@0
    77
	int maxproc = 6;
icculus@0
    78
icculus@0
    79
	/* Load the SDL library */
icculus@0
    80
	if ( SDL_Init(0) < 0 ) {
icculus@0
    81
		fprintf(stderr, "%s\n", SDL_GetError());
icculus@0
    82
		exit(1);
icculus@0
    83
	}
icculus@0
    84
	atexit(SDL_Quit_Wrapper);
icculus@0
    85
icculus@0
    86
	if ( (mutex=SDL_CreateMutex()) == NULL ) {
icculus@0
    87
		fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
icculus@0
    88
		exit(1);
icculus@0
    89
	}
icculus@0
    90
icculus@0
    91
	mainthread = SDL_ThreadID();
icculus@0
    92
	printf("Main thread: %u\n", mainthread);
icculus@0
    93
	atexit(printid);
icculus@0
    94
	for ( i=0; i<maxproc; ++i ) {
icculus@0
    95
		if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL )
icculus@0
    96
			fprintf(stderr, "Couldn't create thread!\n");
icculus@0
    97
	}
icculus@0
    98
	signal(SIGINT, terminate);
icculus@0
    99
	Run(NULL);
icculus@0
   100
icculus@0
   101
	return(0);	/* Never reached */
icculus@0
   102
}