test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:51:08 -0500
branchSDL-1.2
changeset 6297 c787fb1b5699
parent 5105 99acf3d856cb
permissions -rw-r--r--
Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out deraj 2012-02-19 19:01:08 PST Fix to treat ETIMEDOUT as a time out instead of an error (and update the test)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     2
/* Simple test of the SDL semaphore code */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     3
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
#include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     5
#include <stdlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     6
#include <signal.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     7
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
#include "SDL.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     9
#include "SDL_thread.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    10
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    11
#define NUM_THREADS 10
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    13
static SDL_sem *sem;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
int alive = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    15
1769
290b5baf2fca Fixed bug #215
Sam Lantinga <slouken@libsdl.org>
parents: 1615
diff changeset
    16
int SDLCALL ThreadFunc(void *data)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    17
{
1615
d5298e8f22b3 Ugh, more 64-bit cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 1495
diff changeset
    18
	int threadnum = (int)(uintptr_t)data;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
	while ( alive ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
		SDL_SemWait(sem);
1495
1dd8bf30a109 Might have fixed 64-bit issues. :)
Sam Lantinga <slouken@libsdl.org>
parents: 1439
diff changeset
    21
		fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
		SDL_Delay(200);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
		SDL_SemPost(sem);
1495
1dd8bf30a109 Might have fixed 64-bit issues. :)
Sam Lantinga <slouken@libsdl.org>
parents: 1439
diff changeset
    24
		fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
		SDL_Delay(1); /* For the scheduler */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
	}
1495
1dd8bf30a109 Might have fixed 64-bit issues. :)
Sam Lantinga <slouken@libsdl.org>
parents: 1439
diff changeset
    27
	printf("Thread number %d exiting.\n", threadnum);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
	return 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
static void killed(int sig)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
	alive = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
5105
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    36
static void TestWaitTimeout(void)
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    37
{
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    38
	Uint32 start_ticks;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    39
	Uint32 end_ticks;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    40
	Uint32 duration;
6297
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 5105
diff changeset
    41
	int retval;
5105
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    42
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    43
	sem = SDL_CreateSemaphore(0);
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    44
	printf("Waiting 2 seconds on semaphore\n");
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    45
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    46
	start_ticks = SDL_GetTicks();
6297
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 5105
diff changeset
    47
	retval = SDL_SemWaitTimeout(sem, 2000);
5105
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    48
	end_ticks = SDL_GetTicks();
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    49
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    50
	duration = end_ticks - start_ticks;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    51
	
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    52
	/* Accept a little offset in the effective wait */
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    53
	if (duration > 1900 && duration < 2050)
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    54
		printf("Wait done.\n");
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    55
	else
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    56
		fprintf(stderr, "Wait took %d milliseconds\n", duration);
6297
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 5105
diff changeset
    57
	
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 5105
diff changeset
    58
	/* Check to make sure the return value indicates timed out */
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 5105
diff changeset
    59
	if (retval != SDL_MUTEX_TIMEDOUT) 
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 5105
diff changeset
    60
		fprintf(stderr, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
5105
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    61
}
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    62
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
int main(int argc, char **argv)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    65
	SDL_Thread *threads[NUM_THREADS];
1495
1dd8bf30a109 Might have fixed 64-bit issues. :)
Sam Lantinga <slouken@libsdl.org>
parents: 1439
diff changeset
    66
	uintptr_t i;
1dd8bf30a109 Might have fixed 64-bit issues. :)
Sam Lantinga <slouken@libsdl.org>
parents: 1439
diff changeset
    67
	int init_sem;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    68
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    69
	if(argc < 2) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    71
		return(1);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    72
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    74
	/* Load the SDL library */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
	if ( SDL_Init(0) < 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    76
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    77
		return(1);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    78
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    79
	signal(SIGTERM, killed);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    80
	signal(SIGINT, killed);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
	
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    82
	init_sem = atoi(argv[1]);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    83
	sem = SDL_CreateSemaphore(init_sem);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    84
	
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    85
	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    86
	/* Create all the threads */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    87
	for( i = 0; i < NUM_THREADS; ++i ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    88
		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    89
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    90
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    91
	/* Wait 10 seconds */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    92
	SDL_Delay(10 * 1000);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    93
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    94
	/* Wait for all threads to finish */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    95
	printf("Waiting for threads to finish\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    96
	alive = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    97
	for( i = 0; i < NUM_THREADS; ++i ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    98
		SDL_WaitThread(threads[i], NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    99
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   100
	printf("Finished waiting for threads\n");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   101
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   102
	SDL_DestroySemaphore(sem);
5105
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   103
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   104
	TestWaitTimeout();
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   105
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   106
	SDL_Quit();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   107
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   108
}