Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:51:08 -0500
branchSDL-1.2
changeset 6297c787fb1b5699
parent 6294 7a2e0f7b30cb
child 6306 2b923729fd01
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)
src/thread/pthread/SDL_syssem.c
test/testsem.c
     1.1 --- a/src/thread/pthread/SDL_syssem.c	Mon Feb 20 20:55:23 2012 -0500
     1.2 +++ b/src/thread/pthread/SDL_syssem.c	Mon Feb 20 23:51:08 2012 -0500
     1.3 @@ -144,8 +144,14 @@
     1.4  		retval = sem_timedwait(&sem->sem, &ts_timeout);
     1.5  	while (retval == -1 && errno == EINTR);
     1.6  
     1.7 -	if (retval == -1)
     1.8 -		SDL_SetError(strerror(errno));
     1.9 +	if (retval == -1) {
    1.10 +		if (errno == ETIMEDOUT) {
    1.11 +			retval = SDL_MUTEX_TIMEDOUT;
    1.12 +		}
    1.13 +		else {
    1.14 +			SDL_SetError(strerror(errno));
    1.15 +		}
    1.16 +	}
    1.17  #else
    1.18  	end = SDL_GetTicks() + timeout;
    1.19  	while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
     2.1 --- a/test/testsem.c	Mon Feb 20 20:55:23 2012 -0500
     2.2 +++ b/test/testsem.c	Mon Feb 20 23:51:08 2012 -0500
     2.3 @@ -38,12 +38,13 @@
     2.4  	Uint32 start_ticks;
     2.5  	Uint32 end_ticks;
     2.6  	Uint32 duration;
     2.7 +	int retval;
     2.8  
     2.9  	sem = SDL_CreateSemaphore(0);
    2.10  	printf("Waiting 2 seconds on semaphore\n");
    2.11  
    2.12  	start_ticks = SDL_GetTicks();
    2.13 -	SDL_SemWaitTimeout(sem, 2000);
    2.14 +	retval = SDL_SemWaitTimeout(sem, 2000);
    2.15  	end_ticks = SDL_GetTicks();
    2.16  
    2.17  	duration = end_ticks - start_ticks;
    2.18 @@ -53,6 +54,10 @@
    2.19  		printf("Wait done.\n");
    2.20  	else
    2.21  		fprintf(stderr, "Wait took %d milliseconds\n", duration);
    2.22 +	
    2.23 +	/* Check to make sure the return value indicates timed out */
    2.24 +	if (retval != SDL_MUTEX_TIMEDOUT) 
    2.25 +		fprintf(stderr, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
    2.26  }
    2.27  
    2.28  int main(int argc, char **argv)