test/testlock.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 01 Jul 2018 09:50:47 +0300
branchSDL-1.2
changeset 12050 f68904a749c9
parent 1769 290b5baf2fca
child 1662 782fd950bd46
child 1895 c121d94672cb
permissions -rw-r--r--
make sure SDL_vsnprintf() nul terminates if it is using _vsnprintf

(bug #3769: backported commit 5e1341f8c467 for windows and watcom.)
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
slouken@0
     9
#include "SDL.h"
slouken@0
    10
#include "SDL_mutex.h"
slouken@0
    11
#include "SDL_thread.h"
slouken@0
    12
slouken@0
    13
static SDL_mutex *mutex = NULL;
slouken@0
    14
static Uint32 mainthread;
slouken@0
    15
static SDL_Thread *threads[6];
slouken@1769
    16
static volatile int doterminate = 0;
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@1769
    34
	signal(SIGINT, terminate);
slouken@1769
    35
	doterminate = 1;
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@1769
    47
int SDLCALL 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@1769
    66
		if (SDL_ThreadID() == mainthread && doterminate) {
slouken@1769
    67
			printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
slouken@1769
    68
			raise(SIGTERM);
slouken@1769
    69
		}
slouken@0
    70
	}
slouken@0
    71
	return(0);
slouken@0
    72
}
slouken@0
    73
slouken@0
    74
int main(int argc, char *argv[])
slouken@0
    75
{
slouken@0
    76
	int i;
slouken@0
    77
	int maxproc = 6;
slouken@0
    78
slouken@0
    79
	/* Load the SDL library */
slouken@0
    80
	if ( SDL_Init(0) < 0 ) {
slouken@0
    81
		fprintf(stderr, "%s\n", SDL_GetError());
slouken@0
    82
		exit(1);
slouken@0
    83
	}
icculus@1151
    84
	atexit(SDL_Quit_Wrapper);
slouken@0
    85
slouken@0
    86
	if ( (mutex=SDL_CreateMutex()) == NULL ) {
slouken@0
    87
		fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
slouken@0
    88
		exit(1);
slouken@0
    89
	}
slouken@0
    90
slouken@0
    91
	mainthread = SDL_ThreadID();
slouken@0
    92
	printf("Main thread: %u\n", mainthread);
slouken@0
    93
	atexit(printid);
slouken@0
    94
	for ( i=0; i<maxproc; ++i ) {
slouken@0
    95
		if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL )
slouken@0
    96
			fprintf(stderr, "Couldn't create thread!\n");
slouken@0
    97
	}
slouken@0
    98
	signal(SIGINT, terminate);
slouken@0
    99
	Run(NULL);
slouken@0
   100
slouken@0
   101
	return(0);	/* Never reached */
slouken@0
   102
}