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