src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:51:08 -0500
branchSDL-1.2
changeset 6297 c787fb1b5699
parent 6218 f114f1434ff6
permissions -rw-r--r--
Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out deraj 2012-02-19 19:01:08 PST Fix to treat ETIMEDOUT as a time out instead of an error (and update the test)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
6137
4720145f848b Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 5985
diff changeset
     3
    Copyright (C) 1997-2012 Sam Lantinga
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
    Lesser General Public License for more details.
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
    Sam Lantinga
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
    slouken@libsdl.org
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    22
#include "SDL_config.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#include <pthread.h>
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include <semaphore.h>
1851
536b0704b7d8 Make sure sem_wait didn't return early with EINTR. Fixes Bugzilla #231.
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
    26
#include <errno.h>
5985
f617e1b7a27f Cleaned up more compiler warnings in the 1.2 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5105
diff changeset
    27
#include <sys/time.h>
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#include "SDL_thread.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#include "SDL_timer.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
/* Wrapper around POSIX 1003.1b semaphores */
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    34
#ifdef __MACOSX__
1627
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
    35
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
    36
#include "../generic/SDL_syssem.c"
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
    37
#else
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
struct SDL_semaphore {
1630
eba962f9d603 Fixed compile error
Sam Lantinga <slouken@libsdl.org>
parents: 1627
diff changeset
    40
	sem_t sem;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
};
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
/* Create a semaphore, initialized with value */
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
	if ( sem ) {
1630
eba962f9d603 Fixed compile error
Sam Lantinga <slouken@libsdl.org>
parents: 1627
diff changeset
    48
		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
			SDL_SetError("sem_init() failed");
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
			SDL_free(sem);
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
			sem = NULL;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
		}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
	} else {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
		SDL_OutOfMemory();
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
	return sem;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
void SDL_DestroySemaphore(SDL_sem *sem)
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
	if ( sem ) {
1630
eba962f9d603 Fixed compile error
Sam Lantinga <slouken@libsdl.org>
parents: 1627
diff changeset
    62
		sem_destroy(&sem->sem);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
		SDL_free(sem);
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
int SDL_SemTryWait(SDL_sem *sem)
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
	int retval;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
	if ( ! sem ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
		SDL_SetError("Passed a NULL semaphore");
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
		return -1;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
	retval = SDL_MUTEX_TIMEDOUT;
1630
eba962f9d603 Fixed compile error
Sam Lantinga <slouken@libsdl.org>
parents: 1627
diff changeset
    76
	if ( sem_trywait(&sem->sem) == 0 ) {
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
		retval = 0;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
	return retval;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
int SDL_SemWait(SDL_sem *sem)
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
	int retval;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
	if ( ! sem ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
		SDL_SetError("Passed a NULL semaphore");
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
		return -1;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
1851
536b0704b7d8 Make sure sem_wait didn't return early with EINTR. Fixes Bugzilla #231.
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
    91
	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
	if ( retval < 0 ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
		SDL_SetError("sem_wait() failed");
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
	return retval;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
	int retval;
6218
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   101
#ifdef HAVE_SEM_TIMEDWAIT
5105
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   102
	struct timeval now;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   103
	struct timespec ts_timeout;
6218
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   104
#else
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   105
	Uint32 end;
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   106
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
	if ( ! sem ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
		SDL_SetError("Passed a NULL semaphore");
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
		return -1;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
	/* Try the easy cases first */
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
	if ( timeout == 0 ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
		return SDL_SemTryWait(sem);
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
		return SDL_SemWait(sem);
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
6218
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   121
#ifdef HAVE_SEM_TIMEDWAIT
5105
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   122
	/* Setup the timeout. sem_timedwait doesn't wait for
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   123
	 * a lapse of time, but until we reach a certain time.
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   124
	 * This time is now plus the timeout.
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   125
	 */
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   126
	gettimeofday(&now, NULL);
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   127
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   128
	/* Add our timeout to current time */
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   129
	now.tv_usec += (timeout % 1000) * 1000;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   130
	now.tv_sec += timeout / 1000;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   131
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   132
	/* Wrap the second if needed */
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   133
	if ( now.tv_usec >= 1000000 ) {
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   134
		now.tv_usec -= 1000000;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   135
		now.tv_sec ++;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   136
	}
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   137
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   138
	/* Convert to timespec */
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   139
	ts_timeout.tv_sec = now.tv_sec;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   140
	ts_timeout.tv_nsec = now.tv_usec * 1000;
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   141
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   142
	/* Wait. */
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   143
	do
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   144
		retval = sem_timedwait(&sem->sem, &ts_timeout);
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   145
	while (retval == -1 && errno == EINTR);
99acf3d856cb Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents: 4159
diff changeset
   146
6297
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   147
	if (retval == -1) {
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   148
		if (errno == ETIMEDOUT) {
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   149
			retval = SDL_MUTEX_TIMEDOUT;
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   150
		}
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   151
		else {
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   152
			SDL_SetError(strerror(errno));
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   153
		}
c787fb1b5699 Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out
Sam Lantinga <slouken@libsdl.org>
parents: 6218
diff changeset
   154
	}
6218
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   155
#else
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   156
	end = SDL_GetTicks() + timeout;
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   157
	while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   158
		if ((SDL_GetTicks() - end) >= 0) {
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   159
			break;
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   160
		}
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   161
		SDL_Delay(0);
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   162
	}
f114f1434ff6 Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
Sam Lantinga <slouken@libsdl.org>
parents: 6137
diff changeset
   163
#endif /* HAVE_SEM_TIMEDWAIT */
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
	return retval;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
Uint32 SDL_SemValue(SDL_sem *sem)
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
	int ret = 0;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
	if ( sem ) {
1630
eba962f9d603 Fixed compile error
Sam Lantinga <slouken@libsdl.org>
parents: 1627
diff changeset
   172
		sem_getvalue(&sem->sem, &ret);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
		if ( ret < 0 ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
			ret = 0;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
		}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
	return (Uint32)ret;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
int SDL_SemPost(SDL_sem *sem)
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
	int retval;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
	if ( ! sem ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
		SDL_SetError("Passed a NULL semaphore");
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
		return -1;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
1630
eba962f9d603 Fixed compile error
Sam Lantinga <slouken@libsdl.org>
parents: 1627
diff changeset
   189
	retval = sem_post(&sem->sem);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
	if ( retval < 0 ) {
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
		SDL_SetError("sem_post() failed");
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
	}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
	return retval;
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
}
1627
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   195
aee7ea396f59 Fixed bug #179
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   196
#endif /* __MACOSX__ */