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)
     1 
     2 /* Simple test of the SDL semaphore code */
     3 
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 #include <signal.h>
     7 
     8 #include "SDL.h"
     9 #include "SDL_thread.h"
    10 
    11 #define NUM_THREADS 10
    12 
    13 static SDL_sem *sem;
    14 int alive = 1;
    15 
    16 int SDLCALL ThreadFunc(void *data)
    17 {
    18 	int threadnum = (int)(uintptr_t)data;
    19 	while ( alive ) {
    20 		SDL_SemWait(sem);
    21 		fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
    22 		SDL_Delay(200);
    23 		SDL_SemPost(sem);
    24 		fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
    25 		SDL_Delay(1); /* For the scheduler */
    26 	}
    27 	printf("Thread number %d exiting.\n", threadnum);
    28 	return 0;
    29 }
    30 
    31 static void killed(int sig)
    32 {
    33 	alive = 0;
    34 }
    35 
    36 static void TestWaitTimeout(void)
    37 {
    38 	Uint32 start_ticks;
    39 	Uint32 end_ticks;
    40 	Uint32 duration;
    41 	int retval;
    42 
    43 	sem = SDL_CreateSemaphore(0);
    44 	printf("Waiting 2 seconds on semaphore\n");
    45 
    46 	start_ticks = SDL_GetTicks();
    47 	retval = SDL_SemWaitTimeout(sem, 2000);
    48 	end_ticks = SDL_GetTicks();
    49 
    50 	duration = end_ticks - start_ticks;
    51 	
    52 	/* Accept a little offset in the effective wait */
    53 	if (duration > 1900 && duration < 2050)
    54 		printf("Wait done.\n");
    55 	else
    56 		fprintf(stderr, "Wait took %d milliseconds\n", duration);
    57 	
    58 	/* Check to make sure the return value indicates timed out */
    59 	if (retval != SDL_MUTEX_TIMEDOUT) 
    60 		fprintf(stderr, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
    61 }
    62 
    63 int main(int argc, char **argv)
    64 {
    65 	SDL_Thread *threads[NUM_THREADS];
    66 	uintptr_t i;
    67 	int init_sem;
    68 
    69 	if(argc < 2) {
    70 		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
    71 		return(1);
    72 	}
    73 
    74 	/* Load the SDL library */
    75 	if ( SDL_Init(0) < 0 ) {
    76 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    77 		return(1);
    78 	}
    79 	signal(SIGTERM, killed);
    80 	signal(SIGINT, killed);
    81 	
    82 	init_sem = atoi(argv[1]);
    83 	sem = SDL_CreateSemaphore(init_sem);
    84 	
    85 	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
    86 	/* Create all the threads */
    87 	for( i = 0; i < NUM_THREADS; ++i ) {
    88 		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
    89 	}
    90 
    91 	/* Wait 10 seconds */
    92 	SDL_Delay(10 * 1000);
    93 
    94 	/* Wait for all threads to finish */
    95 	printf("Waiting for threads to finish\n");
    96 	alive = 0;
    97 	for( i = 0; i < NUM_THREADS; ++i ) {
    98 		SDL_WaitThread(threads[i], NULL);
    99 	}
   100 	printf("Finished waiting for threads\n");
   101 
   102 	SDL_DestroySemaphore(sem);
   103 
   104 	TestWaitTimeout();
   105 
   106 	SDL_Quit();
   107 	return(0);
   108 }