test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 27 Jan 2011 00:33:26 -0800
branchSDL-1.2
changeset 5105 99acf3d856cb
parent 1769 290b5baf2fca
child 6297 c787fb1b5699
permissions -rw-r--r--
Colin Leroy 2011-01-26 04:24:20 PST

the pthread implementation of SDL_SemWaitTimeout() uses busy waiting, while
pthread's sem_timedwait() does work. Attached are patches that make use of it
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@5105
    41
slouken@5105
    42
	sem = SDL_CreateSemaphore(0);
slouken@5105
    43
	printf("Waiting 2 seconds on semaphore\n");
slouken@5105
    44
slouken@5105
    45
	start_ticks = SDL_GetTicks();
slouken@5105
    46
	SDL_SemWaitTimeout(sem, 2000);
slouken@5105
    47
	end_ticks = SDL_GetTicks();
slouken@5105
    48
slouken@5105
    49
	duration = end_ticks - start_ticks;
slouken@5105
    50
	
slouken@5105
    51
	/* Accept a little offset in the effective wait */
slouken@5105
    52
	if (duration > 1900 && duration < 2050)
slouken@5105
    53
		printf("Wait done.\n");
slouken@5105
    54
	else
slouken@5105
    55
		fprintf(stderr, "Wait took %d milliseconds\n", duration);
slouken@5105
    56
}
slouken@5105
    57
slouken@0
    58
int main(int argc, char **argv)
slouken@0
    59
{
slouken@0
    60
	SDL_Thread *threads[NUM_THREADS];
slouken@1495
    61
	uintptr_t i;
slouken@1495
    62
	int init_sem;
slouken@0
    63
slouken@0
    64
	if(argc < 2) {
slouken@0
    65
		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
icculus@1151
    66
		return(1);
slouken@0
    67
	}
slouken@0
    68
slouken@0
    69
	/* Load the SDL library */
slouken@0
    70
	if ( SDL_Init(0) < 0 ) {
slouken@0
    71
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    72
		return(1);
slouken@0
    73
	}
slouken@0
    74
	signal(SIGTERM, killed);
slouken@0
    75
	signal(SIGINT, killed);
slouken@0
    76
	
slouken@0
    77
	init_sem = atoi(argv[1]);
slouken@0
    78
	sem = SDL_CreateSemaphore(init_sem);
slouken@0
    79
	
slouken@0
    80
	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
slouken@0
    81
	/* Create all the threads */
slouken@0
    82
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    83
		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
slouken@0
    84
	}
slouken@0
    85
slouken@0
    86
	/* Wait 10 seconds */
slouken@0
    87
	SDL_Delay(10 * 1000);
slouken@0
    88
slouken@0
    89
	/* Wait for all threads to finish */
slouken@0
    90
	printf("Waiting for threads to finish\n");
slouken@0
    91
	alive = 0;
slouken@0
    92
	for( i = 0; i < NUM_THREADS; ++i ) {
slouken@0
    93
		SDL_WaitThread(threads[i], NULL);
slouken@0
    94
	}
slouken@0
    95
	printf("Finished waiting for threads\n");
slouken@0
    96
slouken@0
    97
	SDL_DestroySemaphore(sem);
slouken@5105
    98
slouken@5105
    99
	TestWaitTimeout();
slouken@5105
   100
icculus@1151
   101
	SDL_Quit();
slouken@0
   102
	return(0);
slouken@0
   103
}