src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 27 Jan 2011 00:33:26 -0800
branchSDL-1.2
changeset 5105 99acf3d856cb
parent 4159 a1b03ba2fcd0
child 5985 f617e1b7a27f
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
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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 
    28 #include "SDL_thread.h"
    29 #include "SDL_timer.h"
    30 
    31 /* Wrapper around POSIX 1003.1b semaphores */
    32 
    33 #ifdef __MACOSX__
    34 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
    35 #include "../generic/SDL_syssem.c"
    36 #else
    37 
    38 struct SDL_semaphore {
    39 	sem_t sem;
    40 };
    41 
    42 /* Create a semaphore, initialized with value */
    43 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    44 {
    45 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    46 	if ( sem ) {
    47 		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
    48 			SDL_SetError("sem_init() failed");
    49 			SDL_free(sem);
    50 			sem = NULL;
    51 		}
    52 	} else {
    53 		SDL_OutOfMemory();
    54 	}
    55 	return sem;
    56 }
    57 
    58 void SDL_DestroySemaphore(SDL_sem *sem)
    59 {
    60 	if ( sem ) {
    61 		sem_destroy(&sem->sem);
    62 		SDL_free(sem);
    63 	}
    64 }
    65 
    66 int SDL_SemTryWait(SDL_sem *sem)
    67 {
    68 	int retval;
    69 
    70 	if ( ! sem ) {
    71 		SDL_SetError("Passed a NULL semaphore");
    72 		return -1;
    73 	}
    74 	retval = SDL_MUTEX_TIMEDOUT;
    75 	if ( sem_trywait(&sem->sem) == 0 ) {
    76 		retval = 0;
    77 	}
    78 	return retval;
    79 }
    80 
    81 int SDL_SemWait(SDL_sem *sem)
    82 {
    83 	int retval;
    84 
    85 	if ( ! sem ) {
    86 		SDL_SetError("Passed a NULL semaphore");
    87 		return -1;
    88 	}
    89 
    90 	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
    91 	if ( retval < 0 ) {
    92 		SDL_SetError("sem_wait() failed");
    93 	}
    94 	return retval;
    95 }
    96 
    97 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
    98 {
    99 	int retval;
   100 	struct timeval now;
   101 	struct timespec ts_timeout;
   102 
   103 	if ( ! sem ) {
   104 		SDL_SetError("Passed a NULL semaphore");
   105 		return -1;
   106 	}
   107 
   108 	/* Try the easy cases first */
   109 	if ( timeout == 0 ) {
   110 		return SDL_SemTryWait(sem);
   111 	}
   112 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   113 		return SDL_SemWait(sem);
   114 	}
   115 
   116 	/* Setup the timeout. sem_timedwait doesn't wait for
   117 	 * a lapse of time, but until we reach a certain time.
   118 	 * This time is now plus the timeout.
   119 	 */
   120 	gettimeofday(&now, NULL);
   121 
   122 	/* Add our timeout to current time */
   123 	now.tv_usec += (timeout % 1000) * 1000;
   124 	now.tv_sec += timeout / 1000;
   125 
   126 	/* Wrap the second if needed */
   127 	if ( now.tv_usec >= 1000000 ) {
   128 		now.tv_usec -= 1000000;
   129 		now.tv_sec ++;
   130 	}
   131 
   132 	/* Convert to timespec */
   133 	ts_timeout.tv_sec = now.tv_sec;
   134 	ts_timeout.tv_nsec = now.tv_usec * 1000;
   135 
   136 	/* Wait. */
   137 	do
   138 		retval = sem_timedwait(&sem->sem, &ts_timeout);
   139 	while (retval == -1 && errno == EINTR);
   140 
   141 	if (retval == -1)
   142 		SDL_SetError(strerror(errno));
   143 
   144 	return retval;
   145 }
   146 
   147 Uint32 SDL_SemValue(SDL_sem *sem)
   148 {
   149 	int ret = 0;
   150 	if ( sem ) {
   151 		sem_getvalue(&sem->sem, &ret);
   152 		if ( ret < 0 ) {
   153 			ret = 0;
   154 		}
   155 	}
   156 	return (Uint32)ret;
   157 }
   158 
   159 int SDL_SemPost(SDL_sem *sem)
   160 {
   161 	int retval;
   162 
   163 	if ( ! sem ) {
   164 		SDL_SetError("Passed a NULL semaphore");
   165 		return -1;
   166 	}
   167 
   168 	retval = sem_post(&sem->sem);
   169 	if ( retval < 0 ) {
   170 		SDL_SetError("sem_post() failed");
   171 	}
   172 	return retval;
   173 }
   174 
   175 #endif /* __MACOSX__ */