src/thread/pthread/SDL_syssem.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 17 May 2006 23:42:48 +0000
changeset 1851 536b0704b7d8
parent 1630 eba962f9d603
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Make sure sem_wait didn't return early with EINTR. Fixes Bugzilla #231.
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@1361
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1361
     4
slouken@1361
     5
    This library is free software; you can redistribute it and/or
slouken@1361
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1361
     7
    License as published by the Free Software Foundation; either
slouken@1361
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1361
     9
slouken@1361
    10
    This library is distributed in the hope that it will be useful,
slouken@1361
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1361
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1361
    13
    Lesser General Public License for more details.
slouken@1361
    14
slouken@1361
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1361
    16
    License along with this library; if not, write to the Free Software
slouken@1361
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1361
    18
slouken@1361
    19
    Sam Lantinga
slouken@1361
    20
    slouken@libsdl.org
slouken@1361
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@1361
    23
slouken@1361
    24
#include <pthread.h>
slouken@1361
    25
#include <semaphore.h>
icculus@1851
    26
#include <errno.h>
slouken@1361
    27
slouken@1361
    28
#include "SDL_thread.h"
slouken@1361
    29
#include "SDL_timer.h"
slouken@1361
    30
slouken@1361
    31
/* Wrapper around POSIX 1003.1b semaphores */
slouken@1361
    32
slouken@1402
    33
#ifdef __MACOSX__
slouken@1627
    34
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
slouken@1627
    35
#include "../generic/SDL_syssem.c"
slouken@1627
    36
#else
slouken@1361
    37
slouken@1361
    38
struct SDL_semaphore {
slouken@1630
    39
	sem_t sem;
slouken@1361
    40
};
slouken@1361
    41
slouken@1361
    42
/* Create a semaphore, initialized with value */
slouken@1361
    43
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    44
{
slouken@1361
    45
	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
slouken@1361
    46
	if ( sem ) {
slouken@1630
    47
		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
slouken@1361
    48
			SDL_SetError("sem_init() failed");
slouken@1361
    49
			SDL_free(sem);
slouken@1361
    50
			sem = NULL;
slouken@1361
    51
		}
slouken@1361
    52
	} else {
slouken@1361
    53
		SDL_OutOfMemory();
slouken@1361
    54
	}
slouken@1361
    55
	return sem;
slouken@1361
    56
}
slouken@1361
    57
slouken@1361
    58
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@1361
    59
{
slouken@1361
    60
	if ( sem ) {
slouken@1630
    61
		sem_destroy(&sem->sem);
slouken@1361
    62
		SDL_free(sem);
slouken@1361
    63
	}
slouken@1361
    64
}
slouken@1361
    65
slouken@1361
    66
int SDL_SemTryWait(SDL_sem *sem)
slouken@1361
    67
{
slouken@1361
    68
	int retval;
slouken@1361
    69
slouken@1361
    70
	if ( ! sem ) {
slouken@1361
    71
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    72
		return -1;
slouken@1361
    73
	}
slouken@1361
    74
	retval = SDL_MUTEX_TIMEDOUT;
slouken@1630
    75
	if ( sem_trywait(&sem->sem) == 0 ) {
slouken@1361
    76
		retval = 0;
slouken@1361
    77
	}
slouken@1361
    78
	return retval;
slouken@1361
    79
}
slouken@1361
    80
slouken@1361
    81
int SDL_SemWait(SDL_sem *sem)
slouken@1361
    82
{
slouken@1361
    83
	int retval;
slouken@1361
    84
slouken@1361
    85
	if ( ! sem ) {
slouken@1361
    86
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    87
		return -1;
slouken@1361
    88
	}
slouken@1361
    89
icculus@1851
    90
	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
slouken@1361
    91
	if ( retval < 0 ) {
slouken@1361
    92
		SDL_SetError("sem_wait() failed");
slouken@1361
    93
	}
slouken@1361
    94
	return retval;
slouken@1361
    95
}
slouken@1361
    96
slouken@1361
    97
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@1361
    98
{
slouken@1361
    99
	int retval;
slouken@1361
   100
slouken@1361
   101
	if ( ! sem ) {
slouken@1361
   102
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   103
		return -1;
slouken@1361
   104
	}
slouken@1361
   105
slouken@1361
   106
	/* Try the easy cases first */
slouken@1361
   107
	if ( timeout == 0 ) {
slouken@1361
   108
		return SDL_SemTryWait(sem);
slouken@1361
   109
	}
slouken@1361
   110
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@1361
   111
		return SDL_SemWait(sem);
slouken@1361
   112
	}
slouken@1361
   113
slouken@1361
   114
	/* Ack!  We have to busy wait... */
slouken@1627
   115
	/* FIXME: Use sem_timedwait()? */
slouken@1361
   116
	timeout += SDL_GetTicks();
slouken@1361
   117
	do {
slouken@1361
   118
		retval = SDL_SemTryWait(sem);
slouken@1361
   119
		if ( retval == 0 ) {
slouken@1361
   120
			break;
slouken@1361
   121
		}
slouken@1361
   122
		SDL_Delay(1);
slouken@1361
   123
	} while ( SDL_GetTicks() < timeout );
slouken@1361
   124
slouken@1361
   125
	return retval;
slouken@1361
   126
}
slouken@1361
   127
slouken@1361
   128
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@1361
   129
{
slouken@1361
   130
	int ret = 0;
slouken@1361
   131
	if ( sem ) {
slouken@1630
   132
		sem_getvalue(&sem->sem, &ret);
slouken@1361
   133
		if ( ret < 0 ) {
slouken@1361
   134
			ret = 0;
slouken@1361
   135
		}
slouken@1361
   136
	}
slouken@1361
   137
	return (Uint32)ret;
slouken@1361
   138
}
slouken@1361
   139
slouken@1361
   140
int SDL_SemPost(SDL_sem *sem)
slouken@1361
   141
{
slouken@1361
   142
	int retval;
slouken@1361
   143
slouken@1361
   144
	if ( ! sem ) {
slouken@1361
   145
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   146
		return -1;
slouken@1361
   147
	}
slouken@1361
   148
slouken@1630
   149
	retval = sem_post(&sem->sem);
slouken@1361
   150
	if ( retval < 0 ) {
slouken@1361
   151
		SDL_SetError("sem_post() failed");
slouken@1361
   152
	}
slouken@1361
   153
	return retval;
slouken@1361
   154
}
slouken@1627
   155
slouken@1627
   156
#endif /* __MACOSX__ */