test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 26 Feb 2006 05:02:14 +0000
changeset 1439 4d3bb026cd16
parent 1151 be9c9c8f6d53
child 1495 1dd8bf30a109
permissions -rw-r--r--
Fixed warnings in -pedantic mode
slouken@0
     1
slouken@0
     2
/* Simple test of the SDL semaphore code */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <signal.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
#include "SDL_thread.h"
slouken@0
    10
slouken@0
    11
#define NUM_THREADS 10
slouken@0
    12
slouken@0
    13
static SDL_sem *sem;
slouken@0
    14
int alive = 1;
slouken@0
    15
slouken@0
    16
int ThreadFunc(void *data)
slouken@0
    17
{
slouken@0
    18
	while ( alive ) {
slouken@0
    19
		SDL_SemWait(sem);
slouken@0
    20
		fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", (int)data, SDL_SemValue(sem));
slouken@0
    21
		SDL_Delay(200);
slouken@0
    22
		SDL_SemPost(sem);
slouken@0
    23
		fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", (int)data, SDL_SemValue(sem));
slouken@0
    24
		SDL_Delay(1); /* For the scheduler */
slouken@0
    25
	}
slouken@0
    26
	printf("Thread number %d exiting.\n", (int)data);
slouken@0
    27
	return 0;
slouken@0
    28
}
slouken@0
    29
slouken@0
    30
static void killed(int sig)
slouken@0
    31
{
slouken@0
    32
	alive = 0;
slouken@0
    33
}
slouken@0
    34
slouken@0
    35
int main(int argc, char **argv)
slouken@0
    36
{
slouken@0
    37
	SDL_Thread *threads[NUM_THREADS];
slouken@0
    38
	int i, init_sem;
slouken@0
    39
slouken@0
    40
	if(argc < 2) {
slouken@0
    41
		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
icculus@1151
    42
		return(1);
slouken@0
    43
	}
slouken@0
    44
slouken@0
    45
	/* Load the SDL library */
slouken@0
    46
	if ( SDL_Init(0) < 0 ) {
slouken@0
    47
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    48
		return(1);
slouken@0
    49
	}
slouken@0
    50
	signal(SIGTERM, killed);
slouken@0
    51
	signal(SIGINT, killed);
slouken@0
    52
	
slouken@0
    53
	init_sem = atoi(argv[1]);
slouken@0
    54
	sem = SDL_CreateSemaphore(init_sem);
slouken@0
    55
	
slouken@0
    56
	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
slouken@0
    57
	/* Create all the threads */
slouken@0
    58
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    59
		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
slouken@0
    60
	}
slouken@0
    61
slouken@0
    62
	/* Wait 10 seconds */
slouken@0
    63
	SDL_Delay(10 * 1000);
slouken@0
    64
slouken@0
    65
	/* Wait for all threads to finish */
slouken@0
    66
	printf("Waiting for threads to finish\n");
slouken@0
    67
	alive = 0;
slouken@0
    68
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    69
		SDL_WaitThread(threads[i], NULL);
slouken@0
    70
	}
slouken@0
    71
	printf("Finished waiting for threads\n");
slouken@0
    72
slouken@0
    73
	SDL_DestroySemaphore(sem);
icculus@1151
    74
	SDL_Quit();
slouken@0
    75
	return(0);
slouken@0
    76
}