src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Apr 2006 14:27:58 +0000
changeset 1630 eba962f9d603
parent 1627 aee7ea396f59
child 1662 782fd950bd46
child 1851 536b0704b7d8
permissions -rw-r--r--
Fixed compile error
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>
slouken@1361
    26
slouken@1361
    27
#include "SDL_thread.h"
slouken@1361
    28
#include "SDL_timer.h"
slouken@1361
    29
slouken@1361
    30
/* Wrapper around POSIX 1003.1b semaphores */
slouken@1361
    31
slouken@1402
    32
#ifdef __MACOSX__
slouken@1627
    33
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
slouken@1627
    34
#include "../generic/SDL_syssem.c"
slouken@1627
    35
#else
slouken@1361
    36
slouken@1361
    37
struct SDL_semaphore {
slouken@1630
    38
	sem_t sem;
slouken@1361
    39
};
slouken@1361
    40
slouken@1361
    41
/* Create a semaphore, initialized with value */
slouken@1361
    42
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    43
{
slouken@1361
    44
	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
slouken@1361
    45
	if ( sem ) {
slouken@1630
    46
		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
slouken@1361
    47
			SDL_SetError("sem_init() failed");
slouken@1361
    48
			SDL_free(sem);
slouken@1361
    49
			sem = NULL;
slouken@1361
    50
		}
slouken@1361
    51
	} else {
slouken@1361
    52
		SDL_OutOfMemory();
slouken@1361
    53
	}
slouken@1361
    54
	return sem;
slouken@1361
    55
}
slouken@1361
    56
slouken@1361
    57
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@1361
    58
{
slouken@1361
    59
	if ( sem ) {
slouken@1630
    60
		sem_destroy(&sem->sem);
slouken@1361
    61
		SDL_free(sem);
slouken@1361
    62
	}
slouken@1361
    63
}
slouken@1361
    64
slouken@1361
    65
int SDL_SemTryWait(SDL_sem *sem)
slouken@1361
    66
{
slouken@1361
    67
	int retval;
slouken@1361
    68
slouken@1361
    69
	if ( ! sem ) {
slouken@1361
    70
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    71
		return -1;
slouken@1361
    72
	}
slouken@1361
    73
	retval = SDL_MUTEX_TIMEDOUT;
slouken@1630
    74
	if ( sem_trywait(&sem->sem) == 0 ) {
slouken@1361
    75
		retval = 0;
slouken@1361
    76
	}
slouken@1361
    77
	return retval;
slouken@1361
    78
}
slouken@1361
    79
slouken@1361
    80
int SDL_SemWait(SDL_sem *sem)
slouken@1361
    81
{
slouken@1361
    82
	int retval;
slouken@1361
    83
slouken@1361
    84
	if ( ! sem ) {
slouken@1361
    85
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    86
		return -1;
slouken@1361
    87
	}
slouken@1361
    88
slouken@1630
    89
	retval = sem_wait(&sem->sem);
slouken@1361
    90
	if ( retval < 0 ) {
slouken@1361
    91
		SDL_SetError("sem_wait() failed");
slouken@1361
    92
	}
slouken@1361
    93
	return retval;
slouken@1361
    94
}
slouken@1361
    95
slouken@1361
    96
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@1361
    97
{
slouken@1361
    98
	int retval;
slouken@1361
    99
slouken@1361
   100
	if ( ! sem ) {
slouken@1361
   101
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   102
		return -1;
slouken@1361
   103
	}
slouken@1361
   104
slouken@1361
   105
	/* Try the easy cases first */
slouken@1361
   106
	if ( timeout == 0 ) {
slouken@1361
   107
		return SDL_SemTryWait(sem);
slouken@1361
   108
	}
slouken@1361
   109
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@1361
   110
		return SDL_SemWait(sem);
slouken@1361
   111
	}
slouken@1361
   112
slouken@1361
   113
	/* Ack!  We have to busy wait... */
slouken@1627
   114
	/* FIXME: Use sem_timedwait()? */
slouken@1361
   115
	timeout += SDL_GetTicks();
slouken@1361
   116
	do {
slouken@1361
   117
		retval = SDL_SemTryWait(sem);
slouken@1361
   118
		if ( retval == 0 ) {
slouken@1361
   119
			break;
slouken@1361
   120
		}
slouken@1361
   121
		SDL_Delay(1);
slouken@1361
   122
	} while ( SDL_GetTicks() < timeout );
slouken@1361
   123
slouken@1361
   124
	return retval;
slouken@1361
   125
}
slouken@1361
   126
slouken@1361
   127
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@1361
   128
{
slouken@1361
   129
	int ret = 0;
slouken@1361
   130
	if ( sem ) {
slouken@1630
   131
		sem_getvalue(&sem->sem, &ret);
slouken@1361
   132
		if ( ret < 0 ) {
slouken@1361
   133
			ret = 0;
slouken@1361
   134
		}
slouken@1361
   135
	}
slouken@1361
   136
	return (Uint32)ret;
slouken@1361
   137
}
slouken@1361
   138
slouken@1361
   139
int SDL_SemPost(SDL_sem *sem)
slouken@1361
   140
{
slouken@1361
   141
	int retval;
slouken@1361
   142
slouken@1361
   143
	if ( ! sem ) {
slouken@1361
   144
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   145
		return -1;
slouken@1361
   146
	}
slouken@1361
   147
slouken@1630
   148
	retval = sem_post(&sem->sem);
slouken@1361
   149
	if ( retval < 0 ) {
slouken@1361
   150
		SDL_SetError("sem_post() failed");
slouken@1361
   151
	}
slouken@1361
   152
	return retval;
slouken@1361
   153
}
slouken@1627
   154
slouken@1627
   155
#endif /* __MACOSX__ */