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.
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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>
icculus@5985
    27
#include <sys/time.h>
slouken@1361
    28
slouken@1361
    29
#include "SDL_thread.h"
slouken@1361
    30
#include "SDL_timer.h"
slouken@1361
    31
slouken@1361
    32
/* Wrapper around POSIX 1003.1b semaphores */
slouken@1361
    33
slouken@1402
    34
#ifdef __MACOSX__
slouken@1627
    35
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
slouken@1627
    36
#include "../generic/SDL_syssem.c"
slouken@1627
    37
#else
slouken@1361
    38
slouken@1361
    39
struct SDL_semaphore {
slouken@1630
    40
	sem_t sem;
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@1630
    48
		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
slouken@1361
    49
			SDL_SetError("sem_init() failed");
slouken@1361
    50
			SDL_free(sem);
slouken@1361
    51
			sem = NULL;
slouken@1361
    52
		}
slouken@1361
    53
	} else {
slouken@1361
    54
		SDL_OutOfMemory();
slouken@1361
    55
	}
slouken@1361
    56
	return sem;
slouken@1361
    57
}
slouken@1361
    58
slouken@1361
    59
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@1361
    60
{
slouken@1361
    61
	if ( sem ) {
slouken@1630
    62
		sem_destroy(&sem->sem);
slouken@1361
    63
		SDL_free(sem);
slouken@1361
    64
	}
slouken@1361
    65
}
slouken@1361
    66
slouken@1361
    67
int SDL_SemTryWait(SDL_sem *sem)
slouken@1361
    68
{
slouken@1361
    69
	int retval;
slouken@1361
    70
slouken@1361
    71
	if ( ! sem ) {
slouken@1361
    72
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    73
		return -1;
slouken@1361
    74
	}
slouken@1361
    75
	retval = SDL_MUTEX_TIMEDOUT;
slouken@1630
    76
	if ( sem_trywait(&sem->sem) == 0 ) {
slouken@1361
    77
		retval = 0;
slouken@1361
    78
	}
slouken@1361
    79
	return retval;
slouken@1361
    80
}
slouken@1361
    81
slouken@1361
    82
int SDL_SemWait(SDL_sem *sem)
slouken@1361
    83
{
slouken@1361
    84
	int retval;
slouken@1361
    85
slouken@1361
    86
	if ( ! sem ) {
slouken@1361
    87
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    88
		return -1;
slouken@1361
    89
	}
slouken@1361
    90
icculus@1851
    91
	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
slouken@1361
    92
	if ( retval < 0 ) {
slouken@1361
    93
		SDL_SetError("sem_wait() failed");
slouken@1361
    94
	}
slouken@1361
    95
	return retval;
slouken@1361
    96
}
slouken@1361
    97
slouken@1361
    98
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@1361
    99
{
slouken@1361
   100
	int retval;
slouken@5105
   101
	struct timeval now;
slouken@5105
   102
	struct timespec ts_timeout;
slouken@1361
   103
slouken@1361
   104
	if ( ! sem ) {
slouken@1361
   105
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   106
		return -1;
slouken@1361
   107
	}
slouken@1361
   108
slouken@1361
   109
	/* Try the easy cases first */
slouken@1361
   110
	if ( timeout == 0 ) {
slouken@1361
   111
		return SDL_SemTryWait(sem);
slouken@1361
   112
	}
slouken@1361
   113
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@1361
   114
		return SDL_SemWait(sem);
slouken@1361
   115
	}
slouken@1361
   116
slouken@5105
   117
	/* Setup the timeout. sem_timedwait doesn't wait for
slouken@5105
   118
	 * a lapse of time, but until we reach a certain time.
slouken@5105
   119
	 * This time is now plus the timeout.
slouken@5105
   120
	 */
slouken@5105
   121
	gettimeofday(&now, NULL);
slouken@5105
   122
slouken@5105
   123
	/* Add our timeout to current time */
slouken@5105
   124
	now.tv_usec += (timeout % 1000) * 1000;
slouken@5105
   125
	now.tv_sec += timeout / 1000;
slouken@5105
   126
slouken@5105
   127
	/* Wrap the second if needed */
slouken@5105
   128
	if ( now.tv_usec >= 1000000 ) {
slouken@5105
   129
		now.tv_usec -= 1000000;
slouken@5105
   130
		now.tv_sec ++;
slouken@5105
   131
	}
slouken@5105
   132
slouken@5105
   133
	/* Convert to timespec */
slouken@5105
   134
	ts_timeout.tv_sec = now.tv_sec;
slouken@5105
   135
	ts_timeout.tv_nsec = now.tv_usec * 1000;
slouken@5105
   136
slouken@5105
   137
	/* Wait. */
slouken@5105
   138
	do
slouken@5105
   139
		retval = sem_timedwait(&sem->sem, &ts_timeout);
slouken@5105
   140
	while (retval == -1 && errno == EINTR);
slouken@5105
   141
slouken@5105
   142
	if (retval == -1)
slouken@5105
   143
		SDL_SetError(strerror(errno));
slouken@1361
   144
slouken@1361
   145
	return retval;
slouken@1361
   146
}
slouken@1361
   147
slouken@1361
   148
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@1361
   149
{
slouken@1361
   150
	int ret = 0;
slouken@1361
   151
	if ( sem ) {
slouken@1630
   152
		sem_getvalue(&sem->sem, &ret);
slouken@1361
   153
		if ( ret < 0 ) {
slouken@1361
   154
			ret = 0;
slouken@1361
   155
		}
slouken@1361
   156
	}
slouken@1361
   157
	return (Uint32)ret;
slouken@1361
   158
}
slouken@1361
   159
slouken@1361
   160
int SDL_SemPost(SDL_sem *sem)
slouken@1361
   161
{
slouken@1361
   162
	int retval;
slouken@1361
   163
slouken@1361
   164
	if ( ! sem ) {
slouken@1361
   165
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   166
		return -1;
slouken@1361
   167
	}
slouken@1361
   168
slouken@1630
   169
	retval = sem_post(&sem->sem);
slouken@1361
   170
	if ( retval < 0 ) {
slouken@1361
   171
		SDL_SetError("sem_post() failed");
slouken@1361
   172
	}
slouken@1361
   173
	return retval;
slouken@1361
   174
}
slouken@1627
   175
slouken@1627
   176
#endif /* __MACOSX__ */