src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:51:08 -0500
branchSDL-1.2
changeset 6297 c787fb1b5699
parent 6218 f114f1434ff6
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     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2012 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include <pthread.h>
    25 #include <semaphore.h>
    26 #include <errno.h>
    27 #include <sys/time.h>
    28 
    29 #include "SDL_thread.h"
    30 #include "SDL_timer.h"
    31 
    32 /* Wrapper around POSIX 1003.1b semaphores */
    33 
    34 #ifdef __MACOSX__
    35 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
    36 #include "../generic/SDL_syssem.c"
    37 #else
    38 
    39 struct SDL_semaphore {
    40 	sem_t sem;
    41 };
    42 
    43 /* Create a semaphore, initialized with value */
    44 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    45 {
    46 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    47 	if ( sem ) {
    48 		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
    49 			SDL_SetError("sem_init() failed");
    50 			SDL_free(sem);
    51 			sem = NULL;
    52 		}
    53 	} else {
    54 		SDL_OutOfMemory();
    55 	}
    56 	return sem;
    57 }
    58 
    59 void SDL_DestroySemaphore(SDL_sem *sem)
    60 {
    61 	if ( sem ) {
    62 		sem_destroy(&sem->sem);
    63 		SDL_free(sem);
    64 	}
    65 }
    66 
    67 int SDL_SemTryWait(SDL_sem *sem)
    68 {
    69 	int retval;
    70 
    71 	if ( ! sem ) {
    72 		SDL_SetError("Passed a NULL semaphore");
    73 		return -1;
    74 	}
    75 	retval = SDL_MUTEX_TIMEDOUT;
    76 	if ( sem_trywait(&sem->sem) == 0 ) {
    77 		retval = 0;
    78 	}
    79 	return retval;
    80 }
    81 
    82 int SDL_SemWait(SDL_sem *sem)
    83 {
    84 	int retval;
    85 
    86 	if ( ! sem ) {
    87 		SDL_SetError("Passed a NULL semaphore");
    88 		return -1;
    89 	}
    90 
    91 	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
    92 	if ( retval < 0 ) {
    93 		SDL_SetError("sem_wait() failed");
    94 	}
    95 	return retval;
    96 }
    97 
    98 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
    99 {
   100 	int retval;
   101 #ifdef HAVE_SEM_TIMEDWAIT
   102 	struct timeval now;
   103 	struct timespec ts_timeout;
   104 #else
   105 	Uint32 end;
   106 #endif
   107 
   108 	if ( ! sem ) {
   109 		SDL_SetError("Passed a NULL semaphore");
   110 		return -1;
   111 	}
   112 
   113 	/* Try the easy cases first */
   114 	if ( timeout == 0 ) {
   115 		return SDL_SemTryWait(sem);
   116 	}
   117 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   118 		return SDL_SemWait(sem);
   119 	}
   120 
   121 #ifdef HAVE_SEM_TIMEDWAIT
   122 	/* Setup the timeout. sem_timedwait doesn't wait for
   123 	 * a lapse of time, but until we reach a certain time.
   124 	 * This time is now plus the timeout.
   125 	 */
   126 	gettimeofday(&now, NULL);
   127 
   128 	/* Add our timeout to current time */
   129 	now.tv_usec += (timeout % 1000) * 1000;
   130 	now.tv_sec += timeout / 1000;
   131 
   132 	/* Wrap the second if needed */
   133 	if ( now.tv_usec >= 1000000 ) {
   134 		now.tv_usec -= 1000000;
   135 		now.tv_sec ++;
   136 	}
   137 
   138 	/* Convert to timespec */
   139 	ts_timeout.tv_sec = now.tv_sec;
   140 	ts_timeout.tv_nsec = now.tv_usec * 1000;
   141 
   142 	/* Wait. */
   143 	do
   144 		retval = sem_timedwait(&sem->sem, &ts_timeout);
   145 	while (retval == -1 && errno == EINTR);
   146 
   147 	if (retval == -1) {
   148 		if (errno == ETIMEDOUT) {
   149 			retval = SDL_MUTEX_TIMEDOUT;
   150 		}
   151 		else {
   152 			SDL_SetError(strerror(errno));
   153 		}
   154 	}
   155 #else
   156 	end = SDL_GetTicks() + timeout;
   157 	while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
   158 		if ((SDL_GetTicks() - end) >= 0) {
   159 			break;
   160 		}
   161 		SDL_Delay(0);
   162 	}
   163 #endif /* HAVE_SEM_TIMEDWAIT */
   164 
   165 	return retval;
   166 }
   167 
   168 Uint32 SDL_SemValue(SDL_sem *sem)
   169 {
   170 	int ret = 0;
   171 	if ( sem ) {
   172 		sem_getvalue(&sem->sem, &ret);
   173 		if ( ret < 0 ) {
   174 			ret = 0;
   175 		}
   176 	}
   177 	return (Uint32)ret;
   178 }
   179 
   180 int SDL_SemPost(SDL_sem *sem)
   181 {
   182 	int retval;
   183 
   184 	if ( ! sem ) {
   185 		SDL_SetError("Passed a NULL semaphore");
   186 		return -1;
   187 	}
   188 
   189 	retval = sem_post(&sem->sem);
   190 	if ( retval < 0 ) {
   191 		SDL_SetError("sem_post() failed");
   192 	}
   193 	return retval;
   194 }
   195 
   196 #endif /* __MACOSX__ */