src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1361 19418e4422cb
child 1487 dc6b59e925a2
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
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@1361
    33
#define USE_NAMED_SEMAPHORES 1
slouken@1402
    34
#endif
slouken@1361
    35
slouken@1361
    36
struct SDL_semaphore {
slouken@1361
    37
	sem_t *sem;
slouken@1361
    38
#if !USE_NAMED_SEMAPHORES
slouken@1361
    39
	sem_t sem_data;
slouken@1361
    40
#endif
slouken@1361
    41
};
slouken@1361
    42
slouken@1361
    43
/* Create a semaphore, initialized with value */
slouken@1361
    44
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    45
{
slouken@1361
    46
	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
slouken@1361
    47
	if ( sem ) {
slouken@1361
    48
#if USE_NAMED_SEMAPHORES
slouken@1361
    49
		static int semnum = 0;
slouken@1361
    50
		char name[32];
slouken@1361
    51
slouken@1361
    52
		SDL_snprintf(name, SDL_arraysize(name), "/SDL_sem-%d-%4.4d", getpid(), semnum++);
slouken@1361
    53
		sem->sem = sem_open(name, O_CREAT, 0600, initial_value);
slouken@1361
    54
		if ( sem->sem == (sem_t *)SEM_FAILED ) {
slouken@1361
    55
			SDL_SetError("sem_open(%s) failed", name);
slouken@1361
    56
			SDL_free(sem);
slouken@1361
    57
			sem = NULL;
slouken@1361
    58
		} else {
slouken@1361
    59
			sem_unlink(name);
slouken@1361
    60
		}
slouken@1361
    61
#else
slouken@1361
    62
		if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
slouken@1361
    63
			SDL_SetError("sem_init() failed");
slouken@1361
    64
			SDL_free(sem);
slouken@1361
    65
			sem = NULL;
slouken@1361
    66
		} else {
slouken@1361
    67
			sem->sem = &sem->sem_data;
slouken@1361
    68
		}
slouken@1361
    69
#endif /* USE_NAMED_SEMAPHORES */
slouken@1361
    70
	} else {
slouken@1361
    71
		SDL_OutOfMemory();
slouken@1361
    72
	}
slouken@1361
    73
	return sem;
slouken@1361
    74
}
slouken@1361
    75
slouken@1361
    76
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@1361
    77
{
slouken@1361
    78
	if ( sem ) {
slouken@1361
    79
#if USE_NAMED_SEMAPHORES
slouken@1361
    80
		sem_close(sem->sem);
slouken@1361
    81
#else
slouken@1361
    82
		sem_destroy(sem->sem);
slouken@1361
    83
#endif
slouken@1361
    84
		SDL_free(sem);
slouken@1361
    85
	}
slouken@1361
    86
}
slouken@1361
    87
slouken@1361
    88
int SDL_SemTryWait(SDL_sem *sem)
slouken@1361
    89
{
slouken@1361
    90
	int retval;
slouken@1361
    91
slouken@1361
    92
	if ( ! sem ) {
slouken@1361
    93
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    94
		return -1;
slouken@1361
    95
	}
slouken@1361
    96
	retval = SDL_MUTEX_TIMEDOUT;
slouken@1361
    97
	if ( sem_trywait(sem->sem) == 0 ) {
slouken@1361
    98
		retval = 0;
slouken@1361
    99
	}
slouken@1361
   100
	return retval;
slouken@1361
   101
}
slouken@1361
   102
slouken@1361
   103
int SDL_SemWait(SDL_sem *sem)
slouken@1361
   104
{
slouken@1361
   105
	int retval;
slouken@1361
   106
slouken@1361
   107
	if ( ! sem ) {
slouken@1361
   108
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   109
		return -1;
slouken@1361
   110
	}
slouken@1361
   111
slouken@1361
   112
	retval = sem_wait(sem->sem);
slouken@1361
   113
	if ( retval < 0 ) {
slouken@1361
   114
		SDL_SetError("sem_wait() failed");
slouken@1361
   115
	}
slouken@1361
   116
	return retval;
slouken@1361
   117
}
slouken@1361
   118
slouken@1361
   119
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@1361
   120
{
slouken@1361
   121
	int retval;
slouken@1361
   122
slouken@1361
   123
	if ( ! sem ) {
slouken@1361
   124
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   125
		return -1;
slouken@1361
   126
	}
slouken@1361
   127
slouken@1361
   128
	/* Try the easy cases first */
slouken@1361
   129
	if ( timeout == 0 ) {
slouken@1361
   130
		return SDL_SemTryWait(sem);
slouken@1361
   131
	}
slouken@1361
   132
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@1361
   133
		return SDL_SemWait(sem);
slouken@1361
   134
	}
slouken@1361
   135
slouken@1361
   136
	/* Ack!  We have to busy wait... */
slouken@1361
   137
	timeout += SDL_GetTicks();
slouken@1361
   138
	do {
slouken@1361
   139
		retval = SDL_SemTryWait(sem);
slouken@1361
   140
		if ( retval == 0 ) {
slouken@1361
   141
			break;
slouken@1361
   142
		}
slouken@1361
   143
		SDL_Delay(1);
slouken@1361
   144
	} while ( SDL_GetTicks() < timeout );
slouken@1361
   145
slouken@1361
   146
	return retval;
slouken@1361
   147
}
slouken@1361
   148
slouken@1361
   149
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@1361
   150
{
slouken@1361
   151
	int ret = 0;
slouken@1361
   152
	if ( sem ) {
slouken@1361
   153
		sem_getvalue(sem->sem, &ret);
slouken@1361
   154
		if ( ret < 0 ) {
slouken@1361
   155
			ret = 0;
slouken@1361
   156
		}
slouken@1361
   157
	}
slouken@1361
   158
	return (Uint32)ret;
slouken@1361
   159
}
slouken@1361
   160
slouken@1361
   161
int SDL_SemPost(SDL_sem *sem)
slouken@1361
   162
{
slouken@1361
   163
	int retval;
slouken@1361
   164
slouken@1361
   165
	if ( ! sem ) {
slouken@1361
   166
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   167
		return -1;
slouken@1361
   168
	}
slouken@1361
   169
slouken@1361
   170
	retval = sem_post(sem->sem);
slouken@1361
   171
	if ( retval < 0 ) {
slouken@1361
   172
		SDL_SetError("sem_post() failed");
slouken@1361
   173
	}
slouken@1361
   174
	return retval;
slouken@1361
   175
}