test/testsem.c
author Patrice Mandin <patmandin@gmail.com>
Wed, 13 Aug 2014 11:02:11 +0200
branchSDL-1.2
changeset 9059 9a9f56143400
parent 6297 c787fb1b5699
permissions -rw-r--r--
atari:gem: Remove unused variables.
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@5105
    36
static void TestWaitTimeout(void)
slouken@5105
    37
{
slouken@5105
    38
	Uint32 start_ticks;
slouken@5105
    39
	Uint32 end_ticks;
slouken@5105
    40
	Uint32 duration;
slouken@6297
    41
	int retval;
slouken@5105
    42
slouken@5105
    43
	sem = SDL_CreateSemaphore(0);
slouken@5105
    44
	printf("Waiting 2 seconds on semaphore\n");
slouken@5105
    45
slouken@5105
    46
	start_ticks = SDL_GetTicks();
slouken@6297
    47
	retval = SDL_SemWaitTimeout(sem, 2000);
slouken@5105
    48
	end_ticks = SDL_GetTicks();
slouken@5105
    49
slouken@5105
    50
	duration = end_ticks - start_ticks;
slouken@5105
    51
	
slouken@5105
    52
	/* Accept a little offset in the effective wait */
slouken@5105
    53
	if (duration > 1900 && duration < 2050)
slouken@5105
    54
		printf("Wait done.\n");
slouken@5105
    55
	else
slouken@5105
    56
		fprintf(stderr, "Wait took %d milliseconds\n", duration);
slouken@6297
    57
	
slouken@6297
    58
	/* Check to make sure the return value indicates timed out */
slouken@6297
    59
	if (retval != SDL_MUTEX_TIMEDOUT) 
slouken@6297
    60
		fprintf(stderr, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
slouken@5105
    61
}
slouken@5105
    62
slouken@0
    63
int main(int argc, char **argv)
slouken@0
    64
{
slouken@0
    65
	SDL_Thread *threads[NUM_THREADS];
slouken@1495
    66
	uintptr_t i;
slouken@1495
    67
	int init_sem;
slouken@0
    68
slouken@0
    69
	if(argc < 2) {
slouken@0
    70
		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
icculus@1151
    71
		return(1);
slouken@0
    72
	}
slouken@0
    73
slouken@0
    74
	/* Load the SDL library */
slouken@0
    75
	if ( SDL_Init(0) < 0 ) {
slouken@0
    76
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    77
		return(1);
slouken@0
    78
	}
slouken@0
    79
	signal(SIGTERM, killed);
slouken@0
    80
	signal(SIGINT, killed);
slouken@0
    81
	
slouken@0
    82
	init_sem = atoi(argv[1]);
slouken@0
    83
	sem = SDL_CreateSemaphore(init_sem);
slouken@0
    84
	
slouken@0
    85
	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
slouken@0
    86
	/* Create all the threads */
slouken@0
    87
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    88
		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
slouken@0
    89
	}
slouken@0
    90
slouken@0
    91
	/* Wait 10 seconds */
slouken@0
    92
	SDL_Delay(10 * 1000);
slouken@0
    93
slouken@0
    94
	/* Wait for all threads to finish */
slouken@0
    95
	printf("Waiting for threads to finish\n");
slouken@0
    96
	alive = 0;
slouken@0
    97
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    98
		SDL_WaitThread(threads[i], NULL);
slouken@0
    99
	}
slouken@0
   100
	printf("Finished waiting for threads\n");
slouken@0
   101
slouken@0
   102
	SDL_DestroySemaphore(sem);
slouken@5105
   103
slouken@5105
   104
	TestWaitTimeout();
slouken@5105
   105
icculus@1151
   106
	SDL_Quit();
slouken@0
   107
	return(0);
slouken@0
   108
}