src/thread/pthread/SDL_syssem.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Oct 2011 12:29:03 -0400
branchSDL-1.2
changeset 5985 f617e1b7a27f
parent 5105 99acf3d856cb
child 6137 4720145f848b
permissions -rw-r--r--
Cleaned up more compiler warnings in the 1.2 branch.
     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 #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 	struct timeval now;
   102 	struct timespec ts_timeout;
   103 
   104 	if ( ! sem ) {
   105 		SDL_SetError("Passed a NULL semaphore");
   106 		return -1;
   107 	}
   108 
   109 	/* Try the easy cases first */
   110 	if ( timeout == 0 ) {
   111 		return SDL_SemTryWait(sem);
   112 	}
   113 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   114 		return SDL_SemWait(sem);
   115 	}
   116 
   117 	/* Setup the timeout. sem_timedwait doesn't wait for
   118 	 * a lapse of time, but until we reach a certain time.
   119 	 * This time is now plus the timeout.
   120 	 */
   121 	gettimeofday(&now, NULL);
   122 
   123 	/* Add our timeout to current time */
   124 	now.tv_usec += (timeout % 1000) * 1000;
   125 	now.tv_sec += timeout / 1000;
   126 
   127 	/* Wrap the second if needed */
   128 	if ( now.tv_usec >= 1000000 ) {
   129 		now.tv_usec -= 1000000;
   130 		now.tv_sec ++;
   131 	}
   132 
   133 	/* Convert to timespec */
   134 	ts_timeout.tv_sec = now.tv_sec;
   135 	ts_timeout.tv_nsec = now.tv_usec * 1000;
   136 
   137 	/* Wait. */
   138 	do
   139 		retval = sem_timedwait(&sem->sem, &ts_timeout);
   140 	while (retval == -1 && errno == EINTR);
   141 
   142 	if (retval == -1)
   143 		SDL_SetError(strerror(errno));
   144 
   145 	return retval;
   146 }
   147 
   148 Uint32 SDL_SemValue(SDL_sem *sem)
   149 {
   150 	int ret = 0;
   151 	if ( sem ) {
   152 		sem_getvalue(&sem->sem, &ret);
   153 		if ( ret < 0 ) {
   154 			ret = 0;
   155 		}
   156 	}
   157 	return (Uint32)ret;
   158 }
   159 
   160 int SDL_SemPost(SDL_sem *sem)
   161 {
   162 	int retval;
   163 
   164 	if ( ! sem ) {
   165 		SDL_SetError("Passed a NULL semaphore");
   166 		return -1;
   167 	}
   168 
   169 	retval = sem_post(&sem->sem);
   170 	if ( retval < 0 ) {
   171 		SDL_SetError("sem_post() failed");
   172 	}
   173 	return retval;
   174 }
   175 
   176 #endif /* __MACOSX__ */