test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 29 Feb 2008 13:58:37 +0000
branchSDL-1.2
changeset 4141 8b10a6efad41
parent 1769 290b5baf2fca
child 1662 782fd950bd46
child 1895 c121d94672cb
child 5105 99acf3d856cb
permissions -rw-r--r--
Added patch note for Sylvain's patch
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@1769
    16
int SDLCALL ThreadFunc(void *data)
slouken@0
    17
{
slouken@1615
    18
	int threadnum = (int)(uintptr_t)data;
slouken@0
    19
	while ( alive ) {
slouken@0
    20
		SDL_SemWait(sem);
slouken@1495
    21
		fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
slouken@0
    22
		SDL_Delay(200);
slouken@0
    23
		SDL_SemPost(sem);
slouken@1495
    24
		fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
slouken@0
    25
		SDL_Delay(1); /* For the scheduler */
slouken@0
    26
	}
slouken@1495
    27
	printf("Thread number %d exiting.\n", threadnum);
slouken@0
    28
	return 0;
slouken@0
    29
}
slouken@0
    30
slouken@0
    31
static void killed(int sig)
slouken@0
    32
{
slouken@0
    33
	alive = 0;
slouken@0
    34
}
slouken@0
    35
slouken@0
    36
int main(int argc, char **argv)
slouken@0
    37
{
slouken@0
    38
	SDL_Thread *threads[NUM_THREADS];
slouken@1495
    39
	uintptr_t i;
slouken@1495
    40
	int init_sem;
slouken@0
    41
slouken@0
    42
	if(argc < 2) {
slouken@0
    43
		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
icculus@1151
    44
		return(1);
slouken@0
    45
	}
slouken@0
    46
slouken@0
    47
	/* Load the SDL library */
slouken@0
    48
	if ( SDL_Init(0) < 0 ) {
slouken@0
    49
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    50
		return(1);
slouken@0
    51
	}
slouken@0
    52
	signal(SIGTERM, killed);
slouken@0
    53
	signal(SIGINT, killed);
slouken@0
    54
	
slouken@0
    55
	init_sem = atoi(argv[1]);
slouken@0
    56
	sem = SDL_CreateSemaphore(init_sem);
slouken@0
    57
	
slouken@0
    58
	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
slouken@0
    59
	/* Create all the threads */
slouken@0
    60
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    61
		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
slouken@0
    62
	}
slouken@0
    63
slouken@0
    64
	/* Wait 10 seconds */
slouken@0
    65
	SDL_Delay(10 * 1000);
slouken@0
    66
slouken@0
    67
	/* Wait for all threads to finish */
slouken@0
    68
	printf("Waiting for threads to finish\n");
slouken@0
    69
	alive = 0;
slouken@0
    70
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    71
		SDL_WaitThread(threads[i], NULL);
slouken@0
    72
	}
slouken@0
    73
	printf("Finished waiting for threads\n");
slouken@0
    74
slouken@0
    75
	SDL_DestroySemaphore(sem);
icculus@1151
    76
	SDL_Quit();
slouken@0
    77
	return(0);
slouken@0
    78
}